<?php
header(
"content-type:text/html;charset=utf-8"
);
class
Collect
{
public
$userAgent
= NULL;
protected
$url
;
protected
$object
;
protected
$curlInfo
=
array
();
protected
function
_init(
$url
=
''
,
$issetcookie
=FALSE,
$isfollow
=FALSE)
{
$this
->url =
$url
;
$this
->object = curl_init(
$this
->url);
date_default_timezone_set(
'PRC'
);
curl_setopt(
$this
->object, CURLOPT_RETURNTRANSFER, 1);
curl_setopt(
$this
->object, CURLOPT_HEADER, 0);
if
(isset(
$this
->userAgent))
{
curl_setopt(
$this
->object,CURLOPT_USERAGENT,
$this
->userAgent);
}
if
(
$issetcookie
)
{
curl_setopt(
$this
->object, CURLOPT_COOKIESESSION, TRUE);
curl_setopt(
$this
->object, CURLOPT_COOKIEFILE,
"cookiefile"
);
curl_setopt(
$this
->object, CURLOPT_COOKIEJAR,
"cookiefile"
);
}
if
(
$isfollow
)
{
curl_setopt(
$this
->object, CURLOPT_FOLLOWLOCATION,1);
}
}
public
function
curlGet(
$url
,
$pearm
=
''
)
{
$str
=
''
;
if
(
is_array
(
$pearm
))
{
foreach
(
$pearm
as
$key
=>
$value
) {
if
(
empty
(
$str
))
{
$str
.=
'?'
.
$key
.
'='
.
$value
;
}
else
{
$str
.=
'&'
.
$key
.
'='
.
$value
;
}
}
}
else
{
$str
=
'?'
.
$pearm
;
}
$this
->_init(
$url
.
$str
);
$content
=
$this
->curlExec(
$this
->object);
return
$content
;
}
public
function
curlPost(
$url
,
$pearm
=
''
)
{
$this
->_init(
$url
);
curl_setopt(
$this
->object, CURLOPT_POST, 1);
curl_setopt(
$this
->object, CURLOPT_POSTFIELDS,
$pearm
);
curl_setopt(
$this
->object, CURLOPT_HTTPHEADER,
array
(
'application/x-www-form-urlencoded'
,
'Content-length: '
.
strlen
(
$pearm
)));
$content
=
$this
->curlExec(
$this
->object);
return
$content
;
}
public
function
getInfo()
{
return
curl_getinfo(
$this
->object);
}
public
function
__destruct()
{
curl_close(
$this
->object);
}
protected
function
curlExec(
$object
)
{
$info
= curl_exec(
$this
->object);
if
(curl_errno(
$this
->object))
{
return
'Curl error: '
. curl_error(
$this
->object);
}
else
{
return
$info
;
}
}
}