user269867
user269867

Reputation: 3982

guzzle client throws exception in laravel

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?.

Upvotes: 0

Views: 4580

Answers (3)

Vipertecpro
Vipertecpro

Reputation: 3284

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'));

Upvotes: 0

SamChad
SamChad

Reputation: 1

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]);

Upvotes: 0

iivannov
iivannov

Reputation: 4411

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

Upvotes: 2

Related Questions