指定したWebサイトへのアクセス情報の取得
4772 ワード
以前からphpでリクエストを送信するcurlライブラリをしばらく使用していましたが、使いやすい感じでした.最近のプロジェクトモジュールでは、ドメイン名解析時間、接続確立時間、ダウンロード時間、ネット速度などの情報を異なる場所のサーバから取得する必要があります.これらの情報はcurl_にカプセル化されています.getinfo関数が返す配列では,うまく利用できる.
実は、curlのエッセンスはCURLOPTを設置することにあり、phpはこの中にすべての内容を溶け込んでおり、完全に活用するには本当に大きな工夫が必要です.一つのクラスにカプセル化してみましたが、後で勉強するだけで参考になります.あまり言わないで、直接コードをつけます.
サーバ側のテスト:
クライアントがユーザ入力アドレスを取得した後,サーバ側に情報の取得を要求する.
必要なものがあれば参考にしてください.
実は、curlのエッセンスはCURLOPTを設置することにあり、phpはこの中にすべての内容を溶け込んでおり、完全に活用するには本当に大きな工夫が必要です.一つのクラスにカプセル化してみましたが、後で勉強するだけで参考になります.あまり言わないで、直接コードをつけます.
サーバ側のテスト:
<?php
/**
*@project Page speed test
*@author OshynSong <[email protected]>
*@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
";
}
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();
クライアントがユーザ入力アドレスを取得した後,サーバ側に情報の取得を要求する.
必要なものがあれば参考にしてください.