First things first. Here's the bad code:
public function get_mango($mango_juice) {
$mango_id = $this->mango_id;
$respons = array(
'recipient' => [ 'id' => $mango_id ],
'message' => [ 'text' => "$mango_juice" ]
);
return $respons;
}
This is a function of the Mango.php file in which is contained the Mango class. Mango is used in the main page (index.php) to provide an answer to what comes from the outside internet (in this case, Facebook).
The code I provided seems to be bad because I checked that $mango_id has a value after I assigned $this->mango_id but, mysteriously, $respons is neither NULL nor EMPTY! So many error_log("$poor_variable"); brought me to this. As an example, if I error_log(var_dump($respons)) the only thing I get is nothing, pure nothing. So you can imagine that nothing is returned.
I performed the error_logs in the right place, so after instantiate the variables described.
If I try to build the array in the following piece of code (actually the part is commented, obviously) it works! *"Fantastic!" I said after I noticed that using this workaround the code worked, but I can't put the construction of the array in the main index.php because I will lose all the organization the OOP programming method provides you!
if(!empty($input['entry'][0]['messaging'][0]['sender']['id'])) {
$sender_id = $input['entry'][0]['messaging'][0]['sender']['id'];
//the mango class - the constructor put the $sender_id in the mango_id property
$mango = new Mango($sender_id);
//user_input
$messageText = $input['entry'][0]['messaging'][0]['message']['text'];
//I haven't showed this method, but imagine this returns a clean, always non-empty string.
$mango_answer = $mango->talk($messageText);
//This is where I call the incriminated piece of code.
$res = $mango->compose_answer($mango_answer);
/**$response = [
'recipient' => [ 'id' => $sender_id ],
'message' => [ 'text' => "$res" ]
];*/
$ch = curl_init('https://graph.facebook.com/v2.6/me/messages?access_token='.FBHandler::ACCESS_TOKEN);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($res));
curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
curl_exec($ch);
curl_close($ch);
}
What's happening?
PS: I noticed that there were many questions related to PHP not building arrays, many of them marked as a duplicate of others where the solution provided was too general, like "upgrade your PHP" or too specific. I can neither upgrade nor check my version of PHP. So, please, help me!
[EDIT] My PHP version is 5.3.29. Thanks to @RiggsFolly.
<?php echo 'PHP Version ' . phpversion(); ?>