File_get_contents Error Handler
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow file_get_contents handle 404 the company Business Learn more about hiring developers or posting ads with us Stack php file_put_contents catch error Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community
File_get_contents 400 Bad Request
of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How can I handle the warning of file_get_contents() function in PHP? up vote 177 down vote favorite
Warning File_get_contents Failed To Open Stream
55 I wrote a PHP code like this $site="http://www.google.com"; $content = file_get_content($site); echo $content; But when I remove "http://" from $site I get the following warning: Warning: file_get_contents(www.google.com) [function.file-get-contents]: failed to open stream: I tried try and catch but it didn't work. php function exception-handling warnings share|improve this question edited Aug 29 '13 at 13:05 lorem monkey 2,06622137 asked Nov 7 '08 at 15:10 Waseem 3,804113442 1 Also an interesting approach: @file_get_contents in php stackoverflow.com/questions/6718598/… –Hugo Stieglitz Jun 19 '12 at 13:15 Related: stackoverflow.com/q/2002610 –Fr0zenFyr Jun 30 '15 at 19:35 add a comment| 16 Answers 16 active oldest votes up vote 287 down vote accepted Step 1: check the return code: if($content === FALSE) { // handle error here... } Step 2: suppress the warning by putting an @ in front of the file_get_contents: $content = @file_get_contents($site); share|improve this answer edited Jan 2 '14 at 16:29 answered Nov 7 '08 at 15:14 Roel 12.7k54066 46 Remember to use strict comparison: if ($content === FALSE) .If the file contains "0", then it will trigger a false negative. –Aram Kocharyan Jun 24 '11 at 3:48 3 Hi, this didn't work for me, adding @ still causes E_WARNING to be caught by some global (not mine) error handler, and my script dies before I have a chance to handle the return value. Any ideas? tnx. –Sagi Mann Nov 22 '12 at 6:51 1 There is a typo in your answer file_get_content should be file_get_contents –Khawer Zeshan Jan 2 '14 at 15:28 2 Why not use error_reporting(~E_WARNING); –irfandar May 10 '14 at 7:23 1 Though the answer is very old, I still suggest adding a note to your answer that using @ may
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings
File_get_contents Warning
and policies of this site About Us Learn more about Stack file get contents failed to open stream Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs file_get_contents fails silently Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; http://stackoverflow.com/questions/272361/how-can-i-handle-the-warning-of-file-get-contents-function-in-php it only takes a minute: Sign up file_get_contents when url doesn't exist up vote 44 down vote favorite 9 I'm using file_get_contents() to access a URL. file_get_contents('http://somenotrealurl.com/notrealpage'); If the URL is not real, it return this error message. How can I get it to error gracefully so that I know that the page doesn't exist and act accordingly without displaying http://stackoverflow.com/questions/4358130/file-get-contents-when-url-doesnt-exist this error message? file_get_contents('http://somenotrealurl.com/notrealpage') [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found in myphppage.php on line 3 for example in zend you can say: if ($request->isSuccessful()) $client = New Zend_Http_Client(); $client->setUri('http://someurl.com/somepage'); $request = $client->request(); if ($request->isSuccessful()) { //do stuff with the result } php file-get-contents share|improve this question edited Dec 14 '15 at 5:28 Clayton Smith 3,56921129 asked Dec 5 '10 at 9:17 sami 2,05771935 try using stream context: stackoverflow.com/questions/21800276/… , file_get_contents uses fopen under the hood. –rsk82 Feb 15 '14 at 19:24 add a comment| 6 Answers 6 active oldest votes up vote 73 down vote accepted You need to check the HTTP response code: function get_http_response_code($url) { $headers = get_headers($url); return substr($headers[0], 9, 3); } if(get_http_response_code('http://somenotrealurl.com/notrealpage') != "200"){ echo "error"; }else{ file_get_contents('http://somenotrealurl.com/notrealpage'); } share|improve this answer edited Feb 10 '15 at 8:20 Jaylord Ferrer 454 answered Dec 5 '10 at 9:20 ynh 1,6701017 4 This technique is preferable to mine if you need to know why the request failed, ie. checking for status code (404 may need to
this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can http://board.phpbuilder.com/showthread.php?10344146-RESOLVED-Try-Catch-with-File_get_contents post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. Results 1 to 7 of 7 Thread: [RESOLVED] Try/Catch with File_get_contents Tweet Thread Tools Show Printable Version Email this Page… Subscribe to this Thread… Search Thread Advanced Search Display Linear Mode Switch to Hybrid failed to Mode Switch to Threaded Mode 08-16-2007,02:47 PM #1 dougal85 View Profile View Forum Posts Senior Member Join Date Apr 2006 Posts 1,349 [RESOLVED] Try/Catch with File_get_contents I'm not sure if I am using try/catch incorrectly but here is my example, PHP Code: try{
The way I see it, try catch is good for this since there is a chance the resource is external and you cant be sure that its accessible. What I don't understand is, why do you still get the "failed to open stream: HTTP request failed!" warning when the code is within the try/catch block Isn't the try/catch supposed to stop the errors being displayed then go to the catch block. Then you can choose to do what you want with the error message inside the Exception object. What seems to happen is you get the warning and it goes to the catch... Reply With Quote 08-16-2007,03:36 PM #2 dougal85 View Profile View Forum Posts Senior Member Join Date Apr 2006 Posts 1,349 Damn, Seems I messed up in my example testing somehow. It seems file_get_contents doesn't throw and exception, so try/catch doe
$xml=file_get_contents("http://www.someotherdomain.com/file.txt");
}catch(Exception$e){
echo"LoadFailed\n";
}