<?php
class
SinCookie {
public
$name
;
public
$value
;
public
$expires
;
public
$path
;
public
$domain
;
function
__construct(
$s
= false) {
if
(
$s
) {
$i1
=
strpos
(
$s
, '=');
$i2
=
strpos
(
$s
, ';');
$this
->name = trim(
substr
(
$s
, 0,
$i1
));
$this
->value = trim(
substr
(
$s
,
$i1
+1,
$i2
-
$i1
-1));
}
}
function
getKeyValue() {
return
"$this->name=$this->value"
;
}
}
class
SinHttpContext {
public
$cookies
;
public
$referer
;
function
__construct() {
$this
->cookies =
array
();
$this
->refrer =
""
;
}
function
cookie(
$key
,
$val
) {
$ck
=
new
SinCookie();
$ck
->name =
$key
;
$ck
->value =
$val
;
$this
->addCookie(
$ck
);
}
function
addCookie(
$ck
) {
$this
->cookies[
$ck
->name] =
$ck
;
}
function
cookiesString() {
$res
= '';
foreach
(
$this
->cookies
as
$ck
) {
$res
.=
$ck
->getKeyValue() . ';';
}
return
$res
;
}
}
class
SinHttpRequest {
public
$url
;
public
$method
= 'GET';
public
$host
;
public
$path
;
public
$scheme
;
public
$port
;
public
$header
;
public
$body
;
function
setHeader(
$k
,
$v
) {
if
(!isset (
$this
->header)) {
$this
->header =
array
();
}
$this
->header[
$k
] =
$v
;
}
function
reqString() {
$matches
=
parse_url
(
$this
->url);
!isset (
$matches
['host']) &&
$matches
['host'] = '';
!isset (
$matches
['path']) &&
$matches
['path'] = '';
!isset (
$matches
['query']) &&
$matches
['query'] = '';
!isset (
$matches
['port']) &&
$matches
['port'] = '';
$host
=
$matches
['host'];
$path
=
$matches
['path'] ?
$matches
['path'] . (
$matches
['query'] ? '?' .
$matches
['query'] : '') : '/';
$port
= !
empty
(
$matches
['port']) ?
$matches
['port'] : 80;
$scheme
=
$matches
['scheme'] ?
$matches
['scheme'] : 'http';
$this
->host =
$host
;
$this
->path =
$path
;
$this
->scheme =
$scheme
;
$this
->port =
$port
;
$method
=
strtoupper
(
$this
->method);
$res
=
"$method $path HTTP/1.1\r\n"
;
$res
.=
"Host: $host\r\n"
;
if
(
$this
->header) {
reset(
$this
->header);
while
(list (
$k
,
$v
) = each(
$this
->header)) {
if
(isset (
$v
) &&
strlen
(
$v
) > 0)
$res
.=
"$k: $v\r\n"
;
}
}
$res
.=
"\r\n"
;
if
(
$this
->body) {
$res
.=
$this
->body;
$res
.=
"\r\n\r\n"
;
}
return
$res
;
}
}
class
SinHttpResponse {
public
$scheme
;
public
$stasus
;
public
$code
;
public
$header
;
public
$body
;
function
__construct() {
$this
->header =
array
();
$this
->body = null;
}
function
setHeader(
$key
,
$val
) {
$this
->header[
$key
] =
$val
;
}
}
class
SinHttpClient {
public
$keepcontext
= true;
public
$context
;
public
$request
;
public
$response
;
public
$debug
= false;
function
__construct() {
$this
->request =
new
SinHttpRequest();
$this
->response =
new
SinHttpResponse();
$this
->context =
new
SinHttpContext();
$this
->timeout = 15;
}
function
clearRequest() {
$this
->request->body = '';
$this
->request->setHeader('Content-Length', false);
$this
->request->setHeader('Content-Type', false);
}
function
post(
$url
,
$data
= false) {
$this
->clearRequest();
if
(
$data
) {
if
(
is_array
(
$data
)) {
$con
= http_build_query(
$data
);
$this
->request->setHeader('Content-Type', 'application/x-www-form-urlencoded');
}
else
{
$con
=
$data
;
$this
->request->setHeader('Content-Type', 'text/xml; charset=utf-8');
}
$this
->request->body =
$con
;
$this
->request->method =
"POST"
;
$this
->request->setHeader('Content-Length',
strlen
(
$con
));
}
$this
->startRequest(
$url
);
}
function
get(
$url
) {
$this
->clearRequest();
$this
->request->method =
"GET"
;
$this
->startRequest(
$url
);
}
function
startRequest(
$url
) {
$this
->request->url =
$url
;
if
(
$this
->keepcontext) {
$this
->request->setHeader('Referer',
$this
->context->refrer);
$cks
=
$this
->context->cookiesString();
if
(
strlen
(
$cks
) > 0)
$this
->request->setHeader('Cookie',
$cks
);
}
$reqstring
=
$this
->request->reqString();
if
(
$this
->debug)
echo
"Request:\n$reqstring\n"
;
try
{
$fp
=
fsockopen
(
$this
->request->host,
$this
->request->port,
$errno
,
$errstr
,
$this
->timeout);
}
catch
(Exception
$ex
) {
echo
$ex
->getMessage();
exit
(0);
}
if
(
$fp
) {
stream_set_blocking(
$fp
, true);
stream_set_timeout(
$fp
,
$this
->timeout);
fwrite(
$fp
,
$reqstring
);
$status
= stream_get_meta_data(
$fp
);
if
(!
$status
['timed_out']) {
while
(!
feof
(
$fp
)) {
$h
=
fgets
(
$fp
);
if
(
$this
->debug)
echo
$h
;
if
(
$h
&& (
$h
==
"\r\n"
||
$h
==
"\n"
))
break
;
$pos
=
strpos
(
$h
, ':');
if
(
$pos
) {
$k
=
strtolower
(trim(
substr
(
$h
, 0,
$pos
)));
$v
= trim(
substr
(
$h
,
$pos
+1));
if
(
$k
== 'set-cookie') {
if
(
$this
->keepcontext) {
$this
->context->addCookie(
new
SinCookie(
$v
));
}
}
else
{
$this
->response->setHeader(
$k
,
$v
);
}
}
else
{
$preg
= '/^(\S*) (\S*) (.*)$/';
preg_match_all(
$preg
,
$h
,
$arr
);
isset (
$arr
[1][0]) &
$this
->response->scheme = trim(
$arr
[1][0]);
isset (
$arr
[2][0]) &
$this
->response->stasus = trim(
$arr
[2][0]);
isset (
$arr
[3][0]) &
$this
->response->code = trim(
$arr
[3][0]);
}
}
$len
= (int)
$this
->response->header['content-length'];
$res
= '';
while
(!
feof
(
$fp
) &&
$len
> 0) {
$c
=
fread
(
$fp
,
$len
);
$res
.=
$c
;
$len
-=
strlen
(
$c
);
}
$this
->response->body =
$res
;
}
fclose(
$fp
);
$this
->context->refrer =
$url
;
}
}
}
$client
=
new
SinHttpClient();
$client
->get('http:
echo
$client
->response->body;
?>