2014-03-26 84 views
0

我有幾個正在通過php curl運行的URL,以便我可以獲取HTTP代碼。有趣的是,一些URL的工作正常,並返回200,但其他人總是返回400.只有當我從數據庫中提供URL時,如果我通過硬編碼運行它,然後返回200,它應該。PHP cURL在服務器上返回400

我試過urlencoding /解碼,它不是問題。外觀看起來很好。我能夠發現的一個奇怪的事情是,400 URL始終有一個[content_type] => text/html; charset=iso-8859-1。網址也很簡單https://sometext.something.something/test,沒有任何參數

這是我一直在運行URL的PHP​​代碼。它適用於我的本地主機,但在服務器上會導致問題。

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 
     (
     ) 

) 
+0

您是否嘗試過使用parse_url而不是urlencode/urldecode? –

+0

@ Arkadiusz'flies'Rzadkowolski不,我沒有。現在測試。編輯:嗯,我真的不知道它在這裏如何幫助。它以預期的格式返回url,但沒有其他的東西。 – Envious

回答

1

您的網址有https://這就是爲什麼它不爲你工作。

如果您的數據安全性不是問題,那麼您可以使用以下選項爲ssl使用不安全的通信。

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false); 
+0

是的!有效!儘管在編寫代碼的同時,我注意到如果我將查詢更改爲僅輸出1個url(限制1),它將返回200,即使它是相同的url,返回400時select * – Envious

相關問題