Skip to content

Commit 0c46361

Browse files
committed
Merge pull request #70 from dtynn/feature/reqid
增加reqid、xlog信息
2 parents 97dc48a + e2de893 commit 0c46361

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

qiniu/http.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ function Qiniu_ResponseError($resp) // => $error
8686
}
8787
}
8888
}
89+
$err->Reqid = $reqId;
90+
$err->Details = $details;
8991
return $err;
9092
}
9193

@@ -114,6 +116,8 @@ function Qiniu_Client_do($req) // => ($resp, $error)
114116
CURLOPT_RETURNTRANSFER => true,
115117
CURLOPT_SSL_VERIFYPEER => false,
116118
CURLOPT_SSL_VERIFYHOST => false,
119+
CURLOPT_HEADER => true,
120+
CURLOPT_NOBODY => false,
117121
CURLOPT_CUSTOMREQUEST => 'POST',
118122
CURLOPT_URL => $url['path']
119123
);
@@ -141,11 +145,37 @@ function Qiniu_Client_do($req) // => ($resp, $error)
141145
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
142146
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
143147
curl_close($ch);
144-
$resp = new Qiniu_Response($code, $result);
148+
149+
$responseArray = explode("\r\n\r\n", $result);
150+
$responseArraySize = sizeof($responseArray);
151+
$respHeader = $responseArray[$responseArraySize-2];
152+
$respBody = $responseArray[$responseArraySize-1];
153+
154+
list($reqid, $xLog) = getReqInfo($respHeader);
155+
156+
$resp = new Qiniu_Response($code, $respBody);
145157
$resp->Header['Content-Type'] = $contentType;
158+
$resp->Header["X-Reqid"] = $reqid;
146159
return array($resp, null);
147160
}
148161

162+
function getReqInfo($headerContent) {
163+
$headers = explode("\r\n", $headerContent);
164+
$reqid = null;
165+
$xLog = null;
166+
foreach($headers as $header) {
167+
$header = trim($header);
168+
if(strpos($header, 'X-Reqid') !== false) {
169+
list($k, $v) = explode(':', $header);
170+
$reqid = trim($v);
171+
} elseif(strpos($header, 'X-Log') !== false) {
172+
list($k, $v) = explode(':', $header);
173+
$xLog = trim($v);
174+
}
175+
}
176+
return array($reqid, $xLog);
177+
}
178+
149179
class Qiniu_HttpClient
150180
{
151181
public function RoundTrip($req) // => ($resp, $error)

tests/IoTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ public function setUp()
1414
$this->bucket = getenv("QINIU_BUCKET_NAME");
1515
}
1616

17+
public function testReqid()
18+
{
19+
$key = 'testReqid' . getTid();
20+
list($ret, $err) = Qiniu_PutFile("", $key, __file__, null);
21+
$this->assertNotNull($err);
22+
$this->assertNotNull($err->Reqid);
23+
var_dump($err);
24+
}
25+
1726
public function testPutFile()
1827
{
1928
$key = 'testPutFile' . getTid();

0 commit comments

Comments
 (0)