之前一直使用了一段时间php发送请求的curl库,感觉很好用,最近的一个项目模块需要从不同地方的服务器获取用户指定的网站的访问信息,如域名解析时间,建立连接时间,下载时间,网速等信息,这些信息刚好封装在了curl_getinfo函数返回的数组中,可以很好的利用起来。
其实,curl的精华就在于设置CURLOPT,php在这里面融入了所有的内容,完全活用真的是要很大的功夫。我试着封装在了一个类中,仅以后学习参考。也不多说,直接上代码:
测试服务器端:
<?php
/**
*@project Page speed test
*@author OshynSong <dualyangsong@gmail.com>
*@time 2013-9-30
*@copyright All rights reserved!
*/
class PageSpeed
{
/**
*@access private
*@desc The whole info to return
*/
private $speedInfo = array('status' => '1', 'info' => '');
private $curlOpt = array();
private $curlHandle;
public function __construct($opt)
{
$this -> curlHandle = curl_init();
if (!preg_match("/^http(s)?:\/\/([0-9a-z\-]+\.)+(\w)+$/im", $opt['url']))
{
$this -> speedInfo['status'] = '0';
$this -> speedInfo['info'] = 'URL格式错误!';
}
else
$this -> curlOpt[CURLOPT_URL] = $opt['url'];
//设置header
if (isset($opt['header']))
$this -> curlOpt[CURLOPT_HEADER] = $opt['header'];
else
$this -> curlOpt[CURLOPT_HEADER] = false;
//设置port
if (isset($opt['port']))
$this -> curlOpt[CURLOPT_PORT] = $opt['port'];
else
$this -> curlOpt[CURLOPT_PORT] = 80;
//设置timeout
if (isset($opt['timeout_ms']))
$this -> curlOpt[CURLOPT_TIMEOUT_MS] = $opt['timeout_ms'];
else
$this -> curlOpt[CURLOPT_TIMEOUT_MS] = 30000;
//设置protocols
if (isset($opt['protocols']))
{
$this -> curlOpt[CURLOPT_PROTOCOLS] = $opt['protocols'];
$this -> curlOpt[CURLOPT_SSL_VERIFYPEER] = false;
$this -> curlOpt[CURLOPT_SSL_VERIFYHOST] = 1;
}
//设置encoding
if (isset($opt['encoding']))
$this -> curlOpt[CURLOPT_ENCODING] = $opt['encoding'];
else
$this -> curlOpt[CURLOPT_ENCODING] = 'gzip';
//设置http_version
if (isset($opt['http_version']))
$this -> curlOpt[CURLOPT_HTTP_VERSION] = $opt['http_version'];
else
$this -> curlOpt[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_NONE ;
//设置low_speed_limit
if (isset($opt['low_speed_limit']) && intval($opt['low_speed_limit']))
$this -> curlOpt[CURLOPT_LOW_SPEED_LIMIT] = $opt['low_speed_limit'];
else
$this -> curlOpt[CURLOPT_LOW_SPEED_LIMIT] = 100;
//设置low_speed_time
if (isset($opt['low_speed_time']) && intval($opt['low_speed_time']))
$this -> curlOpt[CURLOPT_LOW_SPEED_TIME] = $opt['low_speed_time'];
else
$this -> curlOpt[CURLOPT_LOW_SPEED_TIME] = 1;
//设置referer
if (isset($opt['referer']))
$this -> curlOpt[CURLOPT_REFERER] = $opt['referer'];
else
$this -> curlOpt[CURLOPT_REFERER] = 'http://www.baidu.com';
//设置useragent
if (isset($opt['useragent']))
$this -> curlOpt[CURLOPT_USERAGENT] = $opt['useragent'];
else
$this -> curlOpt[CURLOPT_USERAGENT] = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31';
$this -> curlOpt[CURLOPT_RETURNTRANSFER] = true;
$this -> curlOpt[CURLOPT_HTTPHEADER] = array('Expect:');
}
public function __destruct()
{
curl_close($this -> curlHandle);
}
/**
*@name GetSpeedInfo
*@para array opt
*@return json
*/
public function GetSpeedInfo()
{
if ($this -> speedInfo['status'] === '0')
{
return json_encode($this -> speedInfo);
exit;
}
curl_setopt_array($this -> curlHandle, $this -> curlOpt);
$curlData = curl_exec($this -> curlHandle);
$curlErrno = curl_errno($this -> curlHandle);
$curlError = curl_error($this -> curlHandle);
if ($curlErrno > 0)
{
$this -> speedInfo['status'] = '0';
$this -> speedInfo['info'] = "cURL Error ($curlErrno): $curlError \n";
}
else
{
$this -> speedInfo['status'] = '1';
$this -> speedInfo['info'] = curl_getinfo($this -> curlHandle);
$this -> speedInfo['info']['loc'] = $this -> getLocation($_SERVER['SERVER_ADDR']);
}
return json_encode($this -> speedInfo);
}
}
?>
调用:
$speedObj = new PageSpeed($curlopt);
echo $speedObj -> GetSpeedInfo();
有需要的可以参考~