guzzle client throws exception in laravel

元气小坏坏 提交于 2021-02-17 05:29:07

问题


I am trying to make a http post request in laravel as below

$client = new Client(['debug'=>true,'exceptions'=>false]);
  $res = $client->request('POST', 'http://www.myservice.com/find_provider.php',  [
            'form_params' => [
                'street'=> 'test',
                'apt'=> '',
                'zip'=> 'test',
                'phone'=> 'test',
            ]
        ]);

It return empty response. On debugging ,following exception is occurring

curl_setopt_array(): cannot represent a stream of type Output as a STDIO FILE*

I am using latest version of guzzle.

Any idea how to solve it?.


回答1:


The request() method is returning a GuzzleHttp\Psr7\Response object. To get the actual data that is returned by your service you should use:

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

Now check what you have in $data and if it corresponds to the expected output.

More information on using Guzzle Reponse object here




回答2:


I had to do this

$data = $res->getBody()->getContents();<br>
but also change<br>
$client = new \GuzzleHttp\Client(['verify' => false, 'debug' => true]);<br>
to<br> 
$client = new \GuzzleHttp\Client(['verify' => false]);



回答3:


Here is what i did for my SMS api

use Illuminate\Support\Facades\Http; // Use this on top

// Sample Code
$response = Http::asForm()
           ->withToken(env('SMS_AUTH_TOKEN'))
           ->withOptions([
                         'debug'  => fopen('php://stderr', 'w') // Update This Line
            ])
            ->withHeaders([
                         'Cache-Control' => 'no-cache',
                         'Content-Type'  => 'application/x-www-form-urlencoded',
             ])
             ->post($apiUrl,$request->except('_token'));


来源:https://stackoverflow.com/questions/35901829/guzzle-client-throws-exception-in-laravel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!