时间:2020-01-15加入收藏
<?php
$url = 'http://www.bingdou.net/';
$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);
TGA: 技巧