6

当我尝试使用 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 类的对象无法转换为字符串

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

谢谢你

4

3 回答 3

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

尝试这个

于 2017-06-07T04:44:21.000 回答
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`
于 2020-04-12T09:42:52.463 回答
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();
    }
于 2017-06-07T05:26:43.780 回答