如何将body编码为facebook图形API

时间:2017-07-30 21:10:21

标签: facebook facebook-graph-api curl facebook-messenger-bot

我正在为facebook图形API开发一个messenger bot,我想发送带有id的批量请求。然而,在某些时候,Facebook只是没有抓住它们。

文档不是很有用(我发现的唯一内容:https://developers.facebook.com/docs/graph-api/making-multiple-requests

重点是,我正在向API发送此类请求(一个批量请求用于演示目的):

网址:https://graph.facebook.com/v2.6/

接头: Content-Type: application/json

身体: access_token=XXXXXXX&batch=[{"method":"POST","headers":{"name":"Content-Type","value":"application/json; charset=UTF-8"},"relative_url":"me/messages","body":"%7B%22recipient%22%3A%7B%22id%22%3A%22YYYYY%22%7D%2C%22message%22%3A%7B%22attachment%22%3A%7B%22type%22%3A%22template%22%2C%22payload%22%3A%7B%22template_type%22%3A%22generic%22%2C%22elements%22%3A%5B%7B%22title%22%3A%22test%22%2C%22subtitle%22%3A%22test%20test%22%2C%22item_url%22%3A%22%22%2C%22image_url%22%3A%22https%3A%2F%2Fdata.fideinc.com%2Fcustom%2Fent_19286B%2Fid_1ABEA3C_image.png%22%2C%22buttons%22%3A%5B%7B%22type%22%3A%22element_share%22%7D%2C%7B%22type%22%3A%22postback%22%2C%22title%22%3A%22%5Cud83e%5Cudd11%20%5Cu0410%5Cu043a%5Cu0446%5Cu0438%5Cu0438%20%5Cu0432%20%5Cu0437%5Cu0430%5Cu0432%5Cu0435%5Cu0434%5Cu0435%5Cu043d%5Cu0438%5Cu0438%22%2C%22payload%22%3A%22offers%22%7D%5D%7D%5D%2C%22image_aspect_ratio%22%3A%22horizontal%22%7D%7D%7D%2C%22tag%22%3Anull%7D","access_token":"XXXXXXX"}]

无论我用于body元素的格式是什么,它只会响应:{​​{1}}

对于每个"{\"error\":{\"message\":\"(#100) The parameter recipient is required\",\"type\":\"OAuthException\",\"code\":100,\"fbtrace_id\":\"AHiM+KytmHB\"}}"元素。

我尝试过的其他body编码:

body

"{\"recipient\":{\"id\":\"YYYYY\"},\"message\":{\"attachment\":{\"type\":\"template\",\"payload\":{\"template_type\":\"generic\",\"elements\":[{\"title\":\"test\",\"subtitle\":\"test test\",\"item_url\":\"\",\"image_url\":\"https://data.fideinc.com/custom/ent_19286B/id_1ABEA3C_image.png\",\"buttons\":[{\"type\":\"element_share\"},{\"type\":\"postback\",\"title\":\"\\ud83e\\udd11 \\u0410\\u043a\\u0446\\u0438\\u0438 \\u0432 \\u0437\\u0430\\u0432\\u0435\\u0434\\u0435\\u043d\\u0438\\u0438\",\"payload\":\"offers\"}]}],\"image_aspect_ratio\":\"horizontal\"}}},\"tag\":null}"

等等

P.S。我不能在我的项目中使用任何外部Facebook SDK,我只需要正确地获得此请求

1 个答案:

答案 0 :(得分:0)

如果有人遇到同样的问题,答案非常简单:使用http_build_query代替json_encode ......