|
PHP复制代码
class SWHTTP {
private $useType = TRUE;
private $handler = NULL;
public function swinit (){
if(function_exists('curl_init')) {
$this->useType = TRUE;
} else {
$this->useType = FALSE;
}
}
public function Sw_Run_Method ($url, $vars = array(), $cookies = array(), $method = 'POST'){
// 选择使用CURL或者Fsockopen
if($this->useType) {
// 处理 Cookies
$header = array();
if(!empty($cookies)) {
if(is_array($cookies)) {
$encoded = '';
while(list($k, $v)=each($cookies)) {
$encoded .= ($encoded ? ';' : '');
$encoded .= rawurlencode($k).'='.rawurlencode($v);
$header = array('Cookie :'. $encoded);
}
} else if(is_string($cookies)) {
if(strtolower(substr($cookies, 0, 7)) == 'cookie:') {
$header = array($cookies);
} else {
$header = array('Cookie :'. $cookies);
}
}
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL , $url);
curl_setopt($ch, CURLOPT_HTTPHEADER , $header);
if(in_array($method, array('POST'))) {
curl_setopt($ch, CURLOPT_POST , TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS , $vars);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER , 1);
$data = curl_exec($ch);
$data = $data ? $data : 'CURL_ERRROR';
return $data;
} else {
//组织COOKIE数据
$cookie = '';
if (!empty($cookies)){
if (is_array($cookies)){
$encoded = '';
while (list($k,$v) = each($cookies)) {
$encoded .= ($encoded ? ";" : "");
$encoded .= rawurlencode($k)."=".rawurlencode($v);
}
$cookie = $encoded;
} elseif (is_string($cookies)){
if (strtolower(substr($cookies, 0, 7)) == 'cookie:'){
$cookie = substr($cookies, 7);
} else {
$cookie = $cookies;
}
}
}
if($method == 'POST') {
// 处理POST请求
$post = '';
if(!empty($vars)) {
if (is_array($vars)){
$encoded = '';
while (list($k,$v) = each($vars)) {
$encoded .= ($encoded ? "&" : "");
$encoded .= rawurlencode($k)."=".rawurlencode($v);
}
$post = $encoded;
} else {
$post = $vars;
}
}
}
// 处理请求
$url = parse_url($url);
if(strtolower($url['scheme']) != 'http' && $url['scheme'] != '') {
return 'fsockopen error';
}
if(!($fp = @fsockopen($url['host'], $url['port'] ? $url['port'] : 80, $errno, $errstr, 10))) {
return "Fsockopen Error:<br/>No:{$errno}<br/>String:{$errstr}";
}
if($method == 'POST') {
fputs($fp, sprintf("POST %s%s%s HTTP/1.0\n", $url['path'], $url['query'] ? $url['query'] : "", $url['query']));
} else if($method == 'GET') {
fputs($fp, sprintf("GET %s%s%s HTTP/1.0\n", $url['path'], $url['query'] ? $url['query'] : "", $url['query']));
}
fputs($fp, "Host: {$url['host']}\n");
fputs($fp, "User-Agent: HFHttp-Client\n");
if($cookie != '') {
fputs($fp, "Cookie: {$cookie}\n\n");
}
if($method == 'POST') {
fputs($fp, "Content-type: application/x-www-form-urlencoded\n");
fputs($fp, "Content-length: " . strlen($post) . "\n");
fputs($fp, "Connection: close\n\n");
fputs($fp, "$post \n");
} else if($method == 'GET') {
fputs($fp, "Connection: close\n\n");
fputs($fp, "$post \n");
}
$ret = '';
while (!feof($fp)) {
$c = fgets($fp, 1024);
if (preg_match("/^HTTP/", $c) || preg_match("/^P3P/", $c) || preg_match("/^([A-Za-z\-]+)\:\s*(.*)$/", $c)){
continue;
}
if (trim($c) == ""){
continue;
}
$ret .= $c;
}
fclose($fp);
return $ret;
}
}
}
复制代码 |
|