【问题标题】:Curl GET response empty result卷曲 GET 响应空结果
【发布时间】:2018-11-30 09:57:48
【问题描述】:

你好, 我正在尝试让您使用 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”,当我通过浏览器直接访问该页面时,它可以正常工作!有人可以帮忙吗!!?

【问题讨论】:

    标签: php curl https get header


    【解决方案1】:

    HTTP/1.1 301 永久移动

    您正在获得重定向。您需要遵循它,哪些浏览器会自动执行,但默认情况下哪些 PHP 的 cURL 库不会。你可以改变它:

    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
    

    【讨论】:

    • 谢谢@quentin!
    猜你喜欢
    • 1970-01-01
    • 2018-06-07
    • 1970-01-01
    • 2019-03-08
    • 1970-01-01
    • 1970-01-01
    • 2019-09-19
    • 1970-01-01
    • 2021-01-03
    相关资源
    最近更新 更多