Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

POST support and other improvements

  • Loading branch information...
commit c258ef5d0f05f0122f9f598a29e075597404e6da 1 parent 81f22d8
@christianhellsten authored
Showing with 170 additions and 27 deletions.
  1. +0 −1  README
  2. +29 −0 README.textile
  3. +116 −26 http.php
  4. +25 −0 index.html
View
1  README
@@ -1 +0,0 @@
-A minimalist HTTP client for PHP.
View
29 README.textile
@@ -0,0 +1,29 @@
+A minimalist HTTP client for PHP.
+
+h2. GET
+
+<pre>
+ <code>
+$options = array (
+ 'debug_log' => '/tmp/http.log',
+ 'follow_redirects' => false,
+ 'connect_timeout' => 30000,
+ 'cookie_jar' => '/app/tmp/cookies.txt',
+ 'proxy' => 'http-proxy:8080'
+)
+$headers = array('User-Agent' => 'Googlebot/2.1 ( http://www.googlebot.com/bot.html)');
+$response = HTTP::get('http://google.com', $headers, $options);
+ </pre>
+</code>
+
+h2. POST
+
+<pre>
+ <code>
+$parameters = array(
+ 'name' => 'Data',
+ 'file' => "@$file"#(file_get_contents('/tmp/data.txt')) # File upload
+);
+$response = HTTP::post('http://my-files/upload', $parameters)
+ </pre>
+</code>
View
142 http.php
@@ -1,50 +1,140 @@
+<?php
class Response {
public function Response($code, $body, $headers, $info=null) {
+ #$this->code = $info['http_code'];
$this->code = $code;
$this->body = $body;
$this->headers = $headers;
$this->info = $info;
+ $this->url = $info['url'];
}
}
class HTTP {
- static function get($url, $headers=null) {
+ /**
+ * HTTP GET
+ *
+ * @param string $url
+ * @param array $headers: HTTP headers
+ * @param array $options: CURL options
+ */
+ static function get($url, $headers=null, $options=null) {
+ return self::execute($url, null, $headers, $options);
+ }
+
+ /**
+ * HTTP POST
+ *
+ * @param string $url
+ * @param array $post_data: POST data
+ * @param array $headers: HTTP headers
+ * @param array $options: CURL options
+ */
+ static function post($url, $post_data, $headers=null, $options=null) {
+ if($post_data == null) {
+ $post_data = array();
+ }
+ return self::execute($url, $post_data, $headers, $options);
+ }
+
+ /**
+ * HTTP GET or POST
+ */
+ protected static function execute($url, $post_data, $headers, $options) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
+
+ # POST
+ if($post_data) {
+ curl_setopt($ch, CURLOPT_POST, true);
+ curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
+ }
+ # Set headers
if($headers) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
}
+
+ # Set CURL options
+ if(!empty($options)) {
+ self::set_options($ch, $options);
+ }
- // Execute request
+ # Execute request
$response = curl_exec($ch);
+ $info = curl_getinfo($ch);
+ $curl_error = curl_errno($ch);
+
+ # Check for CURL errors
+ if(!$curl_error) {
+ # Extract HTTP status and body.
+ # Note: ignore eg. "HTTP/1.0 200 Connection established" from proxies
+ $many = preg_match("#HTTP\/1\.[0,1] (100 Continue|200 Connection established)#", $text);
+ if($many) {
+ list($header_text, $proxy_status, $body) = explode("\r\n\r\n", $response, 3);
+ } else {
+ list($header_text, $body) = explode("\r\n\r\n", $response, 2);
+ }
+ $header_lines = explode("\r\n", $header_text);
+
+ # Extract HTTP status
+ $header_line = array_shift($header_lines);
+ if (preg_match('@^HTTP/[0-9]\.[0-9] ([0-9]{3})@', $header_line, $matches)) {
+ $status = $matches[1];
+ }
+ # Extract HTTP headers
+ $headers = array();
+ foreach ($header_lines as $header_line) {
+ list($header, $value) = explode(': ', $header_line, 2);
+ $headers[$header] = $value;
+ }
+ } else {
+ $error = printf("CURL %s error code: %s\nMessage: %s\nOptions: %s",
+ $url, curl_errno($ch), curl_error($ch), print_r($options, true));
+ curl_close($ch);
+ throw new Exception($error);
+ }
+ curl_close($ch);
+
+ return new Response($status, $body, $headers, $info);
+ }
- // Split headers and body
- list($header_text, $body) = explode("\r\n\r\n", $response, 2);
- $header_lines = explode("\r\n", $header_text);
-
- // Extract HTTP status
- $header_line = array_shift($header_lines);
- if (preg_match('@^HTTP/[0-9]\.[0-9] ([0-9]{3})@', $header_line, $matches)) {
- $status = $matches[1];
+ /**
+ * Set CURL options.
+ *
+ * @param curl handle $ch
+ * @param array $options
+ */
+ protected static function set_options($ch, $options) {
+ # Debug log (file path)
+ if(isset($options['debug_log'])) {
+ $debug_log = fopen($options['debug_log'], 'a');
+ curl_setopt($ch, CURLOPT_STDERR, $debug_log);
+ curl_setopt($ch, CURLOPT_VERBOSE, 1);
+ }
+ # Follow redirects (boolean)
+ if(isset($options['follow_redirects'])) {
+ curl_setopt($ch, CURLOPT_FOLLOWLOCATION, $options['follow_redirects']);
}
-
- // Extract HTTP headers
- $headers = array();
- foreach ($header_lines as $header_line) {
- list($header, $value) = explode(': ', $header_line, 2);
- $headers[$header] = $value;
+ # Cookie jar (file path)
+ if(isset($options['cookie_jar'])) {
+ curl_setopt($ch, CURLOPT_COOKIEJAR, $options['cookie_jar']);
+ curl_setopt($ch, CURLOPT_COOKIEFILE, $options['cookie_jar']);
+ }
+ # Connect timeout (int)
+ if(isset($options['connect_timeout'])) {
+ curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $options['connect_timeout']);
+ }
+ # SSL verification (boolean)
+ if(isset($options['verify_ssl_certificate'])) {
+ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $options['verify_ssl_certificate']);
+ }
+ # Proxy (string)
+ if(isset($options['proxy'])) {
+ curl_setopt($ch, CURLOPT_PROXY, $options['proxy']);
}
-
- $info = curl_getinfo($ch);
-
- curl_close($ch);
-
- $status = $info['http_code'];
-
- return new Response($status, $body, $headers, $info);
}
}
-
+?>
View
25 index.html
@@ -0,0 +1,25 @@
+<p>A minimalist <span class="caps">HTTP</span> client for <span class="caps">PHP</span>.</p>
+<h2><span class="caps">GET</span></h2>
+<pre>
+ <code>
+$options = array (
+ 'debug_log' =&gt; '/tmp/http.log',
+ 'follow_redirects' =&gt; false,
+ 'connect_timeout' =&gt; 30000,
+ 'cookie_jar' =&gt; '/app/tmp/cookies.txt',
+ 'proxy' =&gt; 'http-proxy:8080'
+)
+$headers = array('User-Agent' =&gt; 'Googlebot/2.1 ( http://www.googlebot.com/bot.html)');
+$response = HTTP::get('http://google.com', $headers, $options);
+ </pre>
+<p></code></p>
+<h2><span class="caps">POST</span></h2>
+<pre>
+ <code>
+$parameters = array(
+ 'name' =&gt; 'Data',
+ 'file' =&gt; "@$file"#(file_get_contents('/tmp/data.txt')) # File upload
+);
+$response = HTTP::post('http://my-files/upload', $parameters)
+ </pre>
+<p></code></p>
Please sign in to comment.
Something went wrong with that request. Please try again.