TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 09-25-2007, 08:16 PM   #1 (permalink)
The Acquainted
Inquisitive 
 
WinSrev's Avatar
 
Join Date: Sep 2007
Posts: 133
Thanks: 6
WinSrev is on a distinguished road
File_Get_Contents exception

Is it possible to somehow catch the response of file_get_contents like for example:

if everything is okay with the URL go ahead and continue, if an error occured we should try something else?

Thanks!
Send a message via ICQ to WinSrev
WinSrev is offline  
Reply With Quote
Old 09-25-2007, 08:24 PM   #2 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

To put it simply, no. file_get_contents is a highly primitive function. cURL is the way to go if you want more advanced feedback. I have written you a function that will do just this, if the HTTP code is not 200 (OK) then it will return false, else it will return the contents of the website.

PHP Code:
function file_get_conditional_contents($szURL)
{
    
$pCurl curl_init($szURL);
    
    
curl_setopt($pCurlCURLOPT_RETURNTRANSFERtrue);
    
curl_setopt($pCurlCURLOPT_FOLLOWLOCATIONtrue);
    
curl_setopt($pCurlCURLOPT_TIMEOUT10);

    
$szContents curl_exec($pCurl);
    
$aInfo curl_getinfo($pCurl);
    
    if(
$aInfo['http_code'] === 200)
    {
        return 
$szContents;
    }
    
    return 
false;
}

echo 
file_get_conditional_contents('http://www.google.com/'); 
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 09-25-2007, 08:31 PM   #3 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

wild, where have you learned all? :P You seem to know the solution to every problem, just wondering where you've learned :)
Tanax is offline  
Reply With Quote
Old 09-25-2007, 08:54 PM   #4 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Good question, sir! Reading has taught me a lot, naturally. Though the best education tool is just having a stab at it yourself and then having people to ask if you really can't figure it out - that's where TalkPHP comes in :) !
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 09-25-2007, 08:57 PM   #5 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Uhm, so do you have an idea on what I can code? Cause I'm out of ideas..

And nothing too hard, I'm sure you know how much knowledge I have, by now :)
Tanax is offline  
Reply With Quote
Old 09-25-2007, 09:03 PM   #6 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

I remember I started off with a simple code paster. You are presented with a textfield for the code, a dropdown for the language. Submit the code and you're then given a link to show to any one who's interested in helping you. Can have that with line numbers. Very much like NoPaste.

Other than that I'm not really any good at dreaming up scenarios :(
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 09-25-2007, 09:04 PM   #7 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Damn, haha, that sounds hard... okay, I'll try.. >.< :P
Tanax is offline  
Reply With Quote
Old 09-25-2007, 09:25 PM   #8 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Quote:
Originally Posted by WinSrev View Post
Is it possible to somehow catch the response of file_get_contents like for example:

if everything is okay with the URL go ahead and continue, if an error occured we should try something else?

Thanks!
I'm not 100% clear on what you're asking. file_get_contents() will 'catch' the response from whatever URL you pass to it, that is the function's purpose. If you're wanting to look at the headers returned from the remote server, then after calling file_get_contents() you can access the global variable, $http_response_header which holds all of the headers from the last response.

To check for "200" like Wildhoney's CURL example you can do:
PHP Code:
<?php

// Fetch the contents of the URL
$szContents file_get_contents('http://domain.com/file.txt');

// $http_response_header contains the headers, the first header
// will always be the response code.
// E.g. "HTTP/1.1 200 OK"
if (strpos('200'$http_response_header[0]))
{
    echo 
'OK';
}
else
{
    echo 
'FAIL';
}

?>
Salathe is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 09:33 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design