PHP
/ ************************************************** ********************************************************
* Class: Advanced Http Client
*********************************************************** ********************************************************
* Version: 1.1
* Released: 06-20-2002
* Last Modified: 06-10-2003
* Author: guinux <
Guinux@cosmoplazza.com>
*
*********************************************************** ********************************************************
* Changes
*********************************************************** ********************************************************
* 2003-06-10: Guinux
* - Fixed a bug with multiple gets and Basic Auth
* - ADDED Support for Basic Proxy Authentication
* 2003-05-25: by Michael Mauch <
Michael.mauch@gmx.de>
* - Fixed Two Occurences of the former "status" member heich is now deprecated
* 2002-09-23: Guinux
* - Fixed a bug to the post method with some http servers
* - Thanx to l0rd jenci <
Lord_jenci@bigfoot.com> for reporting this bug.
* 2002-09-07: Dirk fokken <
Fokken@cross-consulting.com>
* - Deleted Trailing Characters at The end, Right After the PHP CLOSIING TAG, IN ORDER
* To fix a bug with binary requests.
* 2002-20-06: Guinux, Major Changes
* - Turned to a more OOP style => added class http_header, http_response_header,
* Http_request_message, http_response_message.
* The Members: Status, Body, Response_Headers, Cookies, _Request_Headers of The Http Class
* Is deprecated.
* 2002-19-06: Guinux, fixed some bugs in the http :: _ get_response () Method
* 2002-18-06: BY MATE JOVIC <
Jovic@matoma.de>
* - Added Support for Basic Authentication * USAGE: $ http_client = new http (http_v11, false, array ('user', 'pass');
*
*********************************************************** ********************************************************
* DESCRIPTION:
*********************************************************** ********************************************************
* A http client class
* Supports:
* - Get, Head and Post Methods
* - http cookies
* - MULTIPART / FORM-DATA and Application / X-WWW-FORM-URLENCODED
* - chunked transfer-encoding
* - http 1.0 and 1.1 protocols
* - Keep-Alive Connections
* - Proxy
* - Basic WWW-Authentication and Proxy-Authentification
*
*********************************************************** ********************************************************
* Todo:
*********************************************************** ********************************************************
* - Read Trailing Headers for Chunked Transfer-Encoding
*********************************************************** ********************************************************
* USAGE
*********************************************************** ********************************************************
* See Example Scripts.
*
*********************************************************** ********************************************************
* License
*********************************************************** ********************************************************
* GNU Lesser General Public License (LGPL)
Http://www.opensource.org/license/lgpl-license.html
*
* For any suggestions or bug report please contact me: guinux@cosmoplazza.com
*********************************************************** ********************************************************* /
IF (! Defined ('http_crlf')) Define ('http_crlf', chr (13). chr (10));
Define ('http_v10', '1.0');
Define ('http_v11', '1.1');
Define ('http_status_continue ", 100);
Define ('http_status_switch_protocols', 101);
Define ('http_status_ok', 200);
Define ('http_status_created ", 201);
Define ('http_status_accepted ", 202);
Define ('http_status_non_authoritative ", 203);
Define ('http_status_no_content ", 204);
Define ('http_status_reset_content ", 205);
Define ('http_status_partial_content ", 206);
Define ('http_status_multiple_choices ", 300);
Define ('http_status_moved_persmanently', 301);
Define ('http_status_found ", 302);
Define ('http_status_see_other', 303);
Define ('http_status_not_modified ", 304);
Define ('http_status_use_proxy', 305);
Define ('http_status_temporary_redirect', 307);
Define ('http_status_bad_request ", 400);
Define ('http_status_unauthorized ", 401);
Define ('http_status_forbidden', 403);
Define ('http_status_not_found ", 404);
Define ('http_status_method_not_allowed', 405);
Define ('http_status_not_acceptable', 406); define ('http_status_proxy_auth_required ", 407);
Define ('http_status_request_timeout', 408);
Define ('http_status_conflict', 409);
Define ('http_status_gone', 410);
Define ('http_status_request_too_large ", 413);
Define ('http_status_uri_too_long', 414);
Define ('http_status_server_error', 500);
Define ('http_status_not_implement ", 501);
Define ('http_status_bad_gateway', 502);
Define ('http_status_service_unavailable ", 503);
Define ('http_status_version_not_supported', 505);
/ ************************************************** ************************************************
* Class http_header
*********************************************************** *************************************** /
Class http_header {
Var $ _HEADERS;
Var $ _DEBUG;
Function http_header () {
$ this -> _ headers = array ();
$ this -> _ debug = '';
} // end of function http_header ()
Function Get_Header ($ header_name) {
$ Header_name = $ this -> _ format_header_name ($ header_name);
IF (Isset ($ this -> _ headers [$ header_name]))
Return $ this -> _ Headers [$ header_name];
Else
Return NULL;
} // end of function get ()
Function set_header ($ header_name, $ value) {
IF ($ VALUE! = '') {
$ Header_name = $ this -> _ format_header_name ($ header_name);
$ this -> _ headers [$ header_name] = $ value;
}
} // end of function set ()
Function reset () {
IF ($ this -> _ headers)> 0) $ this -> _ headers = array ();
$ this -> _ debug. = "/ n --------------- Reseted ------------- / n";} // end of Function clear ()
Function serialize_headers () {
$ STR = '';
Foreach ($ this -> _ headers as $ name => $ value) {
$ Str. = "$ name: $ value". http_crlf;
}
Return $ STR;
} // end of function serialize_headers ()
Function _Format_Header_name ($ header_name) {
$ formatted = STR_REPLACE ('-', '', STRTOLOWER ($ header_name));
$ FORMATTED = UCWORDS ($ FORMATTED);
$ formatted = STR_REPLACE ('', '-', $ formatted);
Return $ formatted;
}
Function add_debug_info ($ data) {
$ this -> _ debug. = $ data;
}
Function Get_Debug_info () {
Return $ this -> _ debug;
}
} // end of class http_header
/ ************************************************** ************************************************
* Class http_response_header
*********************************************************** *************************************** /
Class http_response_header extends http_header {
Var $ cookies_headers;
Function http_response_header () {
$ this-> cookies_headers = array ();
http_header :: http_header ();
} // end of function http_response_header ()
Function deserialize_headers ($ flash_headers) {
$ FLAT_HEADERS = preg_replace ("/ ^". http_crlf. "/", '', $ flat_headers);
$ TMP_HEADERS = Split (http_crlf, $ flat_headers);
IF (PREG_MATCH ("'HTTP / (D.D) S (D ). *' I", $ TMP_HEADERS [0], $ matches)) {
$ this-> set_header ('protocol-version ", $ matches [1]);
$ this-> set_header ('status', $ matches [2]);
}
Array_shift ($ TMP_HEADERS);
Foreach ($ TMP_HEADERS AS $ INDEX => $ VALUE) {
$ POS = STRPOS ($ Value, ':'); if ($ POS) {
$ key = Substr ($ Value, 0, $ POS);
$ Value = Trim (Substr ($ Value, $ POS 1);
IF (STRTOUPPER == 'set-cookie')
$ this-> cookies_headers [] = $ value;
Else
$ this-> set_header ($ key, $ value);
}
}
} // end of function deserity_headers ()
Function reset () {
IF ($ this-> cookies_headers> 0) $ this-> cookies_headers = array ();
http_header :: reset ();
}
} // end of class http_response_header
/ ************************************************** ************************************************
* Class http_request_message
*********************************************************** *************************************** /
Class http_request_message extends http_header {
Var $ body;
Function http_request_message () {
$ this-> body = '';
http_header :: http_header ();
} // end of function http_message ()
Function reset () {
$ this-> body = '';
http_header :: reset ();
}
}
/ ************************************************** ************************************************
* Class http_response_message
*********************************************************** *************************************** /
Class http_response_message extends http_response_header {
Var $ body;
Var $ cookies;
Function http_response_message () {
$ this-> cookies = new http_cookie ();
$ this-> body = '';
http_response_header :: http_response_header ();
} // end of function http_response_message ()
Function Get_Status () {
IF ($ this-> get_header ('status')! = NULL)
Return (Integer) $ this-> get_header ('status');
Else
Return -1;
}
Function Get_Protocol_Version () {
IF ($ this-> get_header ('protocol-version')! = null) Return $ this-> get_header ('protocol-version');
Else
Return http_v10;
}
Function GET_CONTENT_TYPE () {
$ this-> get_header ('content-type');
}
Function GET_BODY () {
Return $ this-> body;
}
Function reset () {
$ this-> body = '';
Http_response_header :: reset ();
}
Function Parse_cookies ($ Host) {
For ($ I = 0; $ i <-> cookies_headers; $ i )
$ this-> cookies-> parse ($ this-> cookies_headers [$ I], $ host);
}
}
/ ************************************************** ************************************************
* Class http_cookie
*********************************************************** *************************************** /
Class http_cookie {
Var $ cookies;
Function http_cookie () {
$ this-> cookies = array ();
} // end of function http_cookies ()
Function _now () {
Return Strtotime (GMDATE ("L, D-F-Y H: I: S", TIME ()));
} // end of function _now ()
Function _timestamp ($ DATE) {
IF ($ DATE == ') Return $ this -> _ now () 3600;
$ TIME = STRTOTIME ($ DATE);
Return ($ TIME> 0? $ TIME: $ this -> _ now () 3600);
} // end of function _timestamp ()
Function Get ($ current_domain, $ current_path) {
$ cookie_str = '';
$ now = $ this -> _ now ();
$ new_cookies = array ();
Foreach ($ this-> cookies as $ cookie_name => $ cookie_data) {
IF ($ cookie_data ['expires']> $ now) {
$ New_COOKIES [$ cookie_name] = $ cookie_data;
$ domain = preg_quote ($ cookie_data ['domain ");
$ PATH = preg_quote ($ cookie_data ['PATH']);
IF (preg_match ("" $ domain $ 'I ", $ current_domain) && preg_match ("' ^ $ PATH. * 'I ", $ current_path) $ cookie_str. = $ cookie_name.' = '. $ cookie_data [ 'Value']. ';';
}
}
$ this-> cookies = $ new_cookies;
Return $ cookie_str;
} // end of function get ()
Function SET ($ Name, $ Value, $ Domain, $ PATH, $ EXPIRES) {
$ this-> cookies [$ name] = array ('value' => $ value,
'Domain' => $ Domain,
'PATH' => $ PATH,
'Expires' => $ this -> _ TimeStamp ($ Expires)
);
} // end of function set ()
Function Parse ($ cookie_str, $ host) {
$ cookie_str = STR_REPLACE (';', ';', $ cookie_str). ';';
$ DATA = Split (';', $ cookie_str);
$ Value_Str = $ data [0];
$ cookie_param = 'domain =';
$ start = STRPOS ($ cookie_str, $ cookie_param);
IF ($ START> 0) {
$ domain = Substr ($ cookie_str, $ start strlen ($ cookie_param);
$ domain = Substr ($ Domain, 0, Strpos ($ Domain, ';'));
Else
$ domain = $ host;
$ cookie_param = 'expires =';
$ start = STRPOS ($ cookie_str, $ cookie_param);
IF ($ START> 0) {
$ expires = substr ($ cookie_str, $ start strlen ($ cookie_param);
$ expires = Substr ($ Expires, 0, Strpos ($ Expires, ';'));
Else
$ expires = '';
$ cookie_param = 'PATH =';
$ start = STRPOS ($ cookie_str, $ cookie_param);
IF ($ START> 0) {
$ PATH = SUBSTR ($ cookie_str, $ start strlen ($ cookie_param));
$ PATH = SUBSTR ($ PATH, 0, STRPOS ($ PATH, '));
Else
$ PATH = '/';
$ SEP_POS = STRPOS ($ Value_Str, '='); if ($ SEP_POS) {
$ Name = Substr ($ Value_Str, 0, $ SEP_POS);
$ Value = Substr ($ Value_Str, $ SEP_POS 1);
$ this-> set ($ Name, $ Value, $ Domain, $ PATH, $ EXPIRES;
}
} // end of function parse ()
} // End of class http_cookie
/ ************************************************** ************************************************
* Class HTTP
*********************************************************** *************************************** /
Class http {
Var $ _Socket;
Var $ host;
Var $ port;
Var $ http_version;
VAR $ user_agent;
Var $ Errstr;
Var $ connect;
Var $ URI;
Var $ _PROXY_HOST;
VAR $ _PROXY_PORT;
Var $ _PROXY_LOGIN;
Var $ _Proxy_PWD;
Var $ _USE_PROXY;
Var $ _AUTH_LOGIN;
Var $ _AUTH_PWD;
Var $ _RESPONSE;
VAR $ _REQUEST;
Var $ _Keep_alive;
Function http ($ http_version = http_v10, $ keep_alive = false, $ auth = false) {
$ this-> http_version = $ http_version;
$ this-> connect = false;
$ this-> user_agent = 'cosmohttp / 1.1 (compatible; msie 5.5; linux)';
$ this-> Host = '';
$ this-> port = 80;
$ this-> errstr = '';
$ this -> _ keep_alive = $ keep_alive;
$ this -> _ proxy_host = '';
$ this -> _ proxy_port = -1;
$ this -> _ proxy_login = '';
$ this -> _ proxy_pwd = '';
$ this -> _ auth_login = '';
$ this -> _ auth_pwd = '';
$ this -> _ USE_PROXY = FALSE
$ this -> _RESPONSE = new http_response_message ();
$ this -> _ request = new http_request_message ();
// Basic Authentication Added by Mate Jovic, 2002-18-06,
Jovic@matoma.de
IF (is_ARRAY ($ auth) == 2) {$ this -> _ auth_login = $ auth [0];
$ this -> _ auth_pwd = $ auth [1];
}
} // end of constor
Function use_proxy ($ Host, $ Port, $ proxy_login = null, $ proxy_pwd = null) {
// Proxy Auth Not Yet Supported
$ this-> http_version = http_v10;
$ this -> _ keep_alive = false;
$ this -> _ proxy_host = $ host;
$ this -> _ proxy_port = $ port;
$ this -> _ proxy_login = $ proxy_login;
$ this -> _ proxy_pwd = $ proxy_pwd;
$ this -> _ USE_PROXY = TRUE;
}
Function Set_Request_Header ($ Name, $ VALUE) {
$ this -> _ propest-> set_header ($ name, $ value);
}
Function Get_Response_body () {
Return $ this -> _RESPONSE-> BODY;
}
Function Get_Response () {
Return $ this - >_Restere;
}
Function head ($ URI) {
$ this-> URI = $ URI;
IF ($ this -> _ keep_alive&&! $ this-> connection) ||! $ this -> _ keep_alive) {
IF (! $ this -> _ connect ()) {
$ this-> Errstr = 'Could Not Connect To'. $ this-> Host;
Return -1;
}
}
$ http_cookie = $ this -> _ response-> cookies-> get ($ this-> Host, $ this -> _ current_directory ($ URI));
IF ($ this -> _ user_proxy) {
$ this -> _ propest-> set_header ('host', $ this-> host. ':'. $ this-> port);
$ this -> _ request-> set_header ('proxy-connection ", ($ this -> _ keep_alive?' Keep-alive ':' close ');
IF ($ this -> _ proxy_login! = ') $ this -> _ request-> set_header (' proxy-authorization ', "base". base64_encode ($ this -> _ proxy_login. ":": "$ this -> _ proxy_pwd)) ;
$ uri = 'http: //'. $ this-> Host. ':'. $ this-> port. $ uri;} else {
$ this -> _ request-> set_header ('host', $ this-> host);
$ this -> _ request-> set_header ('connection ", ($ this -> _ keep_live?' keep-alive ':' close ');
}
IF ($ this -> _ auth_login! = ') $ this -> _ propest-> set_header (' authorization ', "Basic". base64_encode ($ this -> _ auth_login. ":". $ this -> _ auth_pwd);
$ this -> _ request-> set_header ('user-agent', $ this-> user_agent);
$ this -> _ request-> set_header ('accept', '* / *');
$ this -> _ request-> set_header ('cookie', $ http_cookie);
$ cmd = "Head $ uri http /". $ this-> http_version. http_crlf.
$ this -> _ propest-> serialize_headers ().
HTTP_CRLF;
FWRITE ($ this -> _ socket, $ cmd);
$ this -> _ request-> add_debug_info ($ cmd);
$ this -> _ get_response (false);
IF ($ this -> _ Socket &&! $ this -> _ keep_alive) $ this-> disconnect ();
IF ($ this -> _RESPONSE-> GET_HEADER ('connection')! = NULL) {
IF ($ this -> _ keep_alive && strtolower ($ this -> _ response-> get_header ('connection') == 'close') {
$ this -> _ keep_alive = false;
$ this-> disconnect ();
}
}
IF ($ this -> _RESPONSE-> get_status () == http_status_use_proxy) {
$ location = $ this -> _ parse_location ($ this -> _ response-> get_header ('location'));
$ this-> disconnect ();
$ this-> USE_PROXY ($ location ['Host'], $ location ['Port']);
$ this-> Head ($ this-> URI);
}
Return $ this -> _ response-> get_header ('status');
} // end of function head () Function Get ($ URI, $ FOLLOW_RIRECTS = true, $ reres = ') {
$ this-> URI = $ URI;
IF ($ this -> _ keep_alive&&! $ this-> connection) ||! $ this -> _ keep_alive) {
IF (! $ this -> _ connect ()) {
$ this-> Errstr = 'Could Not Connect To'. $ this-> Host;
Return -1;
}
}
IF ($ this -> _ user_proxy) {
$ this -> _ propest-> set_header ('host', $ this-> host. ':'. $ this-> port);
$ this -> _ request-> set_header ('proxy-connection ", ($ this -> _ keep_alive?' Keep-alive ':' close ');
IF ($ this -> _ proxy_login! = ') $ this -> _ request-> set_header (' proxy-authorization ', "base". base64_encode ($ this -> _ proxy_login. ":": "$ this -> _ proxy_pwd)) ;
$ uri = 'http: //'. $ this-> Host. ':'. $ this-> port. $ uri;
} else {
$ this -> _ request-> set_header ('host', $ this-> host);
$ this -> _ request-> set_header ('connection ", ($ this -> _ keep_live?' keep-alive ':' close ');
$ this -> _ request-> set_header ('prgma', 'no-cache');
$ this -> _ request-> set_header ('cache-control ";" no-cache');
}
IF ($ this -> _ auth_login! = ') $ this -> _ propest-> set_header (' authorization ', "Basic". base64_encode ($ this -> _ auth_login. ":". $ this -> _ auth_pwd);
$ http_cookie = $ this -> _ response-> cookies-> get ($ this-> Host, $ this -> _ current_directory ($ URI));
$ this -> _ request-> set_header ('user-agent', $ this-> user_agent);
$ this -> _ request-> set_header ('accept', '* / *');
$ this -> _ request-> set_header ('Referer', $ reres); $ this -> _ request-> set_header ('cookie', $ http_cookie);
$ cmd = "GET $ URI HTTP /". $ this-> http_version. http_crlf.
$ this -> _ propest-> serialize_headers ().
HTTP_CRLF;
FWRITE ($ this -> _ socket, $ cmd);
$ this -> _ request-> add_debug_info ($ cmd);
$ this -> _ get_response ();
IF ($ this -> _ Socket &&! $ this -> _ keep_alive) $ this-> disconnect ();
IF ($ this -> _RESPONSE-> GET_HEADER ('connection')! = NULL) {
IF ($ this -> _ keep_alive && strtolower ($ this -> _ response-> get_header ('connection') == 'close') {
$ this -> _ keep_alive = false;
$ this-> disconnect ();
}
}
if ($ follow_redirects && ($ this -> _ response-> get_status () == HTTP_STATUS_MOVED_PERMANENTLY || $ this -> _ response-> get_status () == HTTP_STATUS_FOUND || $ this -> _ response-> get_status () == HTTP_STATUS_SEE_OTHER) ) {
IF ($ this -> _ response-> get_header ('location')! = null) {
$ this -> _ redirect ($ this -> _ response-> get_header ('location'));
}
}
IF ($ this -> _RESPONSE-> get_status () == http_status_use_proxy) {
$ location = $ this -> _ parse_location ($ this -> _ response-> get_header ('location'));
$ this-> disconnect ();
$ this-> USE_PROXY ($ location ['Host'], $ location ['Port']);
$ this-> Get ($ this-> Uri, $ reled);
}
Return $ this -> _ response-> get_status ();
} // end of function get ()
Function Multipart_post ($ URI, & $ FORM_FIELDS, $ form_files = null, $ folow_redirects = true, $ reres = ') {
$ this-> Uri = $ URI; IF ($ this -> _ keep_alive&&! $ this-> connection) ||! $ this -> _ keep_alive) {
IF (! $ this -> _ connect ()) {
$ this-> Errstr = 'Could Not Connect To'. $ this-> Host;
Return -1;
}
}
$ boundary = uniqid ('----------------');
$ http_cookie = $ this -> _ response-> cookies-> get ($ this-> Host, $ this -> _ current_directory ($ URI));
$ body = $ this -> _ merge_multipart_form_data ($ Boundary, $ FORM_FIELDS, $ form_files);
$ this -> _ request-> body = $ body. http_crlf;
$ Content_length = Strlen ($ body);
IF ($ this -> _ user_proxy) {
$ this -> _ propest-> set_header ('host', $ this-> host. ':'. $ this-> port);
$ this -> _ request-> set_header ('proxy-connection ", ($ this -> _ keep_alive?' Keep-alive ':' close ');
IF ($ this -> _ proxy_login! = ') $ this -> _ request-> set_header (' proxy-authorization ', "base". base64_encode ($ this -> _ proxy_login. ":": "$ this -> _ proxy_pwd)) ;
$ uri = 'http: //'. $ this-> Host. ':'. $ this-> port. $ uri;
} else {
$ this -> _ request-> set_header ('host', $ this-> host);
$ this -> _ request-> set_header ('connection ", ($ this -> _ keep_live?' keep-alive ':' close ');
$ this -> _ request-> set_header ('prgma', 'no-cache');
$ this -> _ request-> set_header ('cache-control ";" no-cache');
}
IF ($ this -> _ auth_login! = ') $ this -> _ propest-> set_header (' authorization ', "Basic". base64_encode ($ this -> _ auth_login. ":". $ this -> _ auth_pwd);
$ this -> _ request-> set_header ('accept', '* / *'); $ this -> _ propest-> set_header ('content-type', 'multipart / form-data; boundary ='. $ boundary);
$ this -> _ request-> set_header ('user-agent', $ this-> user_agent);
$ this -> _ request-> set_header ('content-length', $ content_length);
$ this -> _ request-> set_header ('cookie', $ http_cookie);
$ this -> _ request-> set_header ('Referer', $ Referr);
$ req_header = "post $ uri http /". $ this-> http_version. http_crlf.
$ this -> _ propest-> serialize_headers ().
HTTP_CRLF;
FWRITE ($ this -> _ socket, $ req_header;
Usleep (10);
FWRITE ($ this -> _ Socket, $ this -> _ request-> body);
$ this -> _ propest-> add_debug_info ($ req_header);
$ this -> _ get_response ();
IF ($ this -> _ Socket &&! $ this -> _ keep_alive) $ this-> disconnect ();
IF ($ this -> _RESPONSE-> GET_HEADER ('connection')! = NULL) {
IF ($ this -> _ keep_alive && strtolower ($ this -> _ response-> get_header ('connection') == 'close') {
$ this -> _ keep_alive = false;
$ this-> disconnect ();
}
}
if ($ follow_redirects && ($ this -> _ response-> get_status () == HTTP_STATUS_MOVED_PERMANENTLY || $ this -> _ response-> get_status () == HTTP_STATUS_FOUND || $ this -> _ response-> get_status () == HTTP_STATUS_SEE_OTHER) ) {
IF ($ this -> _ response-> get_header ('location')! = null) {
$ this -> _ redirect ($ this -> _ response-> get_header ('location'));
}
}
IF ($ this -> _RESPONSE-> get_status () == http_status_use_proxy) {
$ location = $ this -> _ parse_location ($ this -> _RESPONSE-> Get_Header ('location')); $ this-> disconnect ();
$ this-> USE_PROXY ($ location ['Host'], $ location ['Port']);
$ this-> Multipart_post ($ this-> Uri, $ FORM_FIELDS, $ FORM_FILES, $ reled);
}
Return $ this -> _ response-> get_status ();
} // end of function multipart_post ()
Function Post ($ URI, & $ FORM_DATA, $ FOLLOW_REDIRECTS = True, $ referer = ') {
$ this-> URI = $ URI;
IF ($ this -> _ keep_alive&&! $ this-> connection) ||! $ this -> _ keep_alive) {
IF (! $ this -> _ connect ()) {
$ this-> Errstr = 'Could Not Connect To'. $ this-> Host;
Return -1;
}
}
$ http_cookie = $ this -> _ response-> cookies-> get ($ this-> Host, $ this -> _ current_directory ($ URI));
$ body = subside ($ this -> _ merge_form_data ($ form_data), 1);
$ this -> _ request-> body = $ body. http_crlf. http_crlf;
$ Content_length = Strlen ($ body);
IF ($ this -> _ user_proxy) {
$ this -> _ propest-> set_header ('host', $ this-> host. ':'. $ this-> port);
$ this -> _ request-> set_header ('proxy-connection ", ($ this -> _ keep_alive?' Keep-alive ':' close ');
IF ($ this -> _ proxy_login! = ') $ this -> _ request-> set_header (' proxy-authorization ', "base". base64_encode ($ this -> _ proxy_login. ":": "$ this -> _ proxy_pwd)) ;
$ uri = 'http: //'. $ this-> Host. ':'. $ this-> port. $ uri;
} else {
$ this -> _ request-> set_header ('host', $ this-> host);
$ this -> _ request-> set_header ('connection ", ($ this -> _ keep_live?' Keep-alive ':' close ')); $ this -> _ request-> set_header (' pragma ',' no-cache" );
$ this -> _ request-> set_header ('cache-control ";" no-cache');
}
IF ($ this -> _ auth_login! = ') $ this -> _ propest-> set_header (' authorization ', "Basic". base64_encode ($ this -> _ auth_login. ":". $ this -> _ auth_pwd);
$ this -> _ request-> set_header ('accept', '* / *');
$ this -> _ request-> set_header ('content-type', 'application / x-www-form-urlencoded');
$ this -> _ request-> set_header ('user-agent', $ this-> user_agent);
$ this -> _ request-> set_header ('content-length', $ content_length);
$ this -> _ request-> set_header ('cookie', $ http_cookie);
$ this -> _ request-> set_header ('Referer', $ Referr);
$ req_header = "post $ uri http /". $ this-> http_version. http_crlf.
$ this -> _ propest-> serialize_headers ().
HTTP_CRLF;
FWRITE ($ this -> _ socket, $ req_header;
Usleep (10);
FWRITE ($ this -> _ Socket, $ this -> _ request-> body);
$ this -> _ propest-> add_debug_info ($ req_header);
$ this -> _ get_response ();
IF ($ this -> _ Socket &&! $ this -> _ keep_alive) $ this-> disconnect ();
IF ($ this -> _RESPONSE-> GET_HEADER ('connection')! = NULL) {
IF ($ this -> _ keep_alive && strtolower ($ this -> _ response-> get_header ('connection') == 'close') {
$ this -> _ keep_alive = false;
$ this-> disconnect ();
}
}
if ($ follow_redirects && ($ this -> _ response-> get_status () == HTTP_STATUS_MOVED_PERMANENTLY || $ this -> _ response-> get_status () == HTTP_STATUS_FOUND || $ this -> _ response-> get_status () == HTTP_STATUS_SEE_OTHER) ) {IF ($ this -> _ response-> get_header ('location')! = null) {
$ this -> _ redirect ($ this -> _ response-> get_header ('location'));
}
}
IF ($ this -> _RESPONSE-> get_status () == http_status_use_proxy) {
$ location = $ this -> _ parse_location ($ this -> _ response-> get_header ('location'));
$ this-> disconnect ();
$ this-> USE_PROXY ($ location ['Host'], $ location ['Port']);
$ this-> Post ($ this-> Uri, $ FORM_DATA, $ Referr);
}
Return $ this -> _ response-> get_status ();
} // end of function post ()
Function Post_XML ($ URI, $ XML_DATA, $ FOLLOW_REDIRECTS = True, $ Referr = ') {
$ this-> URI = $ URI;
IF ($ this -> _ keep_alive&&! $ this-> connection) ||! $ this -> _ keep_alive) {
IF (! $ this -> _ connect ()) {
$ this-> Errstr = 'Could Not Connect To'. $ this-> Host;
Return -1;
}
}
$ http_cookie = $ this -> _ response-> cookies-> get ($ this-> Host, $ this -> _ current_directory ($ URI));
$ body = $ XML_DATA;
$ this -> _ request-> body = $ body. http_crlf. http_crlf;
$ Content_length = Strlen ($ body);
IF ($ this -> _ user_proxy) {
$ this -> _ propest-> set_header ('host', $ this-> host. ':'. $ this-> port);
$ this -> _ request-> set_header ('proxy-connection ", ($ this -> _ keep_alive?' Keep-alive ':' close ');
IF ($ this -> _ proxy_login! = ') $ this -> _ request-> set_header (' proxy-authorization ', "base". base64_encode ($ this -> _ proxy_login. ":": "$ this -> _ proxy_pwd)) $ uri = 'http: //'. $ this-> Host. ':'. $ this-> port. $ uri;
} else {
$ this -> _ request-> set_header ('host', $ this-> host);
$ this -> _ request-> set_header ('connection ", ($ this -> _ keep_live?' keep-alive ':' close ');
$ this -> _ request-> set_header ('prgma', 'no-cache');
$ this -> _ request-> set_header ('cache-control ";" no-cache');
}
IF ($ this -> _ auth_login! = ') $ this -> _ propest-> set_header (' authorization ', "Basic". base64_encode ($ this -> _ auth_login. ":". $ this -> _ auth_pwd);
$ this -> _ request-> set_header ('accept', '* / *');
$ this -> _ request-> set_header ('content-type', 'text / xml; charset = utf-8');
$ this -> _ request-> set_header ('user-agent', $ this-> user_agent);
$ this -> _ request-> set_header ('content-length', $ content_length);
$ this -> _ request-> set_header ('cookie', $ http_cookie);
$ this -> _ request-> set_header ('Referer', $ Referr);
$ req_header = "post $ uri http /". $ this-> http_version. http_crlf.
$ this -> _ propest-> serialize_headers ().
HTTP_CRLF;
FWRITE ($ this -> _ socket, $ req_header;
Usleep (10);
FWRITE ($ this -> _ Socket, $ this -> _ request-> body);
$ this -> _ propest-> add_debug_info ($ req_header);
$ this -> _ get_response ();
IF ($ this -> _ Socket &&! $ this -> _ keep_alive) $ this-> disconnect (); if ($ this -> _ response-> get_header ('connection')! = null) {
IF ($ this -> _ keep_alive && strtolower ($ this -> _ response-> get_header ('connection') == 'close') {
$ this -> _ keep_alive = false;
$ this-> disconnect ();
}
}
if ($ follow_redirects && ($ this -> _ response-> get_status () == HTTP_STATUS_MOVED_PERMANENTLY || $ this -> _ response-> get_status () == HTTP_STATUS_FOUND || $ this -> _ response-> get_status () == HTTP_STATUS_SEE_OTHER) ) {
IF ($ this -> _ response-> get_header ('location')! = null) {
$ this -> _ redirect ($ this -> _ response-> get_header ('location'));
}
}
IF ($ this -> _RESPONSE-> get_status () == http_status_use_proxy) {
$ location = $ this -> _ parse_location ($ this -> _ response-> get_header ('location'));
$ this-> disconnect ();
$ this-> USE_PROXY ($ location ['Host'], $ location ['Port']);
$ this-> Post ($ this-> Uri, $ FORM_DATA, $ Referr);
}
Return $ this -> _ response-> get_status ();
} // end of function post_xml ()
Function disconnect () {
IF ($ this -> _ Socket && $ this-> connection) {
Fclose ($ this -> _ saocket);
$ this-> connect = false;
}
} // end of function disconnect ()
/ ************************************************** *****************************
* Private functions
*********************************************************** ***************************** /
Function _Connect () {
IF ($ this-> Host == ') user_error (' class http -> _ connect (): host material not set! ', e_ERROR);
IF ($ this -> _ USE_PROXY)
$ this -> _ socket = fsockopen ($ this-> Host, $ this-> Port, $ Errno, $ Errstr, 10);
Else
$ this -> _ Socket = fsockopen ($ this -> _ proxy_host, $ this -> _ proxy_port, $ errno, $ errstr, 10);
$ this-> Errstr = $ Errstr;
$ this-> connected = ($ this -> _ socket == true);
Return $ this-> connect;
} // end of function connect ()
Function _Merge_multipart_form_data ($ Boundary, & $ FORM_FIELDS, & $ FORM_FILES) {
$ boundary = '-'. $ boundary;
$ MULTIPART_BODY = '';
Foreach ($ FORM_FIELDS AS $ Name => $ data) {
$ MULTIPART_BODY. = $ boundary. http_crlf;
$ MULTIPART_BODY. = 'Content-Disposition: Form-Data; Name = "'. $ name. '"'. http_crlf;
$ MULTIPART_BODY. = http_crlf;
$ MULTIPART_BODY. = $ data. http_crlf;
}
IF (isset ($ form_files)) {
Foreach ($ FORM_FILES AS $ DATA) {
$ MULTIPART_BODY. = $ boundary. http_crlf;
$ MULTIPART_BODY. = 'Content-Disposition: Form-Data; Name = "'. $ data ['name']. '"; filename = "'. $ data ['filename']. '"'. http_crlf;
IF ($ DATA ['Content-Type']! = ')
$ MULTIPART_BODY. = 'Content-Type:'. $ data ['content-type']. http_crlf;
Else
$ MULTIPART_BODY. = 'Content-Type: Application / OcTet-stream'. http_crlf;
$ MULTIPART_BODY. = http_crlf;
$ MULTIPART_BODY. = $ data ['data']. http_crlf;
}
}
$ MULTIPART_BODY. = $ boundary. '-'. http_crlf;
RETURN $ MULTIPART_BODY;
} // End of function _merge_multipart_form_data ()
Function _Merge_form_data (& $ Param_array, $ param_name = '') {
$ params = ''; $ format = ($ param_name! = ''? '&'. $ param_name. '[% s] =% s': '&% s =% s');
Foreach ($ Param_Array AS $ Key => $ VALUE) {
IF (! is_ARRAY ($ Value))
$ params. = Sprintf ($ FORMAT, $ Key, Urlencode);
Else
$ params. = $ this -> _ merge_form_data ($ Param_Array [$ Keey], $ Key);
}
Return $ Params;
} // end of function _merge_form_data ()
Function _Current_directory ($ URI) {
$ TMP = Split ('/', $ uri);
Array_POP ($ TMP);
$ current_dir = Implode ('/', $ tmp). '/';
RETURN ($ current_dir! = '? $ current_dir:' / ');
} // end of function _current_directory ()
Function _Get_response ($ get_body = true) {
$ this -> _RESPONSE-> reset ();
$ this -> _ propest-> reset ();
$ Header = '';
$ body = '';
$ Continue = True;
While ($ Continue) {
$ Header = '';
// read The Response Headers
While ($ tHIS -> _ Socket, 4096))! = http_crlf || $ header == ') &&! feof ($ this -> _ socket) {
IF ($ line! = http_crlf) $ header. = $ line;
}
$ this -> _RESPONSE-> DeserialIze_headers ($ HEADER);
$ this -> _RESPONSE-> PARSE_COOKIES ($ this-> host);
$ this -> _RESPONSE-> add_debug_info ($ header);
$ company = ($ this -> _RESPONSE-> get_status () == http_status_continue);
IF ($ THIS -> _ Socket, http_crlf);
}
IF ($ get_body) return;
// read the response body
IF ($ this -> _ response-> get_header ('transfer-encoding'))! = 'chunked' &&! $ this -> _ keep_alive) {
While (! Feof ($ this -> _ socket) {
$ body. = fread ($ this -> _ socket, 4096);}
} else {
IF ($ this -> _RESPONSE-> GET_HEADER ('content-length')! = null) {
$ Content_length = (Integer) $ this -> _RESPONSE-> GET_HEADER ('Content-length');
$ body = fread ($ this -> _ socket, $ content_length;
} else {
IF ($ this -> _ response-> get_header ('transfer-encoding')! = null) {
IF ($ this -> _ response-> get_header ('transferr-encoding') == 'chunked') {
$ chunk_size = (integer) HEXDEC (FGETS ($ this -> _ socket, 4096));
While ($ chunk_size> 0) {
$ body. = fread ($ this -> _ socket, $ chunk_size);
Fread ($ this -> _ socket, strlen (http_crlf);
$ chunk_size = (integer) HEXDEC (FGETS ($ this -> _ socket, 4096));
}
// Todo: Read Trailing http headers
}
}
}
}
$ this -> _RESPONSE-> body = $ body;
} // end of function _get_response ()
Function _PARSE_LOCATION ($ redirect_uri) {
$ pased_url = parse_url ($ redirect_uri);
$ scheme = (isset ($ pased_url ['scheme'])? $ parse_url ['Scheme']: ');
$ port = (isset ($ pased_url ['port'])? $ pased_url ['Port']: $ this-> port);
$ Host = (isset ($ paared_url ['Host'])? $ pased_url ['Host']: $ this-> Host);
$ request_file = (isset ($ paared_url ['PATH')? $ parse_url ['Path']: ');
$ query_string = (isset ($ paared_url [query '])? $ parse_url [' query ']:');
IF (Substr ($ Request_File, 0, 1)! = '/')
$ Request_File = $ this -> _ current_directory ($ this-> URI). $ request_file;
Return Array ('Scheme' => $ Scheme,
'port' => $ port, 'Host' => $ Host,
'Request_File' => $ Request_File,
'query_string' => $ query_string
);
} // end of function _parse_location ()
Function _redirect ($ URI) {
$ location = $ this -> _ parse_location ($ URI);
IF ($ location ['Host']! = $ this-> Host || $ location ['Port']! = $ this-> port) {
$ this-> Host = $ location ['Host'];
$ this-> port = $ location ['port'];
IF (! $ this -> _ us_proxy) $ this-> disconnect ();
}
Usleep (100);
$ this-> get ($ location ['request_file']. '?'. $ location ['query_string']);
} // end of function _redirect ()
} // End of class http
?>
Hand HTTP GET
PHP code: ----------------------------------------------- ---------------------------------
PHP
/ ************************************************** *******************
* DemonStrates the use of the get () Method
*********************************************************** ****************** /
REQUIRE_ONCE ('http.inc');
Header ('Content-Type: Text / XML');
// grab a rdf file from phpdeveloper.org and display it
$ http_client = new http (http_v11, false);
$ http_client-> host = 'www.phpdeveloper.org';
IF ($ http_client-> get ('/ phpdev.rdf') == http_status_ok)
PRINT ($ http_client-> get_response_body ());
Else
Print ('Server Returned'. $ http_client-> status);
UNSET ($ http_client);
?>
-------------------------------------------------- ------------------------------
POST
PHP code: ----------------------------------------------- ---------------------------------
PHP
REQUIRE_ONCE ('http.inc');
HEADER ('Content-Type: Text / Plain'); $ FORM = Array (
'Value' => 1,
'Date' => '05 / 20/02 ',
'Date_fmt' => 'US',
'Result' => 1,
'lang' => 'ENG',
'Exch' => 'USD',
'Currency' => 'EUR',
'Format' => 'HTML',
'script' => '../convert/fxdaily',
'dest' => 'get table'
);
$ http_client = new http (http_v11, true);
$ http_client-> host = 'www.oanda.com';
$ status = $ http_client-> post ('/ convert / fxdail', $ form, 'http://www.oanda.com/convert/fxdaila ");
IF ($ status == http_status_ok) {
PRINT ($ http_client-> get_response_body ());
} else {
Print ("An Error Occured While Requesting Your File! / N");
Print_r ($ http_client);
}
$ http_client-> disconnect ();
UNSET ($ http_client);
?>
-------------------------------------------------- ------------------------------
Multipart Post is just an example of a usage, how do you recommend it or look at the structure?
PHP code: ----------------------------------------------- ---------------------------------
PHP
REQUIRE_ONCE ('http.inc');
$ FIELDS = Array ('user' => "guinux",
'Password' => 'mypass',
'lang' => 'us'
);
$ files = array ();
$ files [] = array ('name' => 'myfile1',
'Content-Type' => 'Text / Plain',
'filename' => 'Test1.txt',
'Data' => 'Hello from File 1 !!!'
);
$ files [] = array ('name' => 'myfile2',
'Content-Type' => 'Text / Plain',
'filename' => 'Test2.txt',
'Data' => "Blala Blabla / NBLA BLA"
);
$ http_client = new http (http_v11, false);
$ http_client-> host = 'www.myhost.com';
IF ($ http_client-> multipart_post ('/upload.pl', $ fields, $ file) == http_status_ok)
PRINT ($ http_client-> get_response_body ());
Else
PRINT ('Server Returned Status Code:'. $ http_client-> status);
UNSET ($ http_client);
?>
-------------------------------------------------- ------------------------------
Proxy
PHP code: ----------------------------------------------- ---------------------------------
PHP
/ ************************************************** *******************
* Demonstrates The Use of Requests Via Proxy
*********************************************************** ****************** /
HEADER ('Content-Type: Text / Plain');
REQUIRE_ONCE ('http.inc');
$ http_client = new http (http_v11, false);
$ http_client-> host = 'www.yahoo.com';
$ http_client-> us_proxy ('ns.crs.org.ni', 3128);
IF ($ http_client-> get ('/') == http_status_ok)
Print_r ($ http_client);
Else
Print ('Server Returned'. $ http_client-> status);
UNSET ($ http_client);
?>
-------------------------------------------------- ------------------------------