PHP School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
 

Code Snippets

  

PHP Source Code



CURL Class

A simple class that allows you to POST/GET via the php cURL library. Includes cookie file setup.

Submitted By: Vanity
Actions:
Rating:
Views: 6,232

Language: PHP

Last Modified: October 21, 2006
Instructions: Example:
$curl = new cURL(FALSE);
echo $curl->get('http://wwww.google.com');

Snippet


  1. <?
  2. class cURL {
  3.   /*
  4.   * @author Keith Kurson (delusions@gmail.com)
  5.   * @date September 09, 2006
  6.   * @version 1.0
  7.   */
  8.      /*
  9.      * Headers
  10.      */
  11.      var $headers;
  12.      /*
  13.      * User Agent
  14.      */
  15.      var $user_agent;
  16.      /*
  17.      * Compression
  18.      */
  19.      var $compression;
  20.      /*
  21.      * Cookie File
  22.      */
  23.      var $cookie_file;
  24.      /*
  25.      * Proxy Server
  26.      * ip:port
  27.      */
  28.      var $proxy;
  29.      /*
  30.      * Initiate the class
  31.      */
  32.      function cURL($cookies=TRUE,$cookie='cookies.txt',$compression='gzip',$proxy='') {
  33.            $this->headers[] = "Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg";
  34.            $this->headers[] = "Connection: Keep-Alive";
  35.            $this->headers[] = "Content-type: application/x-www-form-urlencoded";
  36.            $this->user_agent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; Media Center PC 4.0)";
  37.            $this->compression=$compression;
  38.            $this->proxy=$proxy;
  39.            $this->cookies=$cookies;
  40.            if ($this->cookies == TRUE) $this->cookie($cookie);
  41.      }
  42.      /*
  43.      * Tests the Cookie File
  44.      */
  45.      function cookie($cookie_file) {
  46.           if (file_exists($cookie_file)) {
  47.                 $this->cookie_file=$cookie_file;
  48.           } else {
  49.                 @fopen($cookie_file,'w') or $this->error("The cookie file could not be opened. Make sure this directory has the correct permissions");
  50.                 $this->cookie_file=$cookie_file;
  51.                 fclose($cookie_file);
  52.           }
  53.      }
  54.      /*
  55.      * Runs a GET through cURL
  56.      */
  57.      function get($url,$refer='') {
  58.           $process = curl_init($url);
  59.           curl_setopt($process, CURLOPT_REFERER, $refer);
  60.           curl_setopt($process, CURLOPT_HTTPHEADER, $this->headers);
  61.           curl_setopt($process, CURLOPT_USERAGENT, $this->user_agent);
  62.           if ($this->cookies == TRUE) curl_setopt($process, CURLOPT_COOKIEFILE, $this->cookie_file);
  63.           if ($this->cookies == TRUE) curl_setopt($process, CURLOPT_COOKIEJAR, $this->cookie_file);
  64.           curl_setopt($process,CURLOPT_ENCODING , $this->compression);
  65.           curl_setopt($process, CURLOPT_TIMEOUT, 30);
  66.           if ($this->proxy) curl_setopt($cUrl, CURLOPT_PROXY, 'proxy_ip:proxy_port');
  67.           curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
  68.           $return = curl_exec($process);
  69.           curl_close($process);
  70.           return $return;
  71.      }
  72.      /*
  73.      * Runs a POST through cURL
  74.      */
  75.      function post($url,$data,$refer) {
  76.           $process = curl_init($url);
  77.           curl_setopt($process, CURLOPT_REFERER, $refer);
  78.           curl_setopt($process, CURLOPT_HTTPHEADER, $this->headers);
  79.           curl_setopt($process, CURLOPT_USERAGENT, $this->user_agent);
  80.           if ($this->cookies == TRUE) curl_setopt($process, CURLOPT_COOKIEFILE, $this->cookie_file);
  81.           if ($this->cookies == TRUE) curl_setopt($process, CURLOPT_COOKIEJAR, $this->cookie_file);
  82.           curl_setopt($process, CURLOPT_ENCODING , $this->compression);
  83.           curl_setopt($process, CURLOPT_TIMEOUT, 30);
  84.           if ($this->proxy) curl_setopt($cUrl, CURLOPT_PROXY, 'proxy_ip:proxy_port');
  85.           curl_setopt($process, CURLOPT_POSTFIELDS, $data);
  86.           curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
  87.           curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
  88.           curl_setopt($process, CURLOPT_POST, 1);
  89.           $return = curl_exec($process);
  90.           curl_close($process);
  91.           return $return;
  92.      }
  93.      /*
  94.      * Error Output
  95.      */
  96.      function error($error) {
  97.           echo "<center><div style='width:500px;border: 3px solid #FFEEFF; padding: 3px; background-color: #FFDDFF;font-family: verdana; font-size: 10px'><b>cURL Error</b><br>$error</div></center>";
  98.           die;
  99.      }
  100. }
  101.  
  102.  
  103. ?>

Copy & Paste


Comments

There are currently no comments for this snippet. Be the first to comment!

Add comment


You must be registered and logged on to </dream.in.code> to leave comments.