Reply to Trapping file_get_contents for bad urls

Your name:

Reply:


Posted by Csaba Gabor on 12/10/05 15:47

I've got a section of code that takes a user supplied $url and
essentially does:
function redirectRequest($url) {
$resultRemote = @file_get_contents($url);
return $resultRemote; }

Is there a reasonable way to trap for and differentiate between the
major (most common) types of failure?

For example, common scenarios in which I would not get back a desired
response include: malformed url, the domain can't be mapped (ie. the
DNS servers don't return an IP address), the domain is not reachable,
the directory or file is not found, standard HTTP error response
header.


I'm sure it's been solved many times over, but my searches didn't lead
me to substantive results. My initial attempt only captures the fact
that there was a problem, but not the nature of it:

function redirectRequest($url) {
$oldTrack = ini_set("track_errors","true"); // allow $php_errormsg
to be set
$php_errormsg = ""; // initialize; will be set
if error with next line
$resultRemote = @file_get_contents($url);
ini_set("track_errors",$oldTrack); // restore setting
if ($php_errormsg) return -1; // this site had a problem
return $resultRemote; }


Thanks for any pointers,
Csaba Gabor from Vienna

Note: This is to be used between cooperating domains where each server
puts out a serialized response when it receives a redirectRequest as
above. So if the requesting site can't unserialize the return value,
it knows there is a problem, but this would still leave a user (which
for now is me) confused as to the source of the problem.

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация