0

I've been fiddling around with Facebook Messenger Platform for the past couple days and have run into an issue. PHP has been the primary language.

Successfully, I've been able to implement a couple API's into the system, through plain text. (See image below)

enter image description here

This is what the system looks like:

$input = json_decode(file_get_contents('php://input'), true);
$senderId = $input['entry'][0]['messaging'][0]['sender']['id'];
$message = $input['entry'][0]['messaging'][0]['message']['text'];
$answer = "I don't understand that. Is that another language? Type 'hi' to get started.";

if($message == "hi") {
    $answer = "Yo!";
}

All of this comes from the Facebook Messenger Getting Started if you're not familiar.

What I'm attempting to do now is pass an image through cURL onto JSON. This is something I'm unfamiliar with, but have found two great sources to help me with this task. POSTing JSON Data With PHP cURL and Create nested list from Multidimensional Array.

Here is the result:

if($message == "test") {
$data = array("message" => array("attachement" => array('"type" => "image"'),"payload" => array('"url" => "http://example.com"')));                                                                    
$data_string = json_encode($data);                                                                                   

$ch = curl_init('https://graph.facebook.com/v2.6/me/messages?access_token=TOKEN_GOES_HERE');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
);                                                                                                               
$answer = curl_exec($ch);
}

Here is the response I receive: enter image description here

I know for sure, that the parameters are not properly being picked up by cURL. Though, my limited knowledge on cuRL, suggests otherwise. My question is, how could I still achieve this? I want to be able to pass an image through JSON into messenger, using PHP.

Community
  • 1
  • 1

1 Answers1

1

I think your post request works fine, but due to the error, you didn't pass the whole json data.

Below is how a image generic message looks like, where did you put the recipient in your data?

{
  "recipient":{
    "id":"USER_ID"
  },
  "message":{
    "attachment":{
      "type":"image",
      "payload":{
        "url":"https://petersapparel.com/img/shirt.png"
      }
    }
  }
}

reference: https://developers.facebook.com/docs/messenger-platform/send-api-reference#guidelines

Hui-Yu Lee
  • 909
  • 8
  • 20
  • Thanks for getting back to me. I passed the user ID through `$senderId`. That came from the first code example I provided. Both examples are combined together. Could there be a conflict with both json_encode? –  May 31 '16 at 17:21
  • So you passed the recipient and it still returned as "The parameter recipient is required"? – Hui-Yu Lee Jun 01 '16 at 04:27
  • could you please paste the whole curl request again, as I cannot see where did you put the senderId in the request – Hui-Yu Lee Jun 04 '16 at 02:45
  • `$input = json_decode(file_get_contents('php://input'), true); $senderId = $input['entry'][0]['messaging'][0]['sender']['id']; $message = $input['entry'][0]['messaging'][0]['message']['text']; $answer = "I don't understand that. Is that another language? Type 'hi' to get started."; if($message == "hi") { $answer = "Yo!"; }` All the code in this question is all in one file. –  Jun 10 '16 at 21:38
  • No, I mean your POST request. Suppose there is a 'recipient' field in `$data` array but there was not. `if($message == "test") { $data = array("message" => array("attachement" => array('"type" => "image"'),"payload" => array('"url" => "http://example.com"')));` – Hui-Yu Lee Jun 13 '16 at 03:13