2
votes

I am using GuzzleHttp to send request to external api and get response, but the response returned is empty from data. and when i test a uri and parameters in advanced rest client i get a data,So why Guzzle response is empty?! please help me if you can.

here is my code:

public function index($id)
{
    $client = new Client(['base_uri' => 'http://qpeople.me/']);

    $response=$client->post('profileinfo', [
        'json'=>[
           'tshirtID'=>$id
        ]
        ]);

    $body=$response->getBody();
    dd($body);
    return view('profile');
}

this is the response enter image description here

2
I don't know laravel's helper functions, but what do you get when you perform dd($response->getBody()->__toString())?Shaun Bramley

2 Answers

0
votes

Ok,I found the solution by using cURL to send the request and get a response, here is the code:

$url = 'http://qpeople.me/profileinfo';
        $data['tshirtID'] =$id;
        $_data = json_encode($data);
        $headers = array(
            'Content-Type: application/json',
            'Content-Length: ' . strlen($_data),
        );

        $curl = curl_init($url);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $_data);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $response = curl_exec($curl);
0
votes

try:

<?php
$body = (string) $response->getBody();
dd($body);