How to use CURL instead of file_get_contents?

86,731

Solution 1

try this:

function file_get_contents_curl($url) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);       

    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}

Solution 2

This should work

function curl_load($url){
    curl_setopt($ch=curl_init(), CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $response = curl_exec($ch);
    curl_close($ch);
    return $response;
}

$url = "http://www.google.com";
echo curl_load($url);

Solution 3

I encountered such a problem accessing Google Drive content via the direct link.

After calling file_get_contents returned 302 Moved temporarily

//Any google url. This example is fake for Google Drive direct link.
$url = "https://drive.google.com/uc?id=0BxQKKJYjuNElbFBNUlBndmVHHAj";
$html = file_get_contents($url);
echo $html; //print none because error 302.

With the code below it worked again:

//Any google url. This example is fake for Google Drive direct link.
$url = "https://drive.google.com/uc?id=0BxQKKJYjuNElbFBNUlBndmVHHAj";

$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);  
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 3);     
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$html = curl_exec($ch);
curl_close($ch);

echo $html;

I tested it today, 03/19/2018

Solution 4

//You can try this . It should work fine.

function curl_tt($url){

$ch = curl_init();

curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);  
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 3);     
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
$data = curl_exec($ch);
curl_close($ch);

return $data;
}
echo curl_tt("https://google.com");
Share:
86,731
Morteza
Author by

Morteza

There isn't any thing about me!

Updated on July 19, 2020

Comments

  • Morteza
    Morteza almost 4 years

    I use file_get_contents function to get and show external links on my specific page.

    In my local file everything is okay, but my server doesn't support the file_get_contents function, so I tried to use cURL with the below code:

    function file_get_contents_curl($url) {
        $ch = curl_init();
    
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
    
        $data = curl_exec($ch);
        curl_close($ch);
    
        return $data;
    }
    
     echo file_get_contents_curl('http://google.com');
    

    But it returns a blank page. What is wrong?

  • Casper André Casse
    Casper André Casse over 10 years
    This code will not behave exactly like file_get_contents. Your code will not follow redirects, file_get_contents does that.
  • 151291
    151291 over 7 years
    Curl returns empty Content.. how can i avoid this?
  • Bing
    Bing about 6 years
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); and curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); got my tripped up for a long time, too. Great catch!
  • Bhavin Visariya
    Bhavin Visariya almost 4 years
    @151291 adding below configuration worked for me : curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);