Guzzle错误100向Facebook Messenger Bot平台发送请求

时间:2016-10-11 06:52:16

标签: php guzzle facebook-messenger

我正在使用php开发一个Facebook Messenger机器人 我想按正式文档中的说明设置问候语文本:Greeting text

curl -X POST -H "Content-Type: application/json" -d '{
  "setting_type":"greeting",
  "greeting":{
    "text":"New message."
  }
}' "https://graph.facebook.com/v2.6/me/thread_settings?access_token=my_token"

上面的命令有效,我得到了:

{"result":"Successfully updated greeting"}

我写了相应的PHP代码利用Guzzle:

$data = '{"setting_type":"greeting","greeting":{"text":"New message."}}';
$client = new \GuzzleHttp\Client();
$response = $client->post(
    'https://graph.facebook.com/v2.6/me/messages?access_token=' . $my_token,
       [
            'headers' => ['Content-Type' => 'application/json'],
            'body' => $data,
            'debug' => true
       ]
);

但它不起作用..请注意上面的php代码可以正常使用Send API参考的其他方法,例如Text message

我做错了什么? 我会根据要求提供任何其他信息。感谢。

这是完整的调试输出:

* Hostname was NOT found in DNS cache
*   Trying 31.13.86.8...
*   Trying 2a03:2880:f008:1:face:b00c:0:1...
* Immediate connect fail for 2a03:2880:f008:1:face:b00c:0:1: Network is unreachable
* Connected to graph.facebook.com (31.13.86.8) port 443 (#0)
* successfully set certificate verify locations:
*   CAfile: none
  CApath: /etc/ssl/certs
* SSL connection using ECDHE-ECDSA-AES128-GCM-SHA256
* Server certificate:
*    subject: C=US; ST=CA; L=Menlo Park; O=Facebook, Inc.; CN=*.facebook.com
*    start date: 2014-08-28 00:00:00 GMT
*    expire date: 2016-12-30 12:00:00 GMT
*    subjectAltName: graph.facebook.com matched
*    issuer: C=US; O=DigiCert Inc; OU=www.digicert.com; CN=DigiCert SHA2 High Assurance Server CA
*    SSL certificate verify ok.
> POST /v2.6/me/messages?access_token=my_token HTTP/1.1
User-Agent: GuzzleHttp/6.2.1 curl/7.35.0 PHP/5.5.9-1ubuntu4.17
Host: graph.facebook.com
Content-Type: application/json
Content-Length: 62

* upload completely sent off: 62 out of 62 bytes
< HTTP/1.1 400 Bad Request
< WWW-Authenticate: OAuth "Facebook Platform" "invalid_request" "(#100) The parameter recipient is required"
< Access-Control-Allow-Origin: *
< Pragma: no-cache
< Cache-Control: no-store
< facebook-api-version: v2.7
< Expires: Sat, 01 Jan 2000 00:00:00 GMT
< Content-Type: text/javascript; charset=UTF-8
< x-fb-trace-id: HNn9aIOgqyq
< x-fb-rev: 2613595
< X-FB-Debug: oD3GTGNh0GIu2muJmZoan9896nrrxC+P+L+G3iYoRZ3cnYh7k+cOl7JRtOvuCedR3gYkUqs2rkTmQ8Nj3plZrg==
< Date: Mon, 10 Oct 2016 21:07:00 GMT
< Connection: keep-alive
< Content-Length: 128
< 
* Connection #0 to host graph.facebook.com left intact
PHP Fatal error:  Uncaught exception 'GuzzleHttp\Exception\ClientException' with message 'Client error: `POST https://graph.facebook.com/v2.6/me/messages?access_token=my_token` resulted in a `400 Bad Request` response:
{"error":{"message":"(#100) The parameter recipient is required","type":"OAuthException","code":100,"fbtrace_id":"HNn9aI (truncated...)
' in /var/www/html/messenger-bot/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php:107

0 个答案:

没有答案