From b52072b5134025461bb8bacd5e4f4cbf7de28a0d Mon Sep 17 00:00:00 2001 From: dtynn Date: Tue, 15 Apr 2014 11:44:18 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=A2=9E=E5=8A=A0reqid=E3=80=81xlog?= =?UTF-8?q?=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- qiniu/http.php | 33 ++++++++++++++++++++++++++++++++- tests/IoTest.php | 10 ++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/qiniu/http.php b/qiniu/http.php index ba890e55..6e0910ed 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,38 @@ 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; + $resp->Header["X-Log"] = $xLog; 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..491b0a79 100644 --- a/tests/IoTest.php +++ b/tests/IoTest.php @@ -14,6 +14,16 @@ 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); + $this->assertNotNull($err->Details); + var_dump($err); + } + public function testPutFile() { $key = 'testPutFile' . getTid(); From e2de893c132687b6181ed37d93f3a256bd926371 Mon Sep 17 00:00:00 2001 From: dtynn Date: Tue, 15 Apr 2014 14:35:24 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E8=B5=9E=E4=B8=8D=E5=8A=A0=E5=85=A5xlog?= =?UTF-8?q?=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- qiniu/http.php | 1 - tests/IoTest.php | 1 - 2 files changed, 2 deletions(-) diff --git a/qiniu/http.php b/qiniu/http.php index 6e0910ed..1064abac 100644 --- a/qiniu/http.php +++ b/qiniu/http.php @@ -156,7 +156,6 @@ function Qiniu_Client_do($req) // => ($resp, $error) $resp = new Qiniu_Response($code, $respBody); $resp->Header['Content-Type'] = $contentType; $resp->Header["X-Reqid"] = $reqid; - $resp->Header["X-Log"] = $xLog; return array($resp, null); } diff --git a/tests/IoTest.php b/tests/IoTest.php index 491b0a79..746f1aca 100644 --- a/tests/IoTest.php +++ b/tests/IoTest.php @@ -20,7 +20,6 @@ public function testReqid() list($ret, $err) = Qiniu_PutFile("", $key, __file__, null); $this->assertNotNull($err); $this->assertNotNull($err->Reqid); - $this->assertNotNull($err->Details); var_dump($err); }