PHP cURL在服务器上返回400

时间:2014-03-26 18:26:09

标签: php curl

我有一些通过php curl运行的URL,以便我可以获取HTTP代码。有趣的是,一些URL工作正常并返回200,但其他人总是返回400.问题只存在于我从数据库中提供URL时,如果我通过硬编码运行它然后它返回200应该。

我尝试过urlencoding / decode,这不是问题。外观看起来不错。我能找到的一件奇怪的事情就是400网址总是有[content_type] => text/html; charset=iso-8859-1。网址也很简单https://sometext.something.something/test,没有任何参数

这是我一直在运行URL的PHP​​代码。它在我的localhost上工作正常,但在服务器上它会导致问题。

function checkStatus($url) {
    $urlencode=urlencode($url);
    foreach (explode('&', $urlencode) as $chunk) {
        $param = explode("=", $chunk);

        if ($param) {
            echo urldecode($param[0]);
            $ch = curl_init(urldecode($param[0]));
        }
    }

    //$ch = curl_init($urlencode);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    // curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    // curl_setopt($ch, CURLINFO_CONTENT_TYPE, true);
    // curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:text/html;charset=UTF-8'));
    curl_exec($ch);
    $returnCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    $error_report= curl_getinfo($ch);
    print_r($error_report);
    curl_close($ch);

    return $returnCode;
}

以下是错误消息的示例:

Array
(
    [url] => https://url.net/something
    [content_type] => text/html; charset=iso-8859-1
    [http_code] => 400
    [header_size] => 171
    [request_size] => 83
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0.056926
    [namelookup_time] => 0.003457
    [connect_time] => 0.004276
    [pretransfer_time] => 0.01614
    [size_upload] => 0
    [size_download] => 0
    [speed_download] => 0
    [speed_upload] => 0
    [download_content_length] => -1
    [upload_content_length] => 0
    [starttransfer_time] => 0.056906
    [redirect_time] => 0
    [certinfo] => Array
        (
        )

)

2 个答案:

答案 0 :(得分:1)

您的网址为https://,这就是为什么它不适合您。

如果数据安全性不是您的问题,那么您可以使用以下选项为ssl使用不安全的通信。

 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);

答案 1 :(得分:0)

我在卷曲时也遇到了同样的400错误。 这是我的错误结果。

Array
(
    [url] => https://www.uwgc.org/give/donor-networks/step-up-program/2016/03/14/oh-the-places-she's-going
    [content_type] => text/html; charset=utf-8
    [http_code] => 400
    [header_size] => 269
    [request_size] => 482
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 2.320807
    [namelookup_time] => 8.1E-5
    [connect_time] => 0.245886
    [pretransfer_time] => 1.853379
    [size_upload] => 0
    [size_download] => 3808
    [speed_download] => 1641
    [speed_upload] => 0
    [download_content_length] => 3808
    [upload_content_length] => -1
    [starttransfer_time] => 2.320739
    [redirect_time] => 0
    [redirect_url] => 
    [primary_ip] => 185.158.118.183
    [certinfo] => Array
        (
        )

    [primary_port] => 80
    [local_ip] => 10.128.0.54
    [local_port] => 55667
)