Commit 907869c5 authored by 杨培's avatar 杨培

php curl 优化

parent d1f52d7b
......@@ -111,8 +111,10 @@ class Client
if ($httpCode == 403 && substr($response, 0, 6) === 'error:') {
$message = substr($response, 6);
throw new ClientException("接口 $api 调用失败: " . $message);
} elseif ($httpCode === 0) {
throw new ClientException('接口请求异常: '.$response, $httpCode);
} elseif ($httpCode != 200 || $response === false) {
throw new ClientException('接口请求异常', $httpCode);
throw new ClientException('接口响应异常: '.$response, $httpCode);
}
$data = @json_decode($response, true);
......@@ -196,6 +198,12 @@ class Client
$response = curl_exec($ci);
$httpCode = curl_getinfo($ci, CURLINFO_HTTP_CODE);
if ($response === false) {
$errno = curl_errno($ci);
$errmsg = curl_error($ci);
$response = "[$errno] $errmsg";
}
//获取调试信息
if ($this->debug) {
$httpInfo = curl_getinfo($ci);
......@@ -204,8 +212,7 @@ class Client
curl_close($ci);
if ($this->debug) {
echo '<pre>';
echo "=====INFO=====\r\n";
echo "<pre>\r\n=====INFO=====\r\n";
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",
......@@ -215,6 +222,10 @@ class Client
"File Time: {$httpInfo['filetime']}\r\n",
"Total Time: {$httpInfo['total_time']}\r\n";
if (isset($errno)) {
echo "\r\n=====ERROR=====\r\nErrorNo: $errno\r\nErrorMsg: $errmsg\r\n";
}
echo "\r\n=====REQUEST=====\r\n",$httpInfo['request_header'];
if (isset($body)) {
......@@ -222,7 +233,7 @@ class Client
}
echo "\r\n=====RESPONSE=====\r\n",$responseHeaders,$response;
echo '</pre>';
echo "\r\n</pre>";
}
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