创建一个CURL对象
fsockopen() Module_HttpClient_Driver_Fsock::_create( string $url , int $timeout )
参数列表
参数 类型 描述 默认值 $url
string
URL地址 $timeout
int
超时时间
fsockopen()
protected function _create($url,$timeout)
{
if (false===strpos($url, '://'))
{
preg_match('#^(http(?:s)?\://[^/]+/)#', $_SERVER["SCRIPT_URI"] , $m);
$the_url = $m[1] . ltrim($url,'/');
}
else
{
$the_url = $url;
}
preg_match('#^(http(?:s)?)\://([^/]+)(/.*)$#', $the_url , $m);
$hostname = $m[2];
$uri = $m[3];
list($host, $port) = explode(':', $hostname, 2);
if ($this->ip)
{
$host = $this->ip;
}
if ($m[1]=='https')
{
$host = 'tls://' . $host;
}
if (!$port)
{
if ($m[1]=='https')
{
$port = 443;
}
else
{
$port = 80;
}
}
$ch = fsockopen($host, $port, $errno, $errstr, $timeout);
$header = array
(
'Host' => $hostname ,
'Accept' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
'Connection' => 'close',
);
if ($this->cookies)
{
$header['Cookie'] = is_array($this->cookies)?http_build_query($this->cookies, '', ';'):$this->cookies;
}
if ($this->referer)
{
$header['Referer'] = $this->referer;
}
if ($this->agent)
{
$header['User-Agent'] = $this->agent;
}
elseif (array_key_exists('HTTP_USER_AGENT', $_SERVER))
{
$header['User-Agent'] = $_SERVER['HTTP_USER_AGENT'];
}
if ($this->header)
{
$header = array();
foreach ($this->header as $item)
{
# 防止有重复的header
if (preg_match('#(^[^:]*):(.*)$#', $item,$m))
{
$header[trim($m[1])] = trim($m[2]);
}
}
}
# 设置POST数据
if ($this->_post_data)
{
$vars = (string)$this->_post_data[$the_url];
$header['Content-Length'] = strlen($vars);
$header['Content-Type'] = 'application/x-www-form-urlencoded';
}
$str = $this->method . ' ' . $uri . ' HTTP/1.1'."\r\n";
foreach ($header as $k=>$v)
{
$str .= $k .' :' . str_replace(array("\r","\n"),'',$v) . "\r\n";
}
$str .= "\r\n";
if ($this->_post_data)
{
// 追加POST数据
$str .= $vars;
}
fwrite($ch,$str);
return $ch;
}