GuzzleHttp \ Psr7 \ Request类的对象无法转换为字符串

时间:2017-06-07 04:30:43

标签: php laravel laravel-5.4 guzzle6 psr-7

当我尝试使用guzzleHttp时,我遇到了laravel 5.4的问题。这是我的代码。

use GuzzleHttp\Client;
$url = 'http://example.com';
$client = new Client();

$parameter = ['query' => ['name' => 'xxx', 'address' => 'yyy'], 'headers' => [ 'User-Agent' => 'xxxx', 'exceptions' => false, 'timeout' => 10 ]];
$res = $client->request('GET', $url, $parameter);

if ($res->getStatusCode() == 200)
{
 $json = (string)$res->getBody();
 return $json;
}

我在日志中遇到此错误: 错误异常:类GuzzleHttp \ Psr7 \ Request的对象无法转换为字符串

我的代码出了什么问题?请帮助我。 fyi,这个错误并不总是发生。有时它会显示此错误,有时会成功。

谢谢

3 个答案:

答案 0 :(得分:8)

$json = $res->getBody()->getContents();

试试这个

答案 1 :(得分:0)

试试这个......

    try {
        $parameter = ['query' => ['name' => 'xxx', 'address' => 'yyy'], 'headers' => [ 'User-Agent' => 'xxxx', 'exceptions' => false, 'timeout' => 10 ]];
        $res = $client->request('GET', $url, $parameter);

        if ($res->getStatusCode() == 200)
          {
             return $res->getBody()->getContents();
          }
    }catch(Exception $e){
        echo 'Caught exception: ',  $e->getMessage();
    }

答案 2 :(得分:0)

$response = $client->post('http:yanjye.com3', ['phone' => '00','password' => '5555',]);
        if ($response->getStatusCode() == 200){
            $json = (string)$response->getBody();
            return $json;
            }
         var_dump( $response);
         die();


Hello, brother, I think this is the Good way to  which is Working On both  laravel 5.2[larave 5.2]



i have removed Httm 
[laraver 5.2][1]

and use this code :

  [1]: https://laravel.com/docs/7.x/http-client`