operator
operator

Reputation: 257

PHP - cannot build array

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.

Upvotes: 0

Views: 72

Answers (2)

RiggsFolly
RiggsFolly

Reputation: 94662

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 )
);

Upvotes: 1

Alex Blex
Alex Blex

Reputation: 37048

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.

Upvotes: 1

Related Questions