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.

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

PHP Parallel curl requests

If you mean multi-curl then, something like this might help: $nodes = array($url1, $url2, $url3); $node_count = count($nodes); $curl_arr = array(); $master = curl_multi_init(); for($i = 0; $i < $node_count; $i++) { $url =$nodes[$i]; $curl_arr[$i] = curl_init($url); curl_setopt($curl_arr[$i], CURLOPT_RETURNTRANSFER, true); curl_multi_add_handle($master, $curl_arr[$i]); } do { curl_multi_exec($master,$running); } while($running > 0); for($i = 0; $i < … Read more

How to get the content-type of a file in PHP?

I am using this function, which includes several fallbacks to compensate for older versions of PHP or simply bad results: function getFileMimeType($file) { if (function_exists(‘finfo_file’)) { $finfo = finfo_open(FILEINFO_MIME_TYPE); $type = finfo_file($finfo, $file); finfo_close($finfo); } else { require_once ‘upgradephp/ext/mime.php’; $type = mime_content_type($file); } if (!$type || in_array($type, array(‘application/octet-stream’, ‘text/plain’))) { $secondOpinion = exec(‘file -b –mime-type … Read more

file_get_contents() Breaks Up UTF-8 Characters

I had similar problem with polish language I tried: $fileEndEnd = mb_convert_encoding($fileEndEnd, ‘UTF-8’, mb_detect_encoding($fileEndEnd, ‘UTF-8’, true)); I tried: $fileEndEnd = utf8_encode ( $fileEndEnd ); I tried: $fileEndEnd = iconv( “UTF-8”, “UTF-8”, $fileEndEnd ); And then – $fileEndEnd = mb_convert_encoding($fileEndEnd, ‘HTML-ENTITIES’, “UTF-8”); This last worked perfectly !!!!!!