使用Guzzle的Post方法不起作用/ Laravel 5

时间:2016-06-01 00:20:27

标签: laravel

我想用laravel执行一个计划任务,它使用一个参数创建一个帖子。

我已经使用Postman检查了我的POST,所以,我只需要使用param ID = 188888打到myurl.com。我让它与邮递员合作。

所以,首先,我正在制作Laravel命令:check:time,只执行帖子,然后,一旦我开始工作,我会安排它。

似乎是命令无效,我没有错误日志。

所以,实际上,调试它并不容易......

这是我的命令代码:

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

当我打印$ response时,我得到:

    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) {
    }
  }
}

我检查了$ numEmp是否正常,我也打印了$ response,一切似乎都很好

正如我所说的那样,我也会与Postman一起执行这个帖子,而且它有效,所以,我真的不明白发生了什么......

任何想法??

1 个答案:

答案 0 :(得分:0)

正如@Denis Mysenko明智地建议的那样,我试过了:

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

并发现我的帖子收到了SQL错误消息。

解决方案:要使用guzzle传递表单参数,您必须像这样传递:

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