curl请求返回错误的响应代码

时间:2012-04-24 12:48:37

标签: php curl cron http-response-codes

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

但是如果我从localhost运行相同的代码,它会返回正确的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>";
}

谢谢..

3 个答案:

答案 0 :(得分:0)

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

你是自己的服务器吗? =&GT; 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?

答案 1 :(得分:0)

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

服务器以另一种方式完成它。

我记得有一次我通过移除网址中的尾随/解决了一个熟悉的问题。

尝试将代码作为

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

但基本上我认为你不允许从另一个站点获取目录的数据 网址不应该以{{1​​}}结尾才能获得网站地图吗?

.xml

答案 2 :(得分:0)

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

尝试

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] => 
)