Juliatzin
Juliatzin

Reputation: 19725

Post Method with Guzzle doesn't work / Laravel 5

I want to execute a scheduled task with laravel, which make a post with a single param.

I already checked with Postman my POST, so, I just have to hit myurl.com with param ID=188888 for instance. I get it working with postman.

So, first, I'm making the Laravel command : check:time, which just performs the post, and then, once I get it working, I will schedule it.

Thing is it appears commands is doing nothing, and I have no error logs.

So, really, it is not so easy to debug it...

Here is my Command Code:

class CheckTime extends Command {

protected $signature = 'check:time {empId}';

protected $description = 'Check your time';

public function handle()
{
    $client = new Client;
    $numEmp = $this->argument('empId');
    $response = $client->post('http://example.com/endpoint.php', ['ID' => $numEmp]);
    var_dump($response);
    }
}

When I print $response, I get:

    class GuzzleHttp\Psr7\Response#495 (6) {
  private $reasonPhrase =>
  string(2) "OK"
  private $statusCode =>
  int(200)
  private $headers =>
  array(6) {
    'date' =>
    array(1) {
      [0] =>
      string(29) "Wed, 01 Jun 2016 00:17:52 GMT"
    }
    'server' =>
    array(1) {
      [0] =>
      string(22) "Apache/2.2.3 (Red Hat)"
    }
    'x-powered-by' =>
    array(1) {
      [0] =>
      string(10) "PHP/5.3.15"
    }
    'content-length' =>
    array(1) {
      [0] =>
      string(3) "146"
    }
    'connection' =>
    array(1) {
      [0] =>
      string(5) "close"
    }
    'content-type' =>
    array(1) {
      [0] =>
      string(24) "text/html; charset=UTF-8"
    }
  }
  private $headerLines =>
  array(6) {
    'Date' =>
    array(1) {
      [0] =>
      string(29) "Wed, 01 Jun 2016 00:17:52 GMT"
    }
    'Server' =>
    array(1) {
      [0] =>
      string(22) "Apache/2.2.3 (Red Hat)"
    }
    'X-Powered-By' =>
    array(1) {
      [0] =>
      string(10) "PHP/5.3.15"
    }
    'Content-Length' =>
    array(1) {
      [0] =>
      string(3) "146"
    }
    'Connection' =>
    array(1) {
      [0] =>
      string(5) "close"
    }
    'Content-Type' =>
    array(1) {
      [0] =>
      string(24) "text/html; charset=UTF-8"
    }
  }
  private $protocol =>
  string(3) "1.1"
  private $stream =>
  class GuzzleHttp\Psr7\Stream#493 (7) {
    private $stream =>
    resource(311) of type (stream)
    private $size =>
    NULL
    private $seekable =>
    bool(true)
    private $readable =>
    bool(true)
    private $writable =>
    bool(true)
    private $uri =>
    string(10) "php://temp"
    private $customMetadata =>
    array(0) {
    }
  }
}

I checked that $numEmp is OK, also, I printed $response and everything seems to be fine

As I said, I also execute the post with Postman, and it works, so, I don't really understand what's going on...

Any idea??

Upvotes: 0

Views: 791

Answers (1)

Juliatzin
Juliatzin

Reputation: 19725

As @Denis Mysenko wisely advised, I tried:

$response->getBody()->getContents() 

and found out that my post was getting a SQL error message.

Solution: to pass form parameters with guzzle, you have to pass it like that:

response = $client->post('http://example.com/endpoint.php', [
'form_params' => [
    'ID' => $empId,
    ...
]
]); 

Upvotes: 1

Related Questions