Problems retrieving object from a json

2

Well I have a file in php that takes a Json array and retrieves the recipient field. The code is 100% working on my localhost. But when I put it online, it does not work.

Follow json:

{
  "data": {
    "messages": [
      {
        "id": 1,
        "sender": "[email protected]",
        "recipient": "[email protected]",
        "sent_at": "2015-01-22T18:17:53.586-02:00",
        "status": "delivered",
        "bounce_code": null,
        "subject": "teste"
      },
      {
        "id": 2,
        "sender": "[email protected]",
        "recipient": "[email protected]",
        "sent_at": "2015-01-22T18:17:53.686-02:00",
        "status": "bounced",
        "bounce_code": "5.1.1",
        "subject": "test2"
      }
    ]
  },
  "links": {
    "self": "http://api.smtplw.locaweb.com.br/v1/message_reports?end_date=2015-04-10&page=2&per=2&start_date=2015-01-01&status=all",
    "next": "http://api.smtplw.locaweb.com.br/v1/message_reports?end_date=2015-04-10&page=3&per=2&start_date=2015-01-01&status=all",
    "prev": null,
    "first": "http://api.smtplw.locaweb.com.br/v1/message_reports?end_date=2015-04-10&page=1&per=2&start_date=2015-01-01&status=all",
    "last": "http://api.smtplw.locaweb.com.br/v1/message_reports?end_date=2015-04-10&page=5&per=2&start_date=2015-01-01\u0026status=all"
  }
}

The code looks like this:

            <?php

    // Monta a consulta na API smtp da LocaWeb
    $curl = curl_init();
    curl_setopt_array($curl, array(
      CURLOPT_URL => "https://api.smtplw.com.br/v1/messages?status=errors&start_date=2016-10-26&end_date=2016-10-26",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "GET",
      CURLOPT_HTTPHEADER => array(
        "x-auth-token: xxxxx"
      ),
    ));

    // Excuta consulta
    $resposta = curl_exec($curl);

    // Faz o parsing da string, criando o array
    $jsonObj = json_decode($resposta);
    $resposta = $jsonObj->data;

    // Navega pelos elementos do array
    foreach ($resposta->messages as $c) {
        echo "$c->recipient<br>"; 
    }

    // Fecha consulta
    curl_close($curl);

The strange thing is that on my local server, it is running 100%. But on the production server it does not work. The log looks like this:

[28-Oct-2016 10:04:36 UTC] PHP Notice:  Trying to get property of non-object in /var/www/webroot/ROOT/teste.php on line 20
[28-Oct-2016 10:04:36 UTC] PHP Warning:  Invalid argument supplied for foreach() in /var/www/webroot/ROOT/teste.php on line 23

Lines with errors are:

$resposta = $jsonObj->data; (linha 20)
foreach ($resposta->messages as $c) { (linha 23)

Can anyone tell me why? Remember that it's the same Json and the same php file. The only thing that changes is the server.

asked by anonymous 28.10.2016 / 12:30

2 answers

3

The problem is with SSL, add this in your settings array to bypass verification:

CURLOPT_SSL_VERIFYPEER => false,
    
28.10.2016 / 16:53
1

I do not know if it will help you but here it worked fine.

$obj = '{  
   "data":{  
      "messages":[  
         {  
            "id":1,
            "sender":"[email protected]",
            "recipient":"[email protected]",
            "sent_at":"2015-01-22T18:17:53.586-02:00",
            "status":"delivered",
            "bounce_code":null,
            "subject":"teste"
         },
         {  
            "id":2,
            "sender":"[email protected]",
            "recipient":"[email protected]",
            "sent_at":"2015-01-22T18:17:53.686-02:00",
            "status":"bounced",
            "bounce_code":"5.1.1",
            "subject":"test2"
         }
      ]
   },
   "links":{  
      "self":"http:\/\/api.smtplw.locaweb.com.br\/v1\/message_reports?end_date=2015-04-10&page=2&per=2&start_date=2015-01-01&status=all",
      "next":"http:\/\/api.smtplw.locaweb.com.br\/v1\/message_reports?end_date=2015-04-10&page=3&per=2&start_date=2015-01-01&status=all",
      "prev":null,
      "first":"http:\/\/api.smtplw.locaweb.com.br\/v1\/message_reports?end_date=2015-04-10&page=1&per=2&start_date=2015-01-01&status=all",
      "last":"http:\/\/api.smtplw.locaweb.com.br\/v1\/message_reports?end_date=2015-04-10&page=5&per=2&start_date=2015-01-01\u0026status=all"
   }
}';


$jsonObj = json_decode($obj, true);
$resposta = $jsonObj['data'];

    // Navega pelos elementos do array
foreach ($resposta['messages'] as $c) {
    echo $c['recipient'] . '</br>'; 
}

Sorry, but I have not yet learned how to format code here :-)

    
28.10.2016 / 13:06