Using Curl except file_get_contents

Discussion in 'PHP' started by SNaRe, Feb 25, 2007.

  1. #1
    I have a script. It's using file_get_contents.
    I heard that curl is more efficient from file_get_contents
    what is the equivalent code for curl?
    $data = file_get_contents("http://www.xxx.com");

    I want this with curl .
    How can i do it ?
     
    SNaRe, Feb 25, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    
    <?php
    
    $url = 'http://www.xxx.com';
    
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    
    $data = curl_exec($ch);
    
    ?>
    
    PHP:
    The good thing about cURL is that you can set all possible options manually, incase the server you're requesting this from requires it.

    http://us2.php.net/curl
     
    nico_swd, Feb 25, 2007 IP
  3. SNaRe

    SNaRe Well-Known Member

    Messages:
    1,132
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    165
    #3
    Thank you
    I wanted this to use in my pagearank script. It uses file_get_contents
    I will try this now
     
    SNaRe, Feb 25, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    Be careful if you plan to make a public script. cURL isn't enabled by default and not all hosts support it. Do something like this:

    
    if (function_exists('curl_init'))
    {
      // Use cURL
    }
    else
    {
       // Use file_get_contents
    }
    
    PHP:
     
    nico_swd, Feb 25, 2007 IP
  5. SNaRe

    SNaRe Well-Known Member

    Messages:
    1,132
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    165
    #5
    thank you for more information .
    Now I tested it's working %100 .
    My script was making 100 job in 60 seconds . now it make it in 14 seconds.
    I advise it to everyone
     
    SNaRe, Feb 25, 2007 IP