如何使用guzzle6和Laravel发送JSON请求

时间:2018-06-18 20:53:29

标签: json laravel api guzzle guzzle6

从API我需要以这种格式发送请求:

Sample Request Body
{
  "first_name": "adam.west",
  "last_name": "adam.west",
  "email_address": "adam.west@example.com",
  "phone": {
    "number": "11111111111",
    "country_code": "UK",
    "phone_type": "Mobile",
    "extension": "124"
  },
  "comment": "comment",
}

所以对于Laravel和Guzzle6,我写这个:

$res1 = $client1->post('https://example.com/someurl', [
    'headers' => [
        'Content-Type' => 'application/json',
        'Authorization' => 'Bearer '.$token,
            ],

   'json' => [

        'first_name' => $FirstName,
        'last_name' => $Surname,
        'email_address' => $Email,
        'phone' => [
            $phone['number'] = $Mobile,
            $phone['country_code'] = $MobileCountryCode,
            $phone['phone_type'] = 'Mobile',
            $phone['extension'] = $request->dialCode,
        ],
        'comment' => $comment,

    ]
]);

一切都很好,除了phone元素 - 发送电话元素的正确方法是什么 - 因为从API响应我收到错误消息"电话不能为空"

0 个答案:

没有答案