Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

both file_get_contents and curl not working

I just used file_get_contents to retrive data in my website. But it does not returns anything.

file_get_contents("https://www.google.co.in/images/srpr/logo11w.png")

When i used the same in my local environment, It returns value.

I also tried curl in my website to figure out whether it returns anything. But it shows

Forbidden
You don't have permission to access.

I googled and i found some hints. I enabled allow_url_fopen and allow_url_include.

But nothing working.

The curl code which i have tried

function curl($url){ 
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL,$url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
  $return = curl_exec($ch); curl_close ($ch);
  return $return;
} 
$string = curl('https://www.google.co.in/images/srpr/logo11w.png');
echo $string;
like image 983
vinothavn Avatar asked Jan 02 '14 07:01

vinothavn


People also ask

Is cURL faster than File_get_contents?

cURL is capable of much more than file_get_contents . That should be enough. FWIW there's little difference with regards to speed. I've just finished fetching 5,000 URLs and saving their HTML to files (about 200k per file).

Does File_get_contents cache?

Short answer: No. file_get_contents is basically just a shortcut for fopen, fread, fclose etc - so I imagine opening a file pointer and freading it isn't cached.


1 Answers

The file_get_contents() way...

<?php
header('Content-Type: image/png');
echo file_get_contents("https://www.google.co.in/images/srpr/logo11w.png");

The cURL way...

<?php
header('Content-Type: image/png');
function curl($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,0);
    $return = curl_exec($ch); curl_close ($ch);
    return $return;
}
$string = curl('https://www.google.co.in/images/srpr/logo11w.png');
echo $string;

OUTPUT :

enter image description here

like image 105
Shankar Narayana Damodaran Avatar answered Sep 22 '22 07:09

Shankar Narayana Damodaran