cURL

From DreamHost
Jump to: navigation, search


Overview

The cURL library (or, 'libcurl' which is the package name on the server) is often used in PHP to access data from outside web pages. Common uses include reading rss feeds or accessing third-party API systems. libcurl currently supports the http, https, ftp, gopher, telnet, dict, file, and ldap protocols. libcurl also supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading (this can also be done with PHP's ftp extension), HTTP form based upload, proxies, cookies, and user+password authentication.

Fetching a file without using cURL

Here's an example that doesn’t use cURL. In the following, the script reads and displays the post titles from the RSS feed xml file of dreamhoststatus.com:

<?php
$xml = simplexml_load_file('http://www.dreamhoststatus.com/feed/');
print "<ul>\n";
foreach ($xml->channel->item as $item){
  print "<li>$item->title</li>\n";
}
print "</ul>";
?>

Fetching a file using cURL

Here's another example, but this time cURL is used. Again, the script reads and displays the post titles from the RSS feed xml file of dreamhoststatus.com. A simplexml_load_string is also used to parse the results that are created when we run curl_exec():

<?php

$ch = curl_init("http://www.dreamhoststatus.com/feed/");

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);

$xml = simplexml_load_string(curl_exec($ch));
curl_close($ch);

print "<ul>\n";
foreach ($xml->channel->item as $item){
  print "<li>$item->title</li>\n";
}
print "</ul>";

?>

Saving the output using cURL

The following script accesses the same RSS feed using cURL but instead of displaying the feed, it saves it to a file named dreamhost_feed.html. This operation is especially useful if you’re building a cache file or don’t need real time updates:

<?php
$ch = curl_init("http://www.dreamhoststatus.com/feed/");
$fp = fopen("dreamhost_feed.html", "w");
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
?>

Images

This script retrieves a remote image and assigns the binary data to the variable $image, and then outputs the image to the browser:

<?php
$image_url = "http://example.com/image.jpg";
$ch = curl_init();
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, $image_url);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

// Getting binary data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);

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

// output to browser
header("Content-type: image/jpeg");
print $image;
?>

Advanced uses for cURL

As you can see from the previous examples, cURL allows for much more control that just calling simplexml_load_file. cURL can be used for accessing other file types and can pass various headers. View the following link for an example of using cURL to access the Twitter API. This code was written by stackoverflow.com user Rivers:

Command line examples

Instead of using cURL within a file, you can also run it directly via the command line on the server. This is often useful to test connections or file headers to verify if something is working as intended.

Testing a connection to a remote site

At some point, you may notice that your site not communicating with another website. An example may be when you're attempting to download a WordPress plugin and seeing it fail.

The following steps help you test your connection to WordPress:

  1. Log into your server via SSH.
  2. Run the following command within your domain's directory.
    curl -v https://wordpress.org/
    * About to connect() to wordpress.org port 443 (#0)
    *   Trying 66.155.40.250... connected
    * Connected to wordpress.org (66.155.40.250) port 443 (#0)
    * successfully set certificate verify locations:
    

This shows a successful connection. If it failed, you'd see the failure in the response instead.

Checking headers

You can also use cURL to check headers from a website, but you must specify the header you wish to view. For example, run the following to check if gzip is enabled:

 
curl -I -H 'Accept-Encoding: gzip,deflate' http://example.com/index.php

If gzip is enabled, you'll see this line in the output:

Content-Encoding: gzip

See Also