How to send cookies with file_get_contents

First, this is probably just a typo in your question, but the third arguments to file_get_contents() needs to be your streaming context, NOT the array of options. I ran a quick test with something like this, and everything worked as expected $opts = array(‘http’ => array(‘header’=> ‘Cookie: ‘ . $_SERVER[‘HTTP_COOKIE’].”\r\n”)); $context = stream_context_create($opts); $contents = … Read more

file_get_contents when url doesn’t exist

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’); }

file_get_contents() error

Your server must have the allow_url_fopen property set to true. Being on a free webhost explains it, as it’s usually disabled to prevent abuse. If you paid for your hosting, get in contact with your host so they can enable it for you. If changing that setting is not an option, then have a look … Read more

Ignoring errors in file_get_contents HTTP wrapper?

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);

file_get_contents throws 400 Bad Request error PHP

You might want to try using curl to retrieve the data instead of file_get_contents. curl has better support for error handling: // make request $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, “http://api.twitter.com/1/statuses/user_timeline/User.json”); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($ch); // convert response $output = json_decode($output); // handle error; error output if(curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) { var_dump($output); } curl_close($ch); … Read more