A simple HTTP client for PHP
PHP posted 6 months ago by christian
A simple HTTP client for PHP that uses curl:
1 class HTTP { 2 static function get($url, $headers=null) { 3 $ch = curl_init(); 4 curl_setopt($ch, CURLOPT_URL, $url); 5 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 6 curl_setopt($ch, CURLOPT_HEADER, true); 7 8 if($headers) { 9 curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 10 } 11 12 // Execute request 13 $response = curl_exec($ch); 14 15 // Split headers and body 16 list($header_text, $body) = explode("\r\n\r\n", $response, 2); 17 $header_lines = explode("\r\n", $header_text); 18 19 // Extract HTTP status 20 $header_line = array_shift($header_lines); 21 if (preg_match('@^HTTP/[0-9]\.[0-9] ([0-9]{3})@', $header_line, $matches)) { 22 $status = $matches[1]; 23 } 24 25 // Extract HTTP headers 26 $headers = array(); 27 foreach ($header_lines as $header_line) { 28 list($header, $value) = explode(': ', $header_line, 2); 29 $headers[$header] = $value; 30 } 31 32 $info = curl_getinfo($ch); 33 34 curl_close($ch); 35 36 $status = $info['http_code']; 37 38 return new Response($status, $body, $headers, $info); 39 } 40 }
Even supports reading response headers.
1 class Response { 2 public function Response($code, $body, $headers, $info=null) { 3 $this->code = $code; 4 $this->body = $body; 5 $this->headers = $headers; 6 $this->info = $info; 7 } 8 }
A simple Python HTTP client
Python posted over 2 years ago by christian
A simple HTTP client I had lying around that I wrote a long time ago. It supports cookies, redirects and stuff:
1 #!/usr/bin/env python 2 # 3 # Http 4 # 5 # A simple HTTP client that supports persistent cookies 6 # 7 8 import cookielib 9 import httplib 10 #httplib.HTTPConnection.debuglevel = 1 11 import urllib2 12 13 class Http: 14 def __init__(self, redirect_callback = None): 15 self.redirect_callback = redirect_callback 16 self.cookie_jar = cookielib.CookieJar() 17 self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor (self.cookie_jar)) 18 19 urllib2.install_opener(self.opener) 20 21 def get(self, url, headers = None): 22 request = urllib2.Request(url, headers = headers) 23 return self.execute_request(request) 24 25 def post(self, url, headers = None, parameters = None): 26 data = None 27 if parameters != None: 28 data = urllib.urlencode(parameters) 29 30 request = urllib2.Request(url, data, headers) 31 return self.execute_request(request) 32 33 def execute_request(self, request): 34 response = self.opener.open(request) 35 # Check for redirect, maybe better way to do this 36 if response.geturl() != request.get_full_url(): 37 if self.redirect_callback == None: 38 raise "Redirected to '" + response.geturl() + "' but no redirect callback defined" 39 else: 40 self.redirect_callback(response) 41 42 return response 43
How to upload a file with PHP and lib_curl
PHP posted over 2 years ago by christian
This is an example of how to upload a file from PHP to another server that may or may not be running PHP.
Note that using Pear’s HTTP_Request package is probably easier.
1 <?php 2 require_once('Var_Dump.php'); 3 Var_Dump::displayInit(array('display_mode' => 'HTML4_Text'), array('mode' => 'normal','offset' => 4)); 4 5 $url = 'www.aktagon.com/upload'; 6 $file = '/tmp/file_to_upload'; 7 $post_data = array(); 8 9 $post_data['username'] = ''; 10 $post_data['password'] = ''; 11 $post_data['file'] = "@$file"; 12 13 $ch = curl_init(); 14 15 curl_setopt($ch, CURLOPT_URL, $url); 16 curl_setopt($ch, CURLOPT_HTTPHEADER, array( 17 # 'Cookie: xxxx', 18 # 'Content-Type: xxxx' 19 )); 20 curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 21 curl_setopt($ch, CURLOPT_POST, true); 22 #curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8888'); 23 24 curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data); 25 26 $response = curl_exec($ch); 27 $info = curl_getinfo($ch); 28 $http_code = $info['http_code']; 29 $speed = round($info['speed_upload'] / 1024); 30 $size = round($info['size_upload'] / 1024); 31 32 if($http_code != 200) 33 { 34 echo "Upload failed with HTTP code $http_code. Uploaded $size kilobytes with $speed kilobytes/s.<br/>"; 35 echo "<h1>Response</h1><pre>$response</pre><h1>Debug information</h1>"; 36 37 Var_Dump::display($info); 38 } 39 else 40 { 41 echo "Uploaded successful. Uploaded $size kilobytes with $speed kilobytes / s.<br/>"; 42 echo "<h1>Response</h1><pre>$response</pre>"; 43 } 44 curl_close($ch); 45 ?> 46
You can remove the Var_Dump code (Pear module), if you haven’t installed it, or don’t need it.