今天對接支付接口,須要獲取支付頁面,發現支付商那邊給的連接會發送302 跳轉,最後發現該方法,絕對給力:php
<?php $url = 'http://auto.jrj.com.cn/'; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //若給定url自動跳轉到新的url,有了下面參數可自動獲取新url內容:302跳轉 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //設置cURL容許執行的最長秒數。 curl_setopt($ch, CURLOPT_TIMEOUT, 10); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:47.0) Gecko/20100101 Firefox/47.0'); curl_setopt($ch, CURLOPT_REFERER, $url); curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate'); $content = curl_exec($ch); //獲取請求返回碼,請求成功返回200 $code = curl_getinfo($ch,CURLINFO_HTTP_CODE); echo $code . "\n\n"; //獲取一個cURL鏈接資源句柄的信息。 //$headers 中包含跳轉的url路徑 $headers = curl_getinfo($ch); var_dump($headers);
參考地址:http://blog.csdn.net/u013372487/article/details/51954562curl