【问题标题】:Pass array in post request using Guzzle使用 Guzzle 在 post 请求中传递数组
【发布时间】:2017-12-11 16:53:14
【问题描述】:

我正在尝试在发布请求有效负载中传递数组。

url = http://IP:PortNo/index.php/v1/login

有效载荷:

data={ "terminal_id": "terminal_id", "api_login": "api_login", "api_key": "api_key", "merchant_code": "merchant_code" }

我所做的是:

$data = ['terminal_id' => $this->terminal_id , 'api_login' => $this->api_login,
        'api_key' => $this->api_key , 'merchant_code' => $this->merchant_code];
$response = $this->client->post($url, array('data' => $data));

但我得到的回应是:

“未找到请求数据”

这意味着我的数据负载格式不正确。有什么想法吗?

【问题讨论】:

  • 您是否尝试过使用 json_encode 将 $data 编码为 json?我过去曾做过类似[ 'data' => json_encode($data) ] 之类的事情,为了发送正确的正文。
  • 是的,我滴滴 j$response = $this->client->post( $url, ['data' => json_encode($data) ] ); 但我得到了相同的结果 Request data not found
  • 可能是$this->client->post($url [ 'body' => json_encode([ 'data' => $data ]) ]); 的形式?也许看看这个:stackoverflow.com/questions/22244738/…

标签: php guzzle guzzle6


【解决方案1】:
$response = $this->client->post($url, ['json' => $data]);

请参阅 Guzzle6 手册中标题为 json 的部分:http://docs.guzzlephp.org/en/stable/request-options.html#json

【讨论】:

    猜你喜欢
    • 2018-03-21
    • 1970-01-01
    • 2020-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 1970-01-01
    相关资源
    最近更新 更多