Amazing things to do with PHP and cURL

by Jean. 22 Comments -

cURL, and its PHP extension libcURL, are very useful tools for tasks like simulating a web browser, submit forms or login to a web service. In this article, I’m going to show you some amazing things that you can do using PHP and cURL.

Check if a specific website is available

Want to know if a specific website is available? cURL is here to help. This script can be used with a cron job to monitor your websites.

Don’t forget to update the script with the url you want to check on line 3. Once done, just paste it somewhere and it will let you know about the site availability.

<?php

       if (isDomainAvailible('http://www.css-tricks.com'))
       {
               echo "Up and running!";
       }
       else
       {
               echo "Woops, nothing found there.";
       }

       //returns true, if domain is availible, false if not
       function isDomainAvailible($domain)
       {
               //check, if a valid url is provided
               if(!filter_var($domain, FILTER_VALIDATE_URL))
               {
                       return false;
               }

               //initialize curl
               $curlInit = curl_init($domain);
               curl_setopt($curlInit,CURLOPT_CONNECTTIMEOUT,10);
               curl_setopt($curlInit,CURLOPT_HEADER,true);
               curl_setopt($curlInit,CURLOPT_NOBODY,true);
               curl_setopt($curlInit,CURLOPT_RETURNTRANSFER,true);

               //get answer
               $response = curl_exec($curlInit);

               curl_close($curlInit);

               if ($response) return true;

               return false;
       }
?>

→ Source: http://css-tricks.com/snippets/php/check-if-website-is-available/

cURL replacement for file_get_contents()

The file_get_contents() function is very useful but it is unfortunely deactivated by default on some webhosts. Using cURL, we can write a replacement function that works exactly like file_get_contents().

function file_get_contents_curl($url) {
	$ch = curl_init();
	
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Set curl to return the data instead of printing it to the browser.
	curl_setopt($ch, CURLOPT_URL, $url);
	
	$data = curl_exec($ch);
	curl_close($ch);
	
	return $data;
}

→ Source: http://snipplr.com/view/4084

Get latest Twitter status

Using PHP and cURL, it is pretty easy to get the status of a specific user. Once you have it, what about displaying it on your blog, like I do in WPRecipes footer?

function get_status($twitter_id, $hyperlinks = true) {
    $c = curl_init();
    curl_setopt($c, CURLOPT_URL, "http://twitter.com/statuses/user_timeline/$twitter_id.xml?count=1");
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
    $src = curl_exec($c);
    curl_close($c);
    preg_match('/<text>(.*)<\/text>/', $src, $m);
    $status = htmlentities($m[1]);
    if( $hyperlinks ) $status = ereg_replace("[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]", '<a href="%5C%22%5C%5C0%5C%22">\\0</a>', $status);
    return($status);
}

The function is super easy to use:

echo get_status('catswhocode');

→ Source: http://www.catswhocode.com/blog/php-snippets-to-interact-with-twitter

Twitter: test friendship between two users

If you want to know if a specific user is following you (or someone else) you have to use the Twitter API. This snippet will echo true if the two users specified on lines 18 and 19 are friends. It will return false otherwise.

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

/* gets the match */
function get_match($regex,$content) {
	preg_match($regex,$content,$matches);
	return $matches[1];
}

/* persons to test */
$person1 = 'phpsnippets';
$person2 = 'catswhocode';

/* send request to twitter */
$url = 'https://api.twitter.com/1/friendships/exist';
$format = 'xml';

/* check */
$persons12 = make_request($url.'.'.$format.'?user_a='.$person1.'&user_b='.$person2);
$result = get_match('/<friends>(.*)<\/friends>/isU',$persons12);
echo $result; // returns "true" or "false"

→ Source: http://www.catswhocode.com/blog/php-snippets-to-interact-with-twitter

Download and save images from a page using cURL

Here is a set of functions that can be very useful: Give this script the url of a webpage, and it will save all images from this page on your server.

function getImages($html) {
    $matches = array();
    $regex = '~http://somedomain.com/images/(.*?)\.jpg~i';
    preg_match_all($regex, $html, $matches);
    foreach ($matches[1] as $img) {
        saveImg($img);
    }
}

function saveImg($name) {
    $url = 'http://somedomain.com/images/'.$name.'.jpg';
    $data = get_data($url);
    file_put_contents('photos/'.$name.'.jpg', $data);
}

$i = 1;
$l = 101;

while ($i < $l) {
    $html = get_data('http://somedomain.com/id/'.$i.'/');
    getImages($html);
    $i += 1;
}

→ Source: http://stackoverflow.com/questions/7747875/grab-download-images-from-multiple-pages-using-php-preg-match-all-curl

Convert currencies using cURl and Google

Converting currencies isn’t very hard to do, but as the currencies fluctuates all the time, we definitely need to use a service like Google to get the most recent values. The currency() function take 3 parameters: from, to, and sum.

function currency($from_Currency,$to_Currency,$amount) {
    $amount = urlencode($amount);
    $from_Currency = urlencode($from_Currency);
    $to_Currency = urlencode($to_Currency);
    $url = "http://www.google.com/ig/calculator?hl=en&q=$amount$from_Currency=?$to_Currency";
    $ch = curl_init();
    $timeout = 0;
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch,  CURLOPT_USERAGENT , "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    $rawdata = curl_exec($ch);
    curl_close($ch);
    $data = explode('"', $rawdata);
    $data = explode(' ', $data['3']);
    $var = $data['0'];
    return round($var,2);
}

→ Source: http://l33ts.org/forum/Thread-PHP-Convert-currencies-using-Google-and-cURL-Snippet

Get remote filesize using cURL

Want to be able to calculate the size of a specific file? The following function can help. It takes 3 parameters: the file url, and in case the file is password protected, a username and a password.

function remote_filesize($url, $user = "", $pw = ""){
    ob_start();
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_NOBODY, 1);
 
    if(!empty($user) && !empty($pw))
    {
        $headers = array('Authorization: Basic ' .  base64_encode("$user:$pw"));
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    }
 
    $ok = curl_exec($ch);
    curl_close($ch);
    $head = ob_get_contents();
    ob_end_clean();
 
    $regex = '/Content-Length:\s([0-9].+?)\s/';
    $count = preg_match($regex, $head, $matches);
 
    return isset($matches[1]) ? $matches[1] : "unknown";
}

→ Source: http://megasnippets.com/source-codes/php/get_remote_filesize

FTP upload with cURL

PHP does have a FTP library, but you can also use cURL to upload files on a FTP server. Here is a working example:

// open a file pointer
$file = fopen("/path/to/file", "r");

// the url contains most of the info needed
$url = "ftp://username:password@mydomain.com:21/path/to/new/file";

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// upload related options
curl_setopt($ch, CURLOPT_UPLOAD, 1);
curl_setopt($ch, CURLOPT_INFILE, $fp);
curl_setopt($ch, CURLOPT_INFILESIZE, filesize("/path/to/file"));

// set for ASCII mode (e.g. text files)
curl_setopt($ch, CURLOPT_FTPASCII, 1);

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

→ Source: http://net.tutsplus.com/tutorials/php/techniques-and-resources-for-mastering-curl/

Comments (22) - Leave yours

  1. Shane said:

    Get a list of amazing things to do with curl with curl:

    function get_list_of_amazing_things() {
    $c = curl_init();
    curl_setopt($c, CURLOPT_URL, “http://www.catswhocode.com/blog/amazing-things-to-do-with-php-and-curl”);
    curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
    $src = curl_exec($c);
    curl_close($c);
    return $src;
    }

  2. Jesús Ortega said:

    Do yo think I could use cURL to get the data of the public objects search in Facebook? I mean what they talk about in the “Searching” section of this page: https://developers.facebook.com/docs/reference/api/
    For example, I want to fetch the data you can see with this: https://graph.facebook.com/search?q=watermelon&type=post

  3. Max Rose-Collins said:

    cURL is SO powerful.

    The amount of times I have used it to grab a Facebook feed or Twitter feed for a website I am developing is unbelievable.
    Facebook is a little bit more tricky than Twitter though. You have to set up a Facebook app and get a secret key. Can be a pain!

    I didn’t know you could FTP upload with it though, you learn something new every day, thanks :)

  4. Darius said:

    I don’t know if I’m missing something but in the section “Download and save images from a page using cURL” shouldnt there be a function get_data.

    • Dizzi said:

      You are correct, Jean-Baptiste forgot to grab the actual cURL function from source page question of which I asked ;)

  5. Mark said:

    It would be great if you can evaluate more on next post on cURL replacement for file_get_contents and finding specific word and then trigger, say an email. Very useful if a site is down as a sort of mini monitoring on a different host.

  6. Chris said:

    Excellent article, thanks!

    Found a bug I thought I’d report. When fetching Google currency rates. I the google returns a number higher than 999, the returned value has a thousand separator. This means when converting a currency that returns a calculated conversion of 1234, Google returns “1 234.00″. Your little code snippet now returns “1″ instead of “1234″.

    There is a simple fix. Insert the following line of code
    (…)
    $data = explode(‘ ‘, $data['3']);
    -> $data = preg_replace(‘/[^(\x20-\x7F)]*/’,”, $data);
    $var = $data['0'];
    (…)

  7. ubaid said:

    I don’t know what file_get_contents() is, because CURL rocks. Thanks for sharing this article, specially “Twitter: test friendship between two users” is the best code snippet.

  8. Mittul Chauhan said:

    i always prefer cURL over file_get_contents or anywhere .. its THE MOST AMAZING library of php .. its php’s one of plus points ..

  9. Farhan said:

    Twitter: test friendship between two users. This code snippet is really really awesome.

    I tried that and I was amazed with the end results. Long live PHP.

    Thanks for sharing these snippets.

Leave a Reply

Your email address will not be published. Required fields are marked *

Please respect the following rules: No advertising, no spam, no keyword in name field. Thank you!