標籤:串連 user flat var_dump referer 內容 允許 headers http
今天對接支付介面,需要擷取支付頁面,發現支付商那邊給的連結會發送302 跳轉,最後發現該方法,絕對給力:
<?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/51954562
php curl 請求302跳轉頁面