0

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.

5
  • 1
    try commenting $mango_id = $this->mango_id; and use hard code value just to debug $mango_id = 123; Commented Mar 2, 2017 at 11:15
  • 1
    try using var_dump($response) or print_r($response) and tell me what you get in response Commented Mar 2, 2017 at 11:16
  • 1
    Here is how to check your version of PHP <?php echo 'PHP Version ' . phpversion(); ?> Commented Mar 2, 2017 at 11:18
  • Thanks @RiggsFolly. I'm checking right now. It's the 5.3.29. I'm updating right now the post. Commented Mar 2, 2017 at 11:20
  • Thanks, @Jabs. I tried but nothing changed. Commented Mar 2, 2017 at 11:20

2 Answers 2

1

If you really are using PHP5.3.29 then the new array syntax [] does not work

This code

$response = [
        'recipient' => [ 'id'   =>  $sender_id ],
        'message'   => [ 'text' => $res ]
    ];

Generates this error in 5.3.29

Parse error: syntax error, unexpected '[' in D:\PHP-SOURCE\tst.php on line 5

As would the Mango code in the function you mention

This code would work

$response = array(
    'recipient' => array( 'id'   =>  $sender_id ),
    'message'   => array( 'text' => $res )
);
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, this works! I dunno why I could not see this error in the logs, but the fix is the correct one. Thank you a lot!
1

Please read the docs for var_dump. It returns void, which is your "pure nothing" you are logging with error_log(var_dump($respons)).

Use one of print_r($respons,true), var_export($respons, true), json_encode($respons) or similar which return a string that can be logged.

3 Comments

Sorry but this is not the answer to my question. Thank you for providing me this correction, but I didn't ask for a correct way to debug. I'm asking why I cannot build an array. Also if I don't debug it, the function returns this pure nothing.
My point is it returns a valid array, you just fail to log it properly, which leads you to wrong assumptions.
yes, I was also asking same in comments to question to find whether array is valid or not

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.