17

The following code is to query an online thesaurus for a search engine I'm building as a college project, but I'm having problems with file_get_contents "failed to open stream" errors. When I send a word the thesaurus doesn't recognize, it throws up an error. I'm trying to write a piece of code that will ignore the error and just proceed without the information.

$thesaurus_search="http://words.bighugelabs.com/api/2/0089388bb57f/".$this->formatted_query."/php";
$result_thesaurus=file_get_contents($thesaurus_search);

I tried:

if (file_get_contents($thesaurus_search) != NULL)
{ // do stuff }

...but its not working because it still returns some sort of string.

What can I do to handle such cases?

4
  • 1
    What exactly does it return when there is an error? Some html? xml? json? You'd have to parse the returned data to check for the error codes - it's unlikely an api would simply return nothing.
    – Marc B
    Commented Jul 13, 2012 at 17:50
  • "I'm trying to write a piece of code that will ignore the error and just proceed without the information". How would you proceed without the information?
    – netcoder
    Commented Jul 13, 2012 at 17:53
  • Maybe I should be a little more specific, this code is part of a class, the information it returns is not intrinsic to the end product, its just a bonus. So if file_get_contents is returning an error(because it doesnt have a match for the word) I want to break out of the class altogether, and not return any code. Commented Jul 13, 2012 at 18:26
  • Because you should not check for NULL, but for FALSE like this if($result_thesaurus !== false) ( do_whatever with $result_thesaurus } else { do_whatever you like, but don't use $result_thesaurus because it contains the error message }
    – RWC
    Commented Aug 26, 2023 at 14:01

4 Answers 4

61

If you don't want file_get_contents to report HTTP errors as PHP Warnings, then this is the clean way to do it, using a stream context (there is something specifically for that):

$context = stream_context_create(array(
    'http' => array('ignore_errors' => true),
));

$result = file_get_contents('http://your/url', false, $context);
3
  • 2
    Thanks Netcoder thats much better, appreciate your help. I'll stop hitting my computer with wrenches too :) Commented Jul 14, 2012 at 10:18
  • Thanks much! I found it within the manual after much probing, but it wasn't readily apparent to me a context option could help. It'd be nice if this were right on the file_get_contents page.
    – Errol
    Commented Feb 17, 2014 at 17:38
  • 1
    this is one of those things that's been annoying me for a while but didn't bother til now. @ should work but doesn't which is stupid. "PHP supports one error control operator: the at sign (@). When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored." seems simple enough but is a lie. Commented Jan 31, 2018 at 5:53
1

The simplest solution if you're okay with just bailing out, would be:

if (empty($thesaurus_search)) { 
   return;
} else {
   //process with value
}

To more fully handle it, looking at the API, it looks like you should be checking the response header, e.g.:

$thesaurus_search="http://words.bighugelabs.com/api/2/0089388bb57f/".$this->formatted_query."/php";
$result_thesaurus=file_get_contents($thesaurus_search);
if ($http_response_header[0] = 'HTTP/1.1 200 OK') {
    //code to handle words
} else {
    // do something else?
}
1
  • 1
    Considering file_get_contents already returns false on error, I don't think that's necessary.
    – netcoder
    Commented Jul 13, 2012 at 18:14
0

If I understand you properly you are trying to make an API call to http://words.bighugelabs.com. You need cURL to achieve this so if you have cURL installed then this code will work for you.

$ch = curl_init();
$thesaurus_search="http://words.bighugelabs.com/api/2/0089388bb57f/".$this->formatted_query."/php";
$options = array();
$options[CURLOPT_URL] = $thesaurus_search;
$options[CURLOPT_RETURNTRANSFER] = true;
curl_setopt_array($ch, $options);

// Print result.
print_r(curl_close($ch));
1
  • 5
    "You need cURL to achieve this", no he doesn't.
    – netcoder
    Commented Jul 13, 2012 at 18:05
-1

You might try curl:

function curl_get_contents($url) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)");
    curl_setopt($ch, CURLOPT_MAXREDIRS, 2); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $content = curl_exec($ch);
    curl_close($ch);
    return $content;
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.