Http Client Class June 9, 2006
Posted by rossoft in CakePHP.trackback
Uses Pear or Curl for a HTTP/HTTPS GET/POST request. Remembers
cookies and can do HTTP basic auth.
Pear requires the libs in vendors/pear/ . Download the libs here
Example1:
vendor(’http_client/http_client’);
$content=http_client_get(’http://www.google.es/search’,array(’hl’=>’en’ , ‘q’ => ‘cakephp’ ));
Example2: (equivalent)
vendor(’http_client/http_client’);
$client=HttpClient();
$document=$client->get(’http://www.google.es/search?hl=en&q=cakephp’));
Use the class if you need http auth or cookies between requests.
Put this in vendors/http_client/http_client.php
<?php
/**
* Http Client Class
*
* Uses Pear or Curl for a HTTP/HTTPS GET/POST request
* (preferred engine: Curl)
*
* Handles Cookies & HTTP Auth
*
* @author RosSoft
* @version 0.1
* @license MIT
*
* If you need cookies or HTTP Auth, don’t use the
* function wrappers http_client_get() or http_client_post()
* Use the class instead. Example:
* $client=new HttpClient();
* $client->user=’username’; //the request requires basic http auth
* $client->password=’xxxx’;
* $client->post(’http://example.com/login’,array(’admin’=>’1′)); //this will remember the cookie set by that request
* $client->get(’http://example.com/admin/index’); //the cookie is used
*
*/
/**
* Gets the content of an HTTP/HTTPS GET request
* @param string $url Destination URL
* @param array $params Associative array of params (don’t need to urlencode them)
* @param string $engine Can be ‘Pear’ or ‘Curl’
* @return string The content
*
*/
function http_client_get($url,$params=array(),$engine=’Curl’)
{
$client=new HttpClient($engine);
return $client->get($url,$params);
}
/**
* Gets the content of an HTTP/HTTPS POST request
* @param string $url Destination URL
* @param array $params Associative array of params (don’t need to urlencode them)
* @param string $engine Can be ‘Pear’ or ‘Curl’
* @return string The content
*/
function http_client_post($url,$params=array(),$engine=’Curl’)
{
$client=new HttpClient($engine);
return $client->post($url,$params);
}
/**
* Main class
* You can use indirectly through http_client_get
* or http_client_post wrappers or directly by:
*
* $client=HttpClient();
* $document=$client->get(’http://www.google.es/search’,array(’hl’=>’en’, ‘q’=>’cakephp’));
*
* Is exactly the same as:
* $client=HttpClient();
* $document=$client->get(’http://www.google.es/search?hl=en&q=cakephp’));
*
*/
class HttpClient extends Object
{
/**
* Engines: ‘Pear’,'Curl’
*/
var $_engine;
/**
* Http Connection timeout in seconds
*/
var $timeout=30;
/**
* Http Basic Auth
* @var $user
*/
var $user=null;
/**
* Http Basic Auth
* @var $password;
*/
var $password=null;
function HttpClient($engine=’Pear’)
{
if (!in_array($engine,array(’Pear’,'Curl’)))
{
$message=”HttpClient: unknown engine $engine”;
$this->log($message,LOG_ERROR);
die($message);
}
$engine=’HttpClient’ . $engine;
$this->_engine=new $engine($this);
}
/**
* Gets the content of an HTTP/HTTPS GET request
* @param string $url Destination URL
* @param array $params Associative array of params (don’t need to urlencode them)
* @return string The content
*/
function get($url,$params=array())
{
return $this->_engine->get($url,$params);
}
/**
* Gets the content of an HTTP/HTTPS POST request
* @param string $url Destination URL
* @param array $params Associative array of params (don’t need to urlencode them)
* @param string $engine Can be ‘Pear’ or ‘Curl’
* @return string The content
*/
function post($url,$params=array())
{
return $this->_engine->post($url,$params);
}
/**
* Returns information of last response
* (the content of the array is engine dependant)
* @return array
*/
function response()
{
return $this->_engine->response();
}
}
/**
* Engine Client: Pear
*/
class HttpClientPear extends Object
{
var $_client;
var $_response;
function __construct($parent)
{
$this->_parent=$parent;
vendor(’pear/init’); //does an ini_set for include_path
vendor(’pear/HTTP/Client’);
$params=array(’timeout’=>$this->_parent->timeout);
if ($this->_parent->user!==null)
{
$params['user']=$this->_parent->user;
$params['password']=$this->_parent->password;
}
$this->_client = new HTTP_Client($params);
}
function get($url,$params=array())
{
$this->_client->get($url, $params);
return $this->_execute();
}
function _execute()
{
$this->_response=$this->_client->currentResponse();
return $this->_response['body'];
}
function post($url,$params)
{
$this->_client->post($url, $params);
return $this->_execute();
}
function response()
{
return $this->_response;
}
}
/**
* Engine Client: Curl
*/
define(’HTTP_CLIENT_CURL_COOKIES’,CACHE . ‘http_curl_cookies.txt’);
class HttpClientCurl extends Object
{
var $_client;
var $_response;
function __construct($parent)
{
$this->_parent=$parent;
file_put_contents(HTTP_CLIENT_CURL_COOKIES,”);
}
function get($url,$params)
{
$this->_init();
if (count($params))
{
$url=$url . ‘?’ . $this->_convert_params($params);
}
curl_setopt($this->_client, CURLOPT_POST, 0);
curl_setopt($this->_client, CURLOPT_URL, $url);
return $this->_execute();
}
function post($url,$params)
{
$this->_init();
curl_setopt($this->_client, CURLOPT_POST, 1);
curl_setopt($this->_client, CURLOPT_POSTFIELDS, $this->_convert_params($params));
curl_setopt($this->_client, CURLOPT_URL, $url);
curl_setopt($this->_client, CURLOPT_FOLLOWLOCATION, 1);
$result=$this->_execute();
return $result;
}
function response()
{
return $this->_response;
}
function _execute()
{
$body= curl_exec($this->_client);
$this->_response=curl_getinfo($this->_client);
curl_close($this->_client);
return $body;
}
function _convert_params($params)
{
$array= array();
foreach ($params as $name=>$value)
{
$array[] = “$name=”.urlencode($value);
}
return implode(”&”, $array);
}
function _init()
{
$this->_client= curl_init();
if (! $this->_client)
{
die(’HttpClientCurl: curl_init() fails’);
}
curl_setopt($this->_client, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->_client, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($this->_client, CURLOPT_TIMEOUT,$this->_parent->timeout);
curl_setopt($this->_client, CURLOPT_COOKIEFILE,HTTP_CLIENT_CURL_COOKIES);
curl_setopt($this->_client, CURLOPT_COOKIEJAR,HTTP_CLIENT_CURL_COOKIES);
if ($this->_parent->user !== null)
{
curl_setopt($this->_client, CURLOPT_USERPWD, “{$this->_parent->user}:{$this->_parent->password}”);
}
}
}
?>
There’s some mistakes with references in PHP4, put
$client=& new …
and
$this->_parent=& $parent;
everywhere for PHP4
Last version in
http://cakeforge.org/snippet/detail.php?type=snippet&id=128
Hi Rosoft,
I have the following message when I try to include the file:
Fatal error: Class httpclient: Cannot inherit from undefined class object in /xxxxxxx/tools.php on line 82
Any ideas?
Thanks
Hi Rosoft,
1) I forgot to say that your file I call it tools.php, I’m using the version on http://cakeforge.org/snippet/detail.php?type=snippet&id=128 and on the line 82 is class HttpClient extends Object
2) could you please delete the path where the file is from the last post.
Thanks,
Andrew.
@andrew: just delete the sentence ‘extends Object’. Object is a class from CakePHP, you’ve this error because you’re using my class outside Cake