Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中对空数组(不是null而是空的)进行URL编码,以传递给curl请求的CURLOPT_POSTFIELDS?_Php_Facebook_Curl - Fatal编程技术网

如何在php中对空数组(不是null而是空的)进行URL编码,以传递给curl请求的CURLOPT_POSTFIELDS?

如何在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

我正在用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.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编码

到现在为止我都试过了

  • 尝试使用数组()和“”(单引号)将其分配给有效负载(此处包括有效负载,但为空,fb seerver返回相同错误)
  • 我可以成功发送不包含空数组的其他类型的消息
  • 给出的curl命令是正确的,因为我使用ssh访问从shell执行它
  • 内容类型为多部分/表单数据
  • 我在一次请求中发送数据和文件,可以吗
  • 已尝试在不编码的情况下传递数据-fb错误收件人为空

  • 试图将数组直接传递给POSTFIELDS,但得到了
    {“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的意思