Posted By

iloveitaly on 11/07/07


Tagged

curl php filegetcontents


Versions (?)

Who likes this?

14 people have marked this snippet as a favorite

vali29
heinz1959
agraddy
jaytee
jimmysessions
fruehjahr
nb109
basementjack
mfgalex
tomasdev
warren
jordilopez
wirenaught
gudeidea


CURL replacement for file_get_contents


 / Published in: PHP
 

  1. function file_get_contents_curl($url) {
  2. $ch = curl_init();
  3.  
  4. curl_setopt($ch, CURLOPT_HEADER, 0);
  5. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
  6. curl_setopt($ch, CURLOPT_URL, $url);
  7.  
  8. $data = curl_exec($ch);
  9. curl_close($ch);
  10.  
  11. return $data;
  12. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: saviola on July 5, 2010

PHP provide other two method to fetch an URL - Curl and Fsockopen. http://www.bin-co.com/php/scripts/load/

Posted By: hisamu on July 20, 2010

TY :)

Posted By: akshay_raje on March 27, 2011

A slight evolved version with some defaults and basic error handling:

function filegetcontentscurl($url, $curlopt = array()){ $ch = curlinit(); $defaultcurlopt = array( CURLOPTTIMEOUT => 2, CURLOPTRETURNTRANSFER => 1, CURLOPTFOLLOWLOCATION => 1, CURLOPTUSERAGENT => "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.13) Gecko/20101203 AlexaToolbar/alxf-1.54 Firefox/3.6.13 GTB7.1" ); $curlopt = array(CURLOPTURL => $url) + $curlopt + $defaultcurlopt; curlsetoptarray($ch, $curlopt); $response = curlexec($ch); if($response === false) triggererror(curlerror($ch)); curl_close($ch); return $response; }

Posted By: akshay_raje on March 27, 2011

Oops, didn't know cant post code! Here's the link - http://snipplr.com/view/51161/basic-curl-wrapper-function-for-php/

You need to login to post a comment.