简体   繁体   中英

php get_headers exception handling

I am running the following command and getting an exception:

$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).

I have tried:

  1. try/catch
  2. The code in this link: 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:

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

// 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM