如何在php中对空数组(不是null而是空的)进行URL编码,以传递给curl请求的CURLOPT_POSTFIELDS?
我正在用php创建Fb messenger机器人,并使用curl与Fb服务器交互。现在,为了发送文件,fb希望我以这种格式发送请求如何在php中对空数组(不是null而是空的)进行URL编码,以传递给curl请求的CURLOPT_POSTFIELDS?,php,facebook,curl,Php,Facebook,Curl,我正在用php创建Fb messenger机器人,并使用curl与Fb服务器交互。现在,为了发送文件,fb希望我以这种格式发送请求 ` curl \ -F recipient='{"id":"USER_ID"}' \ -F message='{"attachment":{"type":"image", "payload":{}}}' \ -F filedata=@/tmp/shirt.png;type=image/png \ "https://graph.fac
` curl \
-F recipient='{"id":"USER_ID"}' \
-F message='{"attachment":{"type":"image", "payload":{}}}' \
-F filedata=@/tmp/shirt.png;type=image/png \
"https://graph.facebook.com/v2.6/me/messages?access_token=PAGE_ACCESS_TOKEN" `
在这里您可以看到有效负载是一个空数组。
我正在做的是
$res['message'] = [
'attachment' => [
'type' => 'image',
'payload' => [
]
]
]; `
在这之后,我像这样使用curl_setopt
curl_setopt($process, CURLOPT_POST, 1);
curl_setopt($process, CURLOPT_BINARYTRANSFER , true);
curl_setopt($process,CURLOPT_POSTFIELDS, http_build_query($data));
但这不起作用,我越来越紧张
{"error":{"message":"(#100) The parameter message[attachment] [payload] is required","type":"OAuthException","code":100,"fbtrace_id":"EaQ66DTssrV"}}
经过一些调试,我得出结论,因为我的附件数组是空的http\u build\u query
没有考虑它。
先看
所以我的问题是如何在php中对空数组进行URL编码
到现在为止我都试过了
{“error”:{“message”:“(#100)参数收件人必须为非空。”,“type”:“OAuthException”,“code”:100,“fbtrace_id”:“BQ00BtZZmPF”}
如果有更好的方法在php中执行这个curl命令,请告诉我。谢谢注意:您可以将数组传递给
CURLOPT\u POSTFIELDS
hi我尝试传递$arr=array('recipient'=>array('id'=>'id'),'message'=>array('attachment'=>array('type'=>image','payload'=>array()),'filedata'=>'@/home/public\html/kapil/webhook/download.jpg;type=image/jpeg');但是它给出了receipent不能为空的错误,你为什么要自己用cURL来做这件事,而不是仅仅使用PHPSDK…?事实上我想我使用的是PHPSDK。我使用curl\u execute来执行语句,问题在于curl\u set\u opt命令。我希望这就是您使用php sdkNote的意思:您可以将数组传递给CURLOPT_POSTFIELDS
hi我尝试传递$arr=array('recipient'=>array('id'=>'id'),'message'=>array('attachment'=>array('type'=>image','payload'=>array()),'filedata'=>'@/home/public\html/kapil/webhook/download.jpg;type=image/jpeg');但是它给出了receipent不能为空的错误,你为什么要自己用cURL来做这件事,而不是仅仅使用PHPSDK…?事实上我想我使用的是PHPSDK。我使用curl\u execute来执行语句,问题在于curl\u set\u opt命令。我希望这就是您使用PHPSDK的意思