Guzzle服务描述json数组

时间:2018-01-13 19:38:17

标签: php rest guzzle guzzlehttp

如何将Guzzle服务描述中的参数设置为json数组? 这是我尝试但不起作用的地方:

$service_description = new Description([
                    'baseUri' => 'http://api.etrackerplus.com/api',
                    'operations' => [

                        'createOrder' => [
                            'httpMethod' => 'POST',
                            'uri' => 'api/stores/{store_id}/orders/new.json',
                            'responseModel' => 'getResponse',
                            'parameters' => [
                                'store_id' => [
                                    'type' => 'string',
                                    'location' => 'uri'
                                ],
                                "order" => [//            <== HERE!!!
                                    "location" => "json",
                                    "type" => "array"
                                ]
                            ]
                        ]


                    ],
                    'models' => [
                        'getResponse' => [
                            'type' => 'object',
                            'additionalProperties' => [
                                'location' => 'json'
                            ]
                        ]
                    ]
    ]);

呼叫:

$response = $client->createOrder(['store_id' => '23',array( 'order'=>
        array("order_number" => "1233"))]);

要发送的json正确的json结构是:

{ 
 "order":  {
      "order_number": "97221"
  } 
}

1 个答案:

答案 0 :(得分:0)

您必须根据JSON模式验证规则编写nested定义。

我已经使用基于XML的请求完成了此操作。让我在基于JSON的数据中尝试一下。希望这会有所帮助:

    "order" => [
        "location" => "json",
        "type" => "object", // Because it's associative array in PHP but object in JS. Use "array" only if it's indexed array
        "properties" => [ // define nested property order_number (by default type is string
            "order_number" => [
                "location" => "json",
            ],
        ],
    ],