473,386 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Trapping file_get_contents for bad urls

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.

Dec 10 '05 #1
3 2653
In theory, you can create a stream context, then attach a callback
function to it to receive notifications:

$context = stream_context_create(array());
stream_context_set_params($context, array('notification' =>
'stream_callback'));
$f = fopen("http://www.google.com/", "rb", false, $context);

The parameters passed to the function are as followed:

function stream_callback(
$notifycode,
$severity,
$xmsg,
$xcode,
$bytes_sofar,
$bytes_max)

Notification codes are defined as such:

1 = RESOLVE
2 = CONNECT
3 = AUTH_REQUIRED
4 = MIME_TYPE_IS
5 = FILE_SIZE_IS
6 = REDIRECTED
7 = PROGRESS
8 = COMPLETED
9 = FAILURE
10 = AUTH_RESULT

Severity codes:

0 = INFO
1 = WARNING
2 = ERROR

In practice, however, the PHP stream API is somewhat of a
work-in-progress. Not everything is listed above is actually
implemented. As of PHP 4.3.6, the callback doesn't receive
notifications for the resolve stage.

Dec 10 '05 #2
Csaba Gabor wrote:
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; }

<snip>
Try cURL
http://www.php.net/curl

Dec 11 '05 #3
NC
Csaba Gabor wrote:

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?
Not if you insist on using file_get_contents()...
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.


Consider an alternative function that does something like this:

1. Parse the argument URL into parts using parse_url() to
verify that it is not obvously malformed.
2. Run the server name through gethostbyname() to verify
that the host name in fact resolves to an IP address.
3. Connect to the server using fsockopen() to verify that
it is operational.
4. Attempt to retrieve the file by emulating a GET request
with fputs() and reading response by fgets().
5. Parse the response headers and, if applicable, the body.

Cheers,
NC

Dec 11 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Rob | last post by:
for some reason i keep getting : Warning: file_get_contents(): php_network_getaddresses: getaddrinfo failed: Temporary failure in name resolution in /home/rob/www/php/filegetcontents2.php on...
13
by: Thelma Lubkin | last post by:
I use code extensively; I probably overuse it. But I've been using error trapping very sparingly, and now I've been trapped by that. A form that works for me on the system I'm using, apparently...
11
by: Mark Rae | last post by:
Hi, I've inherited an ASP.NET web app which I want to restructure into a more logical folder hierarchy (over 150 WebForms in the app's root), but lots of users access the site by fully-qualified...
1
by: jjainschigg | last post by:
I'm running PHP 5 on a shared VPS. I have a couple little apps that are using file_get_contents() and readfile() for various kinds of (what should be) low-overhead, fast outbound accesses ... that...
5
by: howa | last post by:
are there any advantage in replacing all fread() operations with file_get_contents() ? i.e. file_get_contents("/usr/local/something.txt") VS $filename = "/usr/local/something.txt";
4
by: Aetherweb | last post by:
This is probably really obvious, sorry, been a long day... I'm wanting to create a PHP file which is a template for an email, and read the file into a string, ready to send out using my email...
9
by: howa | last post by:
Target: To fetch a file using PHP and send to user Method 1: echo file_get_contents( $file_path ); Method 2: readfile( $file_path );
6
by: Taras_96 | last post by:
Hi everyone, The output of echo file_get_contents("http://watchout4snakes.com/creativitytools/ RandomWord/RandomWordPlus.aspx"); leaves the browser empty.. no error messages, nothing. ...
0
by: pac1250 | last post by:
Hi, I am searching how to solve a problem and I dont find it :( I want to access a page from a script behind a proxy : (my script) <-(a proxy with authentification) <-(https serveur with...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.