php curl 請求302跳轉頁面


今天對接支付接口,需要獲取支付頁面,發現支付商那邊給的鏈接會發送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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM