2015-05-30 49 views
82

我正在嘗試圍繞我的公司正在開發的API進行封裝。這是安寧的,使用郵遞員,我可以發送郵件請求到像http://subdomain.dev.myapi.com/api/v1/auth/這樣的端點,用戶名和密碼爲POST數據,並且我收到一個令牌。所有按預期工作。現在,當我嘗試從PHP執行相同的操作時,我得到一個GuzzleHttp\Psr7\Response對象,但似乎無法像在郵遞員請求中那樣在其內部的任何位置找到該令牌。Guzzlehttp - 如何從Guzzle 6獲得響應的身體?

相關的代碼如下所示:

$client = new Client(['base_uri' => 'http://companysub.dev.myapi.com/']); 
$response = $client->post('api/v1/auth/', [ 
    'form_params' => [ 
     'username' => $user, 
     'password' => $password 
    ] 
]); 

var_dump($response); //or $resonse->getBody(), etc... 

以上代碼的輸出看起來像(警告,文字的傳入牆):

object(guzzlehttp\psr7\response)#36 (6) { 
    ["reasonphrase":"guzzlehttp\psr7\response":private]=> 
    string(2) "ok" 
    ["statuscode":"guzzlehttp\psr7\response":private]=> 
    int(200) 
    ["headers":"guzzlehttp\psr7\response":private]=> 
    array(9) { 
    ["connection"]=> 
    array(1) { 
     [0]=> 
     string(10) "keep-alive" 
    } 
    ["server"]=> 
    array(1) { 
     [0]=> 
     string(15) "gunicorn/19.3.0" 
    } 
    ["date"]=> 
    array(1) { 
     [0]=> 
     string(29) "sat, 30 may 2015 17:22:41 gmt" 
    } 
    ["transfer-encoding"]=> 
    array(1) { 
     [0]=> 
     string(7) "chunked" 
    } 
    ["content-type"]=> 
    array(1) { 
     [0]=> 
     string(16) "application/json" 
    } 
    ["allow"]=> 
    array(1) { 
     [0]=> 
     string(13) "post, options" 
    } 
    ["x-frame-options"]=> 
    array(1) { 
     [0]=> 
     string(10) "sameorigin" 
    } 
    ["vary"]=> 
    array(1) { 
     [0]=> 
     string(12) "cookie, host" 
    } 
    ["via"]=> 
    array(1) { 
     [0]=> 
     string(9) "1.1 vegur" 
    } 
    } 
    ["headerlines":"guzzlehttp\psr7\response":private]=> 
    array(9) { 
    ["connection"]=> 
    array(1) { 
     [0]=> 
     string(10) "keep-alive" 
    } 
    ["server"]=> 
    array(1) { 
     [0]=> 
     string(15) "gunicorn/19.3.0" 
    } 
    ["date"]=> 
    array(1) { 
     [0]=> 
     string(29) "sat, 30 may 2015 17:22:41 gmt" 
    } 
    ["transfer-encoding"]=> 
    array(1) { 
     [0]=> 
     string(7) "chunked" 
    } 
    ["content-type"]=> 
    array(1) { 
     [0]=> 
     string(16) "application/json" 
    } 
    ["allow"]=> 
    array(1) { 
     [0]=> 
     string(13) "post, options" 
    } 
    ["x-frame-options"]=> 
    array(1) { 
     [0]=> 
     string(10) "sameorigin" 
    } 
    ["vary"]=> 
    array(1) { 
     [0]=> 
     string(12) "cookie, host" 
    } 
    ["via"]=> 
    array(1) { 
     [0]=> 
     string(9) "1.1 vegur" 
    } 
    } 
    ["protocol":"guzzlehttp\psr7\response":private]=> 
    string(3) "1.1" 
    ["stream":"guzzlehttp\psr7\response":private]=> 
    object(guzzlehttp\psr7\stream)#27 (7) { 
    ["stream":"guzzlehttp\psr7\stream":private]=> 
    resource(40) of type (stream) 
    ["size":"guzzlehttp\psr7\stream":private]=> 
    null 
    ["seekable":"guzzlehttp\psr7\stream":private]=> 
    bool(true) 
    ["readable":"guzzlehttp\psr7\stream":private]=> 
    bool(true) 
    ["writable":"guzzlehttp\psr7\stream":private]=> 
    bool(true) 
    ["uri":"guzzlehttp\psr7\stream":private]=> 
    string(10) "php://temp" 
    ["custommetadata":"guzzlehttp\psr7\stream":private]=> 
    array(0) { 
    } 
    } 
} 

從郵差的輸出是這樣的:

{ 
    "data" : { 
     "token" "fasdfasf-asfasdfasdf-sfasfasf" 
    } 
} 

很明顯我錯過了一些關於使用響應對象的問題zzle。 Guzzle響應表明請求中有200個狀態碼,所以我不確定我需要做什麼來檢索返回的數據。

+19

'$響應 - > getBody() - > getContents()'不工作? – Federkun

+3

你美麗的天才@Leggendario。你在你自己的時代是一個傳奇。我不知道我以前沒有找到'getContents()'。 – Greg

回答

241

Gu implements工具PSR-7。這意味着它將默認將消息正文存儲在使用PHP臨時流的Stream中。要檢索所有的數據,你可以使用轉換操作符:

$contents = (string) $response->getBody(); 

您還可以

$contents = $response->getBody()->getContents(); 

這兩種方法之間的區別是做這種getContents返回剩餘的內容,使第二除非您通過rewindseek尋找信息流的位置,否則通話將不會返回任何內容。

$stream = $response->getBody(); 
$contents = $stream->getContents(); // returns all the contents 
$contents = $stream->getContents(); // empty string 
$stream->rewind(); // Seek to the beginning 
$contents = $stream->getContents(); // returns all the contents 

相反,usings PHP的字符串鑄造操作,直至達到最終它會讀取從一開始就流中的所有數據。

$contents = (string) $response->getBody(); // returns all the contents 
$contents = (string) $response->getBody(); // returns all the contents 

文檔:http://docs.guzzlephp.org/en/latest/psr7.html#responses

+4

getContents函數僅在Guzzle 6文檔的一小部分中(在流部分中),我錯過了它。你從一大堆搜索中拯救了我。 – Maximus

+26

謝謝。令人難以置信的是,這在文檔中不是很清楚。甚至他們的官方文檔(http://docs.guzzlephp.org/en/latest/)也使得它看起來像調用$ res-> getBody()返回你通常期望的。 – John

+9

他們確實應該在正式文件中加上一條註釋或通知。我在這個問題上浪費了兩天時間。 – cwhsu