【问题标题】:curl request returning wrong response codecurl请求返回错误的响应代码
【发布时间】:2012-05-05 02:29:28
【问题描述】:

我需要从 sitemap.xml 文件中获取页面 URL 的 http 响应代码。当我通过我的 cron 进程获取响应代码时,它返回 403(称为访问禁止:尽管我可以从浏览器访问传递的 url)。

但如果我从本地主机运行相同的代码,它会返回正确的 http 响应代码(即 200)。

为什么从本地主机和服务器返回不同的http响应码有区别??如何解决问题?

http响应码的提取代码如下。

function check_response_code() {
    $pageurl='http://www.certona.com/online-merchandising/';
    $trimurl = '';
    $start = '';
    $end = '';
    $total = '';

    $start = microtime(true);
    $response_code = '';
    if (!stristr($pageurl, "http://"))
    {
        if (!stristr($pageurl, "https://"))
        {
            $trimurl = "http://" . $pageurl;
        } else
        {
            $trimurl = $pageurl;
        }
    } else
    {
        $trimurl = $pageurl;
    }
    $curl = curl_init();
    //don't fetch the actual page, you only want headers

    curl_setopt($curl, CURLOPT_URL, $trimurl);
    curl_setopt($curl, CURLOPT_NOBODY, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_FILETIME, true);

    $result = curl_exec($curl);

    $timestamp = curl_getinfo($curl, CURLINFO_FILETIME);
    $response_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
    $mime_type = curl_getinfo($curl, CURLINFO_CONTENT_TYPE);
    $end = microtime(true);
    $total = round($end - $start, 5);

    if ($timestamp != -1)
    { //otherwise unknown
        $arr=array(date("Y-m-d H:i:s", $timestamp), $response_code, $total, $mime_type); //etc
    } else
    {
        $arr=array("", $response_code, $total, $mime_type);
    }
    echo "<pre>";
    print_r($arr);
    echo "</pre>";
}

谢谢你..

【问题讨论】:

    标签: php curl cron http-response-codes


    【解决方案1】:

    不确定,但您的代码似乎可以正常工作

    试试

    check_response_code();
    
    function check_response_code() {
        $pageurl='http://www.certona.com/online-merchandising/';
        $curl = curl_init($pageurl);
        curl_setopt($curl, CURLOPT_NOBODY, true);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_FILETIME, true);
    
        $result = curl_exec($curl);
        $info = curl_getinfo($curl);
        $info['filetime'] = date("Y-m-d H:i:s", $info['filetime']);
        echo "<pre>";
        print_r($info);
        echo "</pre>";
    }
    

    输出

    Array
    (
        [url] => http://www.certona.com/online-merchandising/
        [content_type] => text/html; charset=utf-8
        [http_code] => 200
        [header_size] => 488
        [request_size] => 76
        [filetime] => 2012-04-24 15:11:28
        [ssl_verify_result] => 0
        [redirect_count] => 0
        [total_time] => 1.342
        [namelookup_time] => 0
        [connect_time] => 0.25
        [pretransfer_time] => 0.25
        [size_upload] => 0
        [size_download] => 0
        [speed_download] => 0
        [speed_upload] => 0
        [download_content_length] => 0
        [upload_content_length] => 0
        [starttransfer_time] => 1.342
        [redirect_time] => 0
        [certinfo] => Array
            (
            )
    
        [redirect_url] => 
    )
    

    【讨论】:

    • hello ...这是来自 localhost 的输出。但是当我从服务器上的 cron 进程中尝试相同的代码时,它返回不同的 http 响应代码..(即 403 而不是 200)!
    【解决方案2】:

    这可能有很多原因......

    是你自己的服务器吗? => http://codewithdesign.com/2011/05/26/curl-403-error-returning/

    可能将 CURLOPT_USERAGENT 设置为“Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0”

    或阅读此curl gives 403 error?

    【讨论】:

    • CURLOPT_USERAGENT 在我在请求之间保持 sleep(10) 时有些帮助.. 但如果我不使用 sleep(10) ,我会在一段时间后得到 403 响应代码。
    【解决方案3】:

    您的 localhost 通过您的计算机运行 curl。就像您的浏览器使用您的 IP 地址和其他内容打开了该网站。

    服务器以另一种方式进行。

    我记得有一次我通过删除 URL 中的尾随 / 解决了一个类似的问题。

    尝试运行代码

    $pageurl = rtrim('http://www.certona.com/online-merchandising/', '/)';
    

    但基本上我认为您不允许从其他站点获取目录数据。
    网址不应该以.xml 结尾以获取站点地图吗?

    $pageurl = 'http://www.certona.com/sitemap.xml';
    

    【讨论】:

    • 嗨 .. sitemap.xml 包含网站的链接网址。这里的页面 URL 是来自 sitemap.xml 的链接 URL 之一。谢谢你的回复。
    • 嗨..我已尝试删除“/”并保留“/”。但是本地的卷曲响应没有区别。顺便说一句,我在本地得到响应代码 200,在服务器上得到响应代码 403! .
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-18
    • 1970-01-01
    • 2017-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-18
    相关资源
    最近更新 更多