Alternative to file_get_contents?

Use cURL. This function is an alternative to file_get_contents. function url_get_contents ($Url) { if (!function_exists(‘curl_init’)){ die(‘CURL is not installed!’); } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $Url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $output = curl_exec($ch); curl_close($ch); return $output; }

Replace a whole line where a particular word is found in a text file

One approach that you can use on smaller files that can fit into your memory twice: $data = file(‘myfile’); // reads an array of lines function replace_a_line($data) { if (stristr($data, ‘certain word’)) { return “replacement line!\n”; } return $data; } $data = array_map(‘replace_a_line’, $data); file_put_contents(‘myfile’, $data); A quick note, PHP > 5.3.0 supports lambda functions … Read more

Show image using file_get_contents

You can use readfile and output the image headers which you can get from getimagesize like this: $remoteImage = “http://www.example.com/gifs/logo.gif”; $imginfo = getimagesize($remoteImage); header(“Content-type: {$imginfo[‘mime’]}”); readfile($remoteImage); The reason you should use readfile here is that it outputs the file directly to the output buffer where as file_get_contents will read the file into memory which is … 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’); }

PHP file_get_contents() returns “failed to open stream: HTTP request failed!”

Try using cURL. <?php $curl_handle=curl_init(); curl_setopt($curl_handle, CURLOPT_URL,’http://###.##.##.##/mp/get?mpsrc=http://mybucket.s3.amazonaws.com/11111.mpg&mpaction=convert format=flv’); curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2); curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl_handle, CURLOPT_USERAGENT, ‘Your application name’); $query = curl_exec($curl_handle); curl_close($curl_handle); ?>

PHP cURL vs file_get_contents

file_get_contents() is a simple screwdriver. Great for simple GET requests where the header, HTTP request method, timeout, cookiejar, redirects, and other important things do not matter. fopen() with a stream context or cURL with setopt are powerdrills with every bit and option you can think of.

Does file_get_contents() have a timeout setting?

The default timeout is defined by default_socket_timeout ini-setting, which is 60 seconds. You can also change it on the fly: ini_set(‘default_socket_timeout’, 900); // 900 Seconds = 15 Minutes Another way to set a timeout, would be to use stream_context_create to set the timeout as HTTP context options of the HTTP stream wrapper in use: $ctx … Read more

file_get_contents(): SSL operation failed with code 1, Failed to enable crypto

This was an enormously helpful link to find: http://php.net/manual/en/migration56.openssl.php An official document describing the changes made to open ssl in PHP 5.6 From here I learned of one more parameter I should have set to false: “verify_peer_name”=>false Note: This has very significant security implications. Disabling verification potentially permits a MITM attacker to use an invalid … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)