Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

Download a URL’s Content Using PHP cURL

32 Responses »

Downloading content at a specific URL is common practice on the internet, especially due to increased usage of web services and APIs offered by Amazon, Alexa, Digg, etc. PHP's cURL library, which often comes with default shared hosting configurations, allows web developers to complete this task.

The Code

/* gets the data from a URL */
function get_data($url)
{
	$ch = curl_init();
	$timeout = 5;
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
	$data = curl_exec($ch);
	curl_close($ch);
	return $data;
}

The Usage

$returned_content = get_data('http://davidwalsh.name');

Alternatively, you can use the file_get_contents function remotely, but many hosts don't allow this.

Discussion

  1. December 11, 2007 @ 12:38 pm

    Alternatively you can use the PHP DOM:

    $keywords = array();
    $domain = array(‘http://davidwalsh.name’);
    $doc = new DOMDocument;
    $doc->preserveWhiteSpace = FALSE;
    foreach ($domain as $key => $value) {
    @$doc->loadHTMLFile($value);
    $anchor_tags = $doc->getElementsByTagName(‘a’);
    foreach ($anchor_tags as $tag) {
    $keywords[] = strtolower($tag->nodeValue);
    }
    }

    Keep in mind this is not a tested piece of code, I took parts from a working script I have created and cut out several of the checks I’ve put in to remove whitespace, duplicates, and more.

  2. December 11, 2007 @ 12:41 pm

    For your script we can also add a User Agent:
    $userAgent = ‘Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)’;
    curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);

    Some other options I use:
    curl_setopt($ch, CURLOPT_FAILONERROR, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);

  3. December 11, 2007 @ 1:46 pm

    Excellent additions Shawn — thank you for posting them!

  4. December 12, 2007 @ 9:47 am

    And with this great power, comes great responsibility =)

  5. December 12, 2007 @ 10:15 am

    Very true Chris. It’s up to the developer to use it for good or evil. I suppose I’ve used it for both in the past.

    For downloading remote XML or text files, this script has been golden.

  6. kp
    April 18, 2008 @ 5:16 am

    Great script! Does anyone know how to use that script to save the content it gathered and save it to a file locally on the server?

  7. April 18, 2008 @ 5:47 am

    @KP: Check out my other article, Basic PHP File Handling — Create, Open, Read, Write, Append, Close, and Delete, here:

    http://davidwalsh.name/basic-php-file-handling-create-open-read-write-append-close-delete

  8. usman
    May 15, 2008 @ 6:52 am

    I am trying to use this function “get_data($url)”, but it gives blank page when I echoed it. Anybody can please help me?

  9. May 15, 2008 @ 7:56 am

    @Usman: There are a few reason why you may get a blank page. You may not have CURL installed on the server. The other possibility is that you need to “echo” the content before you close the connection — someone brought this issue to me the other day.

  10. usman
    May 27, 2008 @ 12:06 am

    Hello David,
    I am still unable to get result of it, I have checked(using phpinfo()) that CURL is installed. But its giving blank page. When I tried it from php command line its working.

  11. dru
    September 1, 2008 @ 12:47 pm

    Works like a charm!

  12. November 14, 2008 @ 11:50 am

    Works just like…. file_get_contents! Thanks.

  13. ajay
    December 16, 2008 @ 8:09 am

    The code is very effective. but the problem is it returns all the html tags like and others. so is there anyway to get rid of it?

  14. bit
    February 17, 2009 @ 10:06 am

    this code is way too short, even php.net probably has a longer version! beware if you use this to enable other users to make the URL requests, they can easily use it to upload malicious code/whole new pages/huge files, like mp3s or movies, that will eat up all your bandwidth.

  15. September 18, 2009 @ 2:19 am

    Do you know of a way to have it click a link on a page. I’m trying to work with another companies registration form. Its a stupid asp page. On the first page it puts ?DoctorId=13074 at the end of the url. On the next page with the registration form it dynamically makes a random string in a hidden input box that gets posted with the form. So is there any way I can have it click and link once it loads a page?

  16. thomas alexander
    October 29, 2009 @ 7:34 am

    hi,
    I’m using curl to get details from an api call, in one of my api call it returns a zip file,
    i like to force download that zip file , how can i do this with curl

  17. joel kiskola
    November 25, 2009 @ 3:39 am

    David Walsh code does not give anything to me.
    Why?

    I did include php tags before and after both codes.

  18. November 30, 2009 @ 1:33 am

    @Joel – cause you have to add :
    echo $returned_content
    after the last line ($returned_content = get_data(‘http://davidwalsh.name’);)

  19. January 3, 2010 @ 4:14 am

    i want to onclick a link after getting contents of webpage
    how to do it?

  20. george
    January 19, 2010 @ 9:16 am

    I would like to remove the xml declaration from the returned url.

    I am appending the gathered data to an existing php/xml file and do not want it.

    is there a simple solution??

  21. fmdb
    February 7, 2010 @ 9:59 am

    hi!

    im trying to parse AJAX with PHP, problem is:

    when i read the URL SOURCE, the AJAX part isn’t visible, and i only grab HTML from the rest of site.

    how to solve this problem? any ideas?

  22. kelly
    February 14, 2010 @ 12:21 pm

    Is there a way to use curl in php like you can in the command line. aka

    curl http://mydomain.com/picture.jpg -o “PATH_TO_SAVE_TO”

    This would download a picture from a website and put it in a folder on my server. It works from Terminal but i cannot find the equivalent in PHP.

    If anyone nows the answer to this I would greatly Appreciate it.

  23. February 14, 2010 @ 12:22 pm

    Is there a way to use curl in php like you can in the command line. aka

    curl http://mydomain.com/picture.jpg -o “PATH_TO_SAVE_TO”

    This would download a picture from a website and put it in a folder on my server. It works from Terminal but i cannot find the equivalent in PHP.

    If anyone nows the answer to this I would greatly Appreciate it.

  24. February 14, 2010 @ 12:22 pm

    Is there a way to use curl in php like you can in the command line. aka

    curl http://mydomain.com/picture.jpg -o “PATH_TO_SAVE_TO”

    This would download a picture from a website and put it in a folder on my server. It works from Terminal but i cannot find the equivalent in PHP.

    If anyone nows the answer to this I would greatly Appreciate it.

  25. george
    February 14, 2010 @ 3:43 pm

    @Kelly: yes,

    place something like this in your php: 3 options,

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,
    “http://www.whateveryouwant.com.php.html.xml”);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $xml_language = curl_exec($ch);
    curl_close($ch);
    echo “$xml.php.html_whatever”;

    }

    you have options using curl:
    return the data in with database driven string. returns the data and appends it to your php, html,xml etc. VERY HANDY – esp. for flash and others, see: worldwideweather.com – forum,

    this trick allows flash too read an external xml file for its language and database info. using php to call the userspecif info you can write the flash xml on the fly – this script returns the users languge interface for flash, php calls the xml – the user is spanish (language) ES and appending to the php xml call, the the file is read and writes this into the php script itself with , very very fast

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,
    “http://www.verdegia.com/Files/System/TEST/Language/M_TEXT_” . $line{“Language”} . “.xml”);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $xml_language = curl_exec($ch);
    curl_close($ch);
    echo “$xml_language”;

    }
    return the data in external xml file from php user specific database call ” string – gets data specif for user and generates file on the fly , xml, php, html whatever..:

    $sql=”SELECT * FROM $tbl_name WHERE username=’$myusername’”;
    $results = mysql_query($sql);
    while($line=mysql_fetch_assoc($results)) {
    $file = “http://www.worldweatheronline.com/feed/weather.ashx?q=” . $line{“Postcode”} . “&format=xml&num_of_days=5&key=6c7e92e827155910100801″;
    }

    $ch = curl_init($file);
    $fp = @fopen(“../Files/System/TEST/temp.xml”, “w”);
    curl_setopt($ch, CURLOPT_FILE, $fp);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_exec($ch);
    curl_close($ch);
    fclose($fp);
    $file = “../Files/System/TEST/temp.xml”;
    $fp = fopen($file, “r”);
    ?>

    HOPE THIS HELPS

  26. April 23, 2010 @ 1:19 pm

    Can we use this function to parse all content in a url?

  27. May 20, 2010 @ 10:03 pm

    @Indonesia: Except there are a lot more options. I (believe) that it’s possible to get the whole HTTP response using CURL, and (believe) that that is not true with ‘file_get_contents())

  28. May 29, 2010 @ 4:21 pm

    $sql = “UPDATE staff SET
    staffNo = $staff_no,
    f_name=$fname,
    l_name=$lname,
    sex=$sex,
    DOB=$dob,
    position=$position,
    salary=$salary,
    hiredate=$hiredate,
    contact_id=$contact_id,
    branchNo=$branch_no
    WHERE staffNo=$staff_no”;
    $query = mysql_query($sql) or die(“Cannot query the database.” . mysql_error());
    echo “Database Updated.”;

  29. June 4, 2010 @ 3:45 am

    Great,

    It is usefull to get xml or images from other site. if server is not able to get content from fopen.

    Thanks

  30. June 4, 2010 @ 3:46 am

    Great,

    It is useful to get xml or images from other site. if server is not able to get content from fopen.

    Thanks

  31. July 5, 2010 @ 7:07 am

    Nice,
    PHP provide other two method to fetch an URL – Curl and Fsockopen.
    To use they you can check this example : http://www.bin-co.com/php/scripts/load/

  32. August 16, 2010 @ 2:04 am

    how to write return into new file?

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!