创建一个CURL对象
curl_init() Module_HttpClient_Driver_Curl::_create( string $url , int $timeout )
参数列表
参数 类型 描述 默认值 $url
string
URL地址 $timeout
int
超时时间
curl_init()
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;
}
if ($this->ip)
{
# 如果设置了IP,则把URL替换,然后设置Host的头即可
if (preg_match('#^(http(?:s)?)\://([^/\:]+)(\:[0-9]+)?/#', $the_url.'/',$m))
{
$this->header[] = 'Host: '.$m[2];
$the_url = $m[1].'://'.$this->ip.$m[3].'/'.substr($the_url,strlen($m[0]));
}
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $the_url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, HttpClient_Driver_Curl::$connecttimeout_ms);
if ( preg_match('#^https://#i', $the_url) )
{
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
}
if ($this->cookies)
{
if (is_array($this->cookies))
{
curl_setopt($ch, CURLOPT_COOKIE, http_build_query($this->cookies, '', ';'));
}
else
{
curl_setopt($ch, CURLOPT_COOKIE, $this->cookies);
}
}
if ($this->referer)
{
curl_setopt($ch, CURLOPT_REFERER, $this->referer);
}
if ($this->agent)
{
curl_setopt($ch, CURLOPT_USERAGENT, $this->agent);
}
elseif (array_key_exists('HTTP_USER_AGENT', $_SERVER))
{
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
}
foreach ($this->_option as $k => $v)
{
curl_setopt($ch, $k, $v);
}
if ( $this->header )
{
$header = array();
foreach ($this->header as $item)
{
# 防止有重复的header
if (preg_match('#(^[^:]*):.*$#', $item,$m))
{
$header[$m[1]] = $item;
}
}
curl_setopt($ch, CURLOPT_HTTPHEADER, array_values($header));
}
# 设置POST数据
if (isset($this->_post_data[$url]))
{
curl_setopt($ch , CURLOPT_POSTFIELDS , $this->_post_data[$url]);
}
return $ch;
}