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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44403540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 16:05:36  来源:igfitidea点击:

Object of class GuzzleHttp\Psr7\Request could not be converted to string

phplaravellaravel-5.4guzzle6psr-7

提问by php dev

I've got an issue for laravel 5.4 when I trying to using guzzleHttp. here is my code.

我在尝试使用 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;
}

and I've got this error on log: Error Exception: Object of class GuzzleHttp\Psr7\Request could not be converted to string

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

what is wrong with my code? please kindly help me. fyi, this error not always happen. sometimes it show this error, sometimes success.

我的代码有什么问题?请帮助我。仅供参考,此错误并不总是发生。有时会显示此错误,有时会显示成功。

thank you

谢谢你

回答by Exprator

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

try this

尝试这个

回答by Farhad Hossen

Try this.....

尝试这个.....

    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();
    }

回答by James Mark Saphani

$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`