簡體   English   中英

PHP的get_headers異常處理

[英]php get_headers exception handling

我正在運行以下命令並出現異常:

$ headers = get_headers(“ http://www.moneysupermarket.com/mortgages/”,1 );

如何處理此異常(在我的情況下,請忽略此URL,因為它會導致異常)。

我努力了:

  1. 試着抓
  2. 此鏈接中的代碼: https : //stackoverflow.com/a/6184127/1486303

但是,我仍然出現此錯誤(我想忽略)。

謝謝!

新版本

同樣,這不是問題的正確答案,但是避免錯誤,可以得到預期的結果。

get_headers在沒有代理的情況下執行HTTP GET,因此moneysupermarket.com不喜歡它,因此請使用ini_set設置請求中的默認用戶代理,並且一切正常:

ini_set("user_agent","Mozilla custom agent");
$headers = get_headers("http://www.moneysupermarket.com/mortgages/", 1);

以前

顯然,如果請求的格式不正確,moneysupermarket.com會重置連接,請使用cUrl進行請求(取自curl手冊頁):

// create a new cURL resource
$ch = curl_init();

// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.moneysupermarket.com/mortgages/");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla custom agent");

// grab URL and pass it to the browser
curl_exec($ch);

// close cURL resource, and free up system resources
curl_close($ch);

也不例外。 get_headers()在錯誤時返回FALSE。 雖然有警告消息,但這也不例外,因此無法被捕獲。 有關警告和其他錯誤,請參見: http : //www.php.net/manual/zh/function.set-error-handler.php

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM