Oct 13th

0

cURL wrapper class with executable and PHP extension support

PHPcURL

Most cURL wrapper classes I found could handle either the PHP extension or the command line executable version of cURL. I needed a class where I could set one variable and have it switch from using the PHP cURL extension to the cURL executable without having to change the other variable of the class.

The class I came up with is below:

001 class Curl {
002  
003     // Set to "PHP" or the path to the curl executable (binary)
004     var $curlExecutable = "PHP";
005  
006     // URL to request, request type (GET or POST), request parameters as an associative array or null for none
007     var $url;
008     var $requestType = "GET";
009     var $parameters = null;
010  
011     // request headers as an array
012     var $requestHeaders = array();
013  
014     // certification authority, return data with headers, follow redirects and verify peer if URL is https
015     var $cainfo = null;
016     var $outputWithHeaders = false;
017     var $followLocation = false;
018     var $SSLVerifyPeer = true;
019  
020     // connect to remote server timeout and timeout to download the entire page
021     var $connectTimeout = 30;
022     var $timeout = 10800;
023  
024     // if there was an error, errorNumber and error is populated. data contains the page downloaded
025     var $error = null;
026     var $errorNumber = null;
027     var $data = null;
028  
029     function fetchPage() {
030  
031         if ($this->curlExecutable == "PHP") {
032             $ch = curl_init();
033  
034             if ($this->cainfo) {
035                 curl_setopt($ch, CURLOPT_CAINFO, $this->cainfo);
036             }
037  
038             if ($this->SSLVerifyPeer) {
039                 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
040             } else {
041                 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
042             }
043  
044             if ($this->requestType === "POST") {
045                 curl_setopt($ch, CURLOPT_POST, 1);
046  
047                 if ($this->parameters) {
048                     if (is_array($this->parameters)) {
049  
050                         $encodedParameters = "";
051                         $join = "";
052  
053                         foreach ($this->parameters as $name => $value) {
054                             $encodedParameters = $encodedParameters . $join . rawurlencode($name) . "=" . rawurlencode($value);
055                             $join = "&";
056                         }
057  
058                         curl_setopt($ch, CURLOPT_POSTFIELDS, $encodedParameters);
059  
060                     } elseif (is_string($this->parameters)) {
061  
062                         curl_setopt($ch, CURLOPT_POSTFIELDS, $this->parameters);
063  
064                     }
065                 }
066  
067             } else {
068                 curl_setopt($ch, CURLOPT_POST, 0);
069  
070                 if ($this->parameters) {
071                     $encodedParameters = "";
072                     $join = "?";
073  
074                     foreach ($this->parameters as $name => $value) {
075                         $encodedParameters = $encodedParameters . $join . rawurlencode($name) . "=" . rawurlencode($value);
076                         $join = "&";
077                     }
078  
079                     $this->url = $this->url . $encodedParameters;
080                 }
081  
082             }
083  
084             curl_setopt($ch, CURLOPT_HTTPHEADER, $this->requestHeaders);
085  
086             if ($this->outputWithHeaders) {
087                 curl_setopt($ch, CURLOPT_HEADER, 1);
088             } else {
089                 curl_setopt($ch, CURLOPT_HEADER, 0);
090             }
091  
092             if ($this->followLocation) {
093                 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
094             } else {
095                 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
096             }
097  
098             curl_setopt($ch, CURLOPT_URL, $this->url);
099             curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
100  
101             curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->connectTimeout);
102             curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
103  
104             $this->data = curl_exec($ch);
105             $this->error = curl_error($ch);
106             $this->errorNumber = curl_errno($ch);
107             curl_close($ch);
108  
109             if ($this->error) {
110                 return false;
111             } else {
112                 return true;
113             }
114  
115         } else {
116  
117             $command = $this->executable;
118  
119             $command = $command . " -s -S";
120  
121             $command = $command . " --connect-timeout " . $this->connectTimeout;
122             $command = $command . " --max-time " . $this->timeout;
123  
124             if ($this->cainfo) {
125                 $command = $command . " --cacert "" . $this->cainfo . """;
126             }
127  
128             if ($this->outputWithHeaders) {
129                 $command = $command . " -i";
130             }
131  
132             if ($this->followLocation) {
133                 $command = $command . " -L";
134             }
135  
136             for ($i = 0; $i < count($this->requestHeaders); $i++) {
137                 $command = $command . " -H "" . $this->requestHeaders[$i] . """;
138             }
139  
140             if ($this->parameters){
141                 $encodedParameters = "";
142                 $join = "";
143  
144                 foreach ($this->parameters as $name => $value) {
145                     $encodedParameters = $encodedParameters . $join . rawurlencode($name) . "=" . rawurlencode($value);
146                     $join = "&";
147                 }
148  
149                 $command = $command . " -d " . escapeshellarg($encodedParameters) . "";
150             }
151  
152             if ($this->requestType === "GET") {
153                 $command = $command . " -G";
154             }
155  
156             $command = $command . " " . $this->url;
157  
158             $descriptorspec = array(
159             0 => array("pipe", "r"),
160             1 => array("pipe", "w"),
161             2 => array("pipe", "w")
162             );
163  
164             $process = proc_open($command, $descriptorspec, $pipes);
165  
166             if (is_resource($process)) {
167  
168                 fclose($pipes[0]);    // close stdin as we are not going to supply any input
169  
170                 $this->data = stream_get_contents($pipes[1]);    // read stdout
171                 $this->error = stream_get_contents($pipes[2]);    // read stderr
172  
173                 fclose($pipes[1]);
174                 fclose($pipes[2]);
175  
176                 $this->errorNumber = proc_close($process);
177  
178             } else {
179  
180                 return false;
181  
182             }
183  
184             if ($this->errorNumber === 0) {
185                 return true;
186             } else {
187                 return false;
188             }
189  
190         }
191  
192     }
193  
194 }

The following example fetches http://www.google.com :

01 $request = new Curl;
02 $request->curlExecutable = "PHP";
03 $request->followLocation = true;
04 $request->url = "http://www.google.com";
05 if ( $request->fetchPage() ) {
06     header("Content-Type:text/plain");
07     echo $request->data;
08 } else {
09     echo "An error occured: " . $request->errorNumber . " - " . $request->error;
10 }

This example fetches a random quote (the ones tou see on the top right of this website):

01 $request = new Curl;
02 $request->curlExecutable = "PHP";
03 $request->url = "http://www.ankur.com/quotes.php";
04 $request->requestType = "GET";
05 $request->parameters = array(
06     "action" => "random"
07 );
08 if ( $request->fetchPage() ) {
09     header("Content-Type:text/plain");
10     echo $request->data;
11 } else {
12     echo "An error occured: " . $request->errorNumber . " - " . $request->error;
13 }

Unrelated to the cURL wrapper class, you can try changing the action parameter from random to all to fetch all the quotes.

Related posts:

  1. AES wrapper class with pure php and MCrypt extension support
  2. Resume HTTP downloads in PHP using cURL or fsockopen