Commit 2585769e authored by pader's avatar pader

优化了 Client 的请求,优化了调试模式的输出信息

parent d4a960fa
...@@ -143,47 +143,86 @@ class Client ...@@ -143,47 +143,86 @@ class Client
} }
protected function request($url, $post='', $headers=[]) { protected function request($url, $post='', $headers=[]) {
$ci = curl_init(); $ci = curl_init($url);
/* Curl settings */
curl_setopt($ci, CURLOPT_USERAGENT, 'PHP/'.PHP_VERSION.' Client/'.$this->clientId);
curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ci, CURLOPT_TIMEOUT, 8);
curl_setopt($ci, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ci, CURLOPT_HEADER, FALSE);
//添加客户端信息以辨服务端识别和调试
$userAgent = 'PHP/'.PHP_VERSION.' ('.PHP_OS.') Client/'.$this->clientId;
if (isset($_SERVER['HTTP_HOST'])) {
$userAgent .= ' ('.$_SERVER['HTTP_HOST'].')';
}
$curl = curl_version();
$userAgent .= " cURL/{$curl['version']} ({$curl['ssl_version']})";
//基础设置
curl_setopt_array($ci, [
CURLOPT_CONNECTTIMEOUT => 5,
CURLOPT_TIMEOUT => 8,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => false,
CURLOPT_HEADER => false,
CURLOPT_USERAGENT => $userAgent
]);
//POST 请求
if ($post) { if ($post) {
curl_setopt($ci, CURLOPT_POST, TRUE); $body = is_array($post) ? http_build_query($post) : $post;
$post = is_array($post) ? http_build_query($post) : $post; curl_setopt_array($ci, [
$post && curl_setopt($ci, CURLOPT_POSTFIELDS, $post); CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $body
]);
} }
$header[] = 'Expect:'; //优化
$headers[] = 'Expect:';
curl_setopt($ci, CURLOPT_URL, $url); curl_setopt_array($ci, [
curl_setopt($ci, CURLOPT_HTTPHEADER, $headers); CURLOPT_HTTPHEADER => $headers,
//curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE); CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_0,
//curl_setopt($ci, CURLOPT_HEADERFUNCTION, array($this, 'getHeader')); CURLOPT_IPRESOLVE => CURL_IPRESOLVE_V4
]);
$response = curl_exec($ci); //调试
if ($this->debug) {
curl_setopt($ci, CURLINFO_HEADER_OUT, true);
$responseHeaders = '';
curl_setopt($ci, CURLOPT_HEADERFUNCTION, function ($ci, $header) use (&$responseHeaders) {
$responseHeaders .= $header;
return strlen($header);
});
}
$response = curl_exec($ci);
$httpCode = curl_getinfo($ci, CURLINFO_HTTP_CODE); $httpCode = curl_getinfo($ci, CURLINFO_HTTP_CODE);
$this->debug && $httpInfo = curl_getinfo($ci);
//获取调试信息
if ($this->debug) {
$httpInfo = curl_getinfo($ci);
}
curl_close($ci); curl_close($ci);
if ($this->debug) { if ($this->debug) {
echo '<pre>'; echo '<pre>';
echo '=====info====='."\r\n"; echo "=====INFO=====\r\n";
print_r($httpInfo); echo "Remote Address: {$httpInfo['primary_ip']}:{$httpInfo['primary_port']}\r\n",
"Local Address: {$httpInfo['local_ip']}:{$httpInfo['local_port']}\r\n",
"Namelookup Time: {$httpInfo['namelookup_time']}\r\n",
"Connect Time: {$httpInfo['connect_time']}\r\n",
"Pre Transfer Time: {$httpInfo['pretransfer_time']}\r\n",
"Start Transfer Time: {$httpInfo['starttransfer_time']}\r\n",
"File Time: {$httpInfo['filetime']}\r\n",
"Total Time: {$httpInfo['total_time']}\r\n";
echo "\r\n=====REQUEST=====\r\n",$httpInfo['request_header'];
echo "\r\n=====post data======\r\n"; if (isset($body)) {
var_dump($post); echo $body."\r\n";
}
echo "\r\n=====response=====\r\n"; echo "\r\n=====RESPONSE=====\r\n",$responseHeaders,$response;
print_r($response);
echo '</pre>'; echo '</pre>';
exit;
} }
return [$httpCode, $response]; return [$httpCode, $response];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment