嗨, 我正在尝试使用CURL从以下网站获取网页,这是我的代码: 我的带有CURL设置的PHP文件
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url );
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300);
curl_setopt($ch, CURLOPT_TIMEOUT, 300); //timeout in seconds
$resultcurl = curl_exec($ch);
$curl_errno = curl_errno($ch);
$curl_error = curl_error($ch);
curl_close($ch);
在通过Stackoverflow浏览了CURL之后,我得到了一条建议如下的帖子
$header = get_headers('https://www.websitedomain.com/somepage');
echo '<pre>';
print_r($header);
echo '</pre>';
然后我得到了:
Array
(
[0] => HTTP/1.1 301 Moved Permanently
[1] => Cache-Control: no-cache, must-revalidate
[2] => Content-Type: text/html; charset=UTF-8
[3] => Date: Thu, 21 Jun 2018 06:50:00 GMT
[4] => Expires: Sun, 19 Nov 1978 05:00:00 GMT
[5] => Location: https://www.websitedomain.com/somepage
[6] => Server: Apache/2.4.33 (Amazon) OpenSSL/1.0.2k-fips PHP/5.6.36
[7] => X-Content-Type-Options: nosniff
[8] => X-Content-Type-Options: nosniff
[9] => X-Drupal-Cache: MISS
[10] => X-Powered-By: PHP/5.6.36
[11] => Content-Length: 10
[12] => Connection: Close
[13] => HTTP/1.1 200 OK
[14] => Cache-Control: no-cache, must-revalidate
[15] => Content-Language: en
[16] => Content-Type: text/html; charset=utf-8
[17] => Date: Thu, 21 Jun 2018 06:50:00 GMT
[18] => Expires: Sun, 19 Nov 1978 05:00:00 GMT
[19] => Link: ,,,,,
[20] => Server: Apache/2.4.33 (Amazon) OpenSSL/1.0.2k-fips PHP/5.6.36
[21] => Vary: Accept-Encoding
[22] => X-Content-Type-Options: nosniff
[23] => X-Content-Type-Options: nosniff
[24] => X-Drupal-Cache: MISS
[25] => X-Generator: Drupal 7 (http://drupal.org)
[26] => X-Powered-By: PHP/5.6.36
[27] => X-UA-Compatible: IE=edge,chrome=1
[28] => Connection: Close
)
内容长度始终显示为“ 10”,当我直接通过浏览器访问该页面时,它会起作用!有人可以帮忙!!吗?
答案 0 :(得分:1)
HTTP / 1.1 301永久移动
您将获得重定向。您需要遵循它,哪些浏览器会自动执行,但是默认情况下不会启用PHP的cURL库。您可以使用以下方法进行更改:
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);