Question

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!

Était-ce utile?

La solution

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

Autres conseils

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

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top