PHP version conflict

Asked

Viewed 60 times

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 the server of my site, it worked correctly.

But on the client server, the following error is occurring:

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

Which is on that line:

$json = json_encode([

I searched the PHP documentation for something, but found nothing related to the syntax error.

  • Try changing the way you declare the array, instead of putting it like this [] put array()

  • which version of php from the client server?

  • The one on my site is 5.6. The one on the customer is 5.2.

2 answers

4

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

Sources:

I believe that your version is not less than 5.2, the problem must be 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);

3


Try to do it this way:

$json = json_encode(array(
    'email_address' => $email_popup,
    'status'        => 'subscribed',
    'merge_fields'  => array(
        'FNAME'   => $nome_empresa_popup,
        'CIDADE'  => $cidade_estado_popup,
        'MMERGE4' => $vendedor_popup
    )
));
  • 1

    They were the same keys. Learning one more. Thank you so much for the help! I will pay more attention before putting here. I come here as a last resort. But I’ll pay more attention.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.