PHP version conflict [duplicate]

3

I tested a script to integrate forms with Mailchimp:

<?php
    // MailChimp API URL
    $memberID   = md5(strtolower($email_popup));
    $dataCenter = substr($apiKey,strpos($apiKey,'-')+1);
    $url        = 'https://' . $dataCenter . '.api.mailchimp.com/3.0/lists/' . $listID . '/members/' . $memberID;

    // member information
    $json = json_encode([
        'email_address' => $email_popup,
        'status'        => 'subscribed',
        'merge_fields'  => [
            'FNAME'   => $nome_empresa_popup,
            'CIDADE'  => $cidade_estado_popup,
            'MMERGE4' => $vendedor_popup
        ]
    ]);

    // send a HTTP POST request with curl
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_USERPWD, 'user:' . $apiKey);
    curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
    $result = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
?>

On my site's server, it worked correctly.

But on the client server, you are giving the following error:

  

Parse error: syntax error, unexpected '[', expecting ')' in /home/{domain}/public_html/{site}/paginas/modal.php on line 511

What's in this line:

$json = json_encode([

I looked in the PHP documentation for something, but found nothing concerning the syntax error.

    
asked by anonymous 19.05.2018 / 16:13

2 answers

3

Try to do this:

$json = json_encode(array(
    'email_address' => $email_popup,
    'status'        => 'subscribed',
    'merge_fields'  => array(
        'FNAME'   => $nome_empresa_popup,
        'CIDADE'  => $cidade_estado_popup,
        'MMERGE4' => $vendedor_popup
    )
));
    
19.05.2018 / 16:25
4

The json_encode function only works in php version 5.2 or higher, and this way of writing an array using only keys [] only works in php version 5.4 or higher.

Sources:

I believe that your version is not less than 5.2, the problem is this when declaring the array, you can solve the problem by putting the code as follows:

$merge_fields = array(
        'FNAME'   => $nome_empresa_popup,
        'CIDADE'  => $cidade_estado_popup,
        'MMERGE4' => $vendedor_popup
);

$array_json = array(
    'email_address' => $email_popup,
    'status'        => 'subscribed',
    'merge_fields'  => $merge_fields
);

$json = json_encode($array_json);
    
19.05.2018 / 16:26