#19744704 XEHTTPRequest also support cURL library.

git-svn-id: http://xe-core.googlecode.com/svn/branches/1.5.0@8361 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
taggon 2011-05-12 00:03:12 +00:00
parent 72df762337
commit a3848104d4

View file

@ -1,13 +1,12 @@
<?php
/**
* @class HttpRequest
* @author NHN (developers@xpressengine.com)
* @version 0.1
* @brief a class that is designed to be used for sending out HTTP request to an external server and retrieving response
* @remarks Connection: keep-alive is not supported
*/
class XEHttpRequest {
/**
* @class HttpRequest
* @author NHN (developers@xpressengine.com)
* @version 0.1
* @brief a class that is designed to be used for sending out HTTP request to an external server and retrieving response
* @remarks Connection: keep-alive is not supported
*/
class XEHttpRequest {
/// target host
var $m_host;
/// target Port
@ -30,7 +29,7 @@
* @param[in] key HTTP header element
* @param[in] value value string for HTTP header element
*/
function AddToHeader($key, $value)
function addToHeader($key, $value)
{
$this->m_headers[$key] = $value;
}
@ -42,44 +41,103 @@
* @param[in] timeout time out value for HTTP request expiration
* @return Returns an object containing HTTP Response body and HTTP response code
*/
function Send($target, $method="GET", $timeout = 3)
function send($target='/', $method='GET', $timeout=3)
{
$socket = @fsockopen($this->m_host, $this->m_port, $errno, $errstr, $timeout);
if(!$socket)
{
return new Object(-1, "socket_connect_failed");
}
static $allow_methods=null;
$this->AddToHeader('Host', $this->m_host);
$this->AddToHeader('Connection', "close");
$this->addToHeader('Host', $this->m_host);
$this->addToHeader('Connection', 'close');
$crlf = "\r\n";
$request = "$method $target HTTP/1.1$crlf";
$method = strtoupper($method);
if(!$allow_methods) $allow_methods = explode(' ', 'GET POST PUT DELETE');
if(!in_array($method, $allow_methods)) $method = $allow_methods[0];
foreach($this->m_headers as $equiv => $content)
{
$request .= "$equiv: $content$crlf";
}
$request .= $crlf;
fwrite($socket, $request);
// $timeout should be an integer that is bigger than zero
$timout = max((int)$timeout, 0);
list($httpver, $code, $status) = split(' +', rtrim(fgets($socket)));
// read response header
while(strlen(trim($line = fgets($socket))))
{
list($equiv, $content) = split(' *: *', rtrim($line));
}
$body = '';
while(!feof($socket))
{
$body .= fgets($socket, 128);
}
fclose($socket);
$ret->result_code = $code;
$ret->body = $body;
return $ret;
if(is_callable('curl_init')) {
return $this->sendWithCurl($target, $method, $timeout);
} else {
return $this->sendWithSock($target, $method, $timeout);
}
}
}
/**
* @brief Send a request with the file socket
* @private
*/
function sendWithSock($target, $method, $timeout)
{
static $crlf = "\r\n";
$sock = @fsockopen($this->m_host, $this->m_port, $errno, $errstr, $timeout);
if(!$sock) {
return new Object(-1, 'socket_connect_failed');
}
$request = "$method $target HTTP/1.1$crlf";
foreach($this->m_headers as $equiv=>$content) {
$request .= "$equiv: $content$crlf";
}
$request .= $crlf;
fwrite($sock, $request);
list($httpver, $code, $status) = split(' +', rtrim(fgets($sock)));
// read response headers
while(strlen(trim($line = fgets($sock)))) {
list($equiv, $content) = split(' *: *', rtrim($line));
}
$body = '';
while(!feof($sock)) {
$body .= fgets($sock, 512);
}
fclose($sock);
$ret = new stdClass;
$ret->result_code = $code;
$ret->body = $body;
return $ret;
}
/**
* @brief Send a request with the curl library
* @private
*/
function sendWithCurl($target, $method, $timeout)
{
// creat a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://{$this->m_host}{$target}");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_PORT, $this->m_port);
curl_setopt($ch, CURLOPT_CONNECTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
switch($method) {
case 'GET': curl_setopt($ch, CURLOPT_HTTPGET, true); break;
case 'POST': curl_setopt($ch, CURLOPT_POST, true); break;
case 'PUT': curl_setopt($ch, CURLOPT_PUT, true); break;
}
$body = curl_exec($ch);
if(curl_errno($ch)) {
return new Object(-1, 'socket_connect_failed');
}
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$ret = new stdClass;
$ret->result_code = $code;
$ret->body = $body;
return $ret;
}
}
?>