如何在JSON中访问数组?

时间:2017-05-18 17:28:36

标签: json laravel rest guzzle

我使用Guzzle for Laravel为Webservice发送POST,但是我无法访问数组来发送图片。这是我需要遵循的结构:

{
"title": "xxxxxxxxxxxxxxxxx",
"category_id": "XXXX",
"official_store_id": null,
"pictures": [
  {
"id": "XXXXX",
"url": "http://mlb-s1-p.mlstatic.com/292325-MLB25432321923_032017-O.jpg",
"secure_url": "https://mlb-s1-p.mlstatic.com/292325-MLB25432321923_032017-O.jpg",
"size": "500x500",
"max_size": "500x500",
"quality": ""
}
],
}

我尝试像这样发送:

 $r = $client->request('POST', 'https://api.mercadolibre.com/items?access_token='.$token->erp_access_token, [
                'json' => [
                    'title' => $name,
                    'category_id' => $cat2,
                    'price' => $price,
                    'currency_id' => 'BRL',
                    'available_quantity' => $quantity,
                    'buying_mode' => 'buy_it_now',
                    'listing_type_id' => 'gold_special',
                    'condition' => 'new',
                    'description' => $description,
                    'pictures' => [
                        'url' => $image
                    ]
                ]
            ]);

返回错误:

{"message":"body.invalid_field_types","error":"[invalid property type: [pictures] expected List but was JSONObject value (truncated...)

我阅读了Guzzle文档,但我没有找到任何关于该案例的例子。

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

从此

更改图片值
'pictures' => [
    'url' => $image
]

到这个

'pictures' => [
    ['url' => $image]
]