php curl支援302域名跳轉
阿新 • • 發佈:2018-11-28
$url = 'http://sdk.x.com/index.php/callback?sign=a515afa7c4fd539765d53a54d5166f69¬ify_time=2017-06-19 16:32:55';
$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);
$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);