2017-01-07 1 views
1

response.txtの内容です:Guzz 6を使ってファイルから応答を模擬する方法は?ここ

HTTP/1.1 200 OK 
Server: nginx 
Date: Fri, 15 Feb 2016 18:25:28 GMT 
Content-Type: application/json;charset=utf-8 

{ 
    "field1": "a", 
    "field2": "b", 
} 

私が試した:

$stream = Psr7\stream_for(file_get_contents('response.txt')); 
$response = new Response(200, ['Content-Type' => 'application/json'], $stream); 
dd($response->getBody()); 

をどの出力:だから私は行う方法、response.txtにJSONコンテンツを取得することはできません

object(GuzzleHttp\Psr7\Stream)#3 (7) { 
    ["stream":"GuzzleHttp\Psr7\Stream":private]=> 
    resource(26) 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) { 
    } 
} 

それ?私が欲しいのは次のようなものです:

array('field1'=>'a','field2'=>'b'); 

答えて

1

Responseクラスのコンストラクタの3番目のパラメータはボディ文字列でなければなりません。 Guzz Responseクラスのドキュメントhttp://docs.guzzlephp.org/en/latest/psr7.html#guzzle-and-psr-7を参照してください。

次のコードは動作するはずです

$stream = file_get_contents('response.txt'); 
$response = new Response(200, ['Content-Type' => 'application/json'], $stream); 
dd($response->getBody()); 
+0

は実際に私が使用し、 '$対応 - > getBody() - >のgetContent()' – Phoenix

+0

のgetContentsストリームのみで、残りを返す私の問題を解決します。体全体を安全に取得したい場合は、文字列にキャストする必要があります。 –

関連する問題