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)
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:
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.