Thursday, October 4, 2012

cURL


 Curl is a reflective object-oriented programming language for interactive web applications whose goal is to provide a smoother transition between formatting and programming. It makes it possible to embed complex objects in simple documents without needing to switch between programming languages or development platforms.

3 Languages in 1 

One of the powers of the Curl language is that it combines the worlds of document markup, scripting, and object-oriented programming into a single language.

Language Feature: Description

Markup Language: Similar to HTML, it allows you to easily format text, tables, and lists

Scripting Language: Similar to JavaScript, it allows you to create interface elements such as buttons and drop-down menus

Programming Language: Similar to Java, it combines the flexibility of object-oriented programming



Curl was specifically designed to have a gentle slope learning curve. At one end of the curve is a simple markup language and at the highest end of the curve is a feature rich programming language in which you can build object-oriented applications with 3-D graphics and other advanced tools.

Starting at the beginning of the curve, the simplest element in an applet written in the Curl language is text. When you include text in a Curl applet, the browser displays that text that is displayed in the default font and point size. It treats spaces in the same manner as HTML does, by collapsing spaces that appear between words. In other words, if a number of consecutive spaces appear between words, the spaces are collapsed and only one space is displayed in the browser.

A sample "Hello World" implementation in Curl:

Basic curl example

Once you've compiled PHP with cURL support, you can begin using the cURL functions. The basic idea behind the cURL functions is that you initialize a cURL session using the curl_init(), then you can set all your options for the transfer via the curl_setopt(), then you can execute the session with the curl_exec() and then you finish off your session using the curl_close(). Here is an example that uses the cURL functions to fetch the example.com homepage into a file:

Using PHP's cURL module to fetch the example.com homepage



Amazing things to do with cURL

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.
  • Check if a specific website is available
    <?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;
           }
    ?>
  • Get latest twitter status
  • 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);
    }
  • Twitter: Test friendship between two users
    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"

Comments/Testimonials:
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!
- Max Rose-Collins 


References/Sources: 
JNSM  2010-51883

No comments:

Post a Comment