2015-09-29 53 views
1

我想在我的PHP應用程序中使用cURL。我去了enter link description here,並找到了它的文件。但是當我想要實現我的代碼時,我發現cURL並不總是在無效的URL上返回錯誤。
爲了確保它,我將this example中提到的URL更改爲http://www.googles123.com,這是無效的和註冊的。爲什麼cURL不會在無效URL上返回錯誤?

<?php 
// Create a curl handle to a non-existing location 
$ch = curl_init('http://www.googles123.com/'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

if(curl_exec($ch) === false) 
{ 
    echo 'Curl error: ' . curl_error($ch); 
} 
else 
{ 
    echo 'Operation completed without any errors'; 
} 

// Close handle 
curl_close($ch); 
?> 

的結果操作完成,沒有任何錯誤!儘管此代碼在其默認URL http://404.php.net/上返回了錯誤。爲什麼?

+0

你必須curl_getinfo($ CH)的使用和查看HTML的ERROR_CODE。所有錯誤都在curl_errno($ ch)中。 – SoftGuide

回答

0
<?php 
$ch = curl_init("http://www.googles123.com/"); 

// Send request 
curl_exec($ch); 

// Check for errors and display the error message 
if($errno = curl_errno($ch)) { 
    $error_message = curl_strerror($errno); 
    echo "cURL error ({$errno}):\n {$error_message}"; 
} 

// Close the handle 
curl_close($ch); 
?> 

捲曲錯誤消息:無法解析主機名

你可以試試上面的代碼?

+0

沒有工作。 –

+0

它的工作。請在個別文件中查看以上代碼。 你能告訴我你喜歡Wamp/Xampp使用哪個服務器嗎? –

+1

這真的很奇怪。現在你的和我的代碼都在工作!我正在使用XAMPP。怎麼了? –

0

你可以嘗試CURLOPT_FAILONERROR設置爲false

**curl_setopt($ch , CURLOPT_FAILONERROR , false);** 
相關問題