简体   繁体   English

PHP的get_headers异常处理

[英]php get_headers exception handling

I am running the following command and getting an exception: 我正在运行以下命令并出现异常:

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

How do I handle this exception (in my case, ignore this url as it is causing an exception). 如何处理此异常(在我的情况下,请忽略此URL,因为它会导致异常)。

I have tried: 我努力了:

  1. try/catch 试着抓
  2. The code in this link: https://stackoverflow.com/a/6184127/1486303 此链接中的代码: https : //stackoverflow.com/a/6184127/1486303

However, I still get this error appear (which I want ignored). 但是,我仍然出现此错误(我想忽略)。

Thanks! 谢谢!

NEW VERSION 新版本

Again this's not the right answer of the question, but avoiding the error, can give the expected result. 同样,这不是问题的正确答案,但是避免错误,可以得到预期的结果。

get_headers do an HTTP GET without Agent, so moneysupermarket.com don't like it, so use ini_set to set the default user agent in request, and all work well: get_headers在没有代理的情况下执行HTTP GET,因此moneysupermarket.com不喜欢它,因此请使用ini_set设置请求中的默认用户代理,并且一切正常:

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

PREVIOUS 以前

Apparently moneysupermarket.com reset a connection if request is not well formatted, do the request using cUrl (take from curl man page): 显然,如果请求的格式不正确,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);

There's no exception. 也不例外。 get_headers() returns FALSE on error. get_headers()在错误时返回FALSE。 There's a warning message though, but that is no exception, thus cannot get catched. 虽然有警告消息,但这也不例外,因此无法被捕获。 For warnings and other errors see: http://www.php.net/manual/en/function.set-error-handler.php 有关警告和其他错误,请参见: 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