Monday, August 19, 2013

Overview of PHP And Curl



Curl is a utility that can be used to do network-related tasks. It stands for Client URL.  Curl can be used with any programming language and work with a variety of protocols, including HTTP, HTTPS, FTP, FTPS, TELNET and FILE. You can use it to access Websites, Upload files on FTP server,create proxy application, Content scrapers ,Link Checkers,search Engines ,and so on .PHP also supports libcurl, a cURL library that allows you to develop php application.

For some reason or another , you might want to fetch a webpage content and parse the HTML returned to get only certain values from a certain website .Of course ,you might tell yourself there are other ways to to fetch a web page with PHP like :


  1. $content  =  readfile("http://test.com");
  2. // or
  3. $content = file_get_contents("http://test.com");
  4. // or
  5. $content = file("http://test.com");



    $urltopost :The url where you want to post your data to
    $datatopost:The post data as an associative array. The keys are the post variables
   $ch = curl_init ($urltopost):Initializes cURL
   curl_setopt ($ch, CURLOPT_POST, true):Tells cURL that we want to send post data
   curl_setopt ($ch, CURLOPT_POSTFIELDS, $datatopost):Tells cURL what are post data is
   curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true):
          Tells cURL to return the output of the post
   $returndata = curl_exec ($ch):Executes the cURL and saves theoutput in $returndata

Example 1 : Downloading A Web Page using Php/Curl:

Below is a Simple example how to fetch webpage using curl: 



    //Start Curl Transaction

   $ch = curl_init();

   //The url that needs to be fetched

   curl_setopt($ch, CURLOPT_URL, "http://test.com/");

   //Return the content of the transaction

   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

   //Exucute The transaction

   $output = curl_exec($ch);

   echo $output;

   curl_close($ch); //Close the Transaction



Example 2 : Login to a website with Apache HTTP Authentication:

You might want to access a protected page which has Apache HTTP Authentication . Below is the code how you can achieve this with curl .


   // HTTP authentication

   $url = "http://test.com/";

   $ch = curl_init();

   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

   curl_setopt($ch, CURLOPT_URL, $url);

   //Specify The username and Password for the protected Page

   curl_setopt($ch, CURLOPT_USERPWD, "admin:admin");

   $result = curl_exec($ch);

   curl_close($ch);

   echo $result;//Output of page will be displayed here

Example 3 : Fetch a webpage by using proxies:



   $ch = curl_init();

   curl_setopt($ch, CURLOPT_URL, 'http://test.com/');

   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

   //Tunnel through a given HTTP proxy.

   curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);

   //The HTTP proxy to tunnel requests through,It should be in format IP:Port

   curl_setopt($ch, CURLOPT_PROXY, 'IP_Address:Port');

   //Specify Username and Password to connect through the proxy

   curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'Proxy_username:Proxy_Password');

   $result   = curl_exec($ch);//Execute The Transaction

   echo $result;

  curl_close($ch);

Example 4 : Send post data using Array:

Also we can send post data using an array:


   $urltopost = "http://somewebsite.com/script.php";
   $datatopost = array (
      "firstname" => "Mike",
      "lastname" => "Lopez",
      "email" => "my@email.com",
      );

   $ch = curl_init ($urltopost);
   curl_setopt ($ch, CURLOPT_POST, true);
   curl_setopt ($ch, CURLOPT_POSTFIELDS, $datatopost);
   curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
   $returndata = curl_exec ($ch);


No comments:

Post a Comment