diff --git a/qiniu/http.php b/qiniu/http.php index ba890e55..1064abac 100644 --- a/qiniu/http.php +++ b/qiniu/http.php @@ -86,6 +86,8 @@ function Qiniu_ResponseError($resp) // => $error } } } + $err->Reqid = $reqId; + $err->Details = $details; return $err; } @@ -114,6 +116,8 @@ function Qiniu_Client_do($req) // => ($resp, $error) CURLOPT_RETURNTRANSFER => true, CURLOPT_SSL_VERIFYPEER => false, CURLOPT_SSL_VERIFYHOST => false, + CURLOPT_HEADER => true, + CURLOPT_NOBODY => false, CURLOPT_CUSTOMREQUEST => 'POST', CURLOPT_URL => $url['path'] ); @@ -141,11 +145,37 @@ function Qiniu_Client_do($req) // => ($resp, $error) $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); $contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE); curl_close($ch); - $resp = new Qiniu_Response($code, $result); + + $responseArray = explode("\r\n\r\n", $result); + $responseArraySize = sizeof($responseArray); + $respHeader = $responseArray[$responseArraySize-2]; + $respBody = $responseArray[$responseArraySize-1]; + + list($reqid, $xLog) = getReqInfo($respHeader); + + $resp = new Qiniu_Response($code, $respBody); $resp->Header['Content-Type'] = $contentType; + $resp->Header["X-Reqid"] = $reqid; return array($resp, null); } +function getReqInfo($headerContent) { + $headers = explode("\r\n", $headerContent); + $reqid = null; + $xLog = null; + foreach($headers as $header) { + $header = trim($header); + if(strpos($header, 'X-Reqid') !== false) { + list($k, $v) = explode(':', $header); + $reqid = trim($v); + } elseif(strpos($header, 'X-Log') !== false) { + list($k, $v) = explode(':', $header); + $xLog = trim($v); + } + } + return array($reqid, $xLog); +} + class Qiniu_HttpClient { public function RoundTrip($req) // => ($resp, $error) diff --git a/tests/IoTest.php b/tests/IoTest.php index fea4fa46..746f1aca 100644 --- a/tests/IoTest.php +++ b/tests/IoTest.php @@ -14,6 +14,15 @@ public function setUp() $this->bucket = getenv("QINIU_BUCKET_NAME"); } + public function testReqid() + { + $key = 'testReqid' . getTid(); + list($ret, $err) = Qiniu_PutFile("", $key, __file__, null); + $this->assertNotNull($err); + $this->assertNotNull($err->Reqid); + var_dump($err); + } + public function testPutFile() { $key = 'testPutFile' . getTid();