繁体   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