Problems recovering a json object

Asked

Viewed 291 times

2

Well I have a php file 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 doesn’t work.

Follow the 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 went 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 working 100%. But on the production server it does not work. The log is 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

The lines with errors are:

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

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

_______________________________ EDIT _______________________

I solved the problem with disabling SSL

CURLOPT_SSL_VERIFYPEER => false,
  • Friend has how you put the part of the code you fill the variable $answer?

  • ok posted the entire php file

  • Dude, after that line $resposta = curl_exec($curl); puts a print_r($resposta); exit; to see what is being assigned to the variable $resposta

  • What you can also do is take the json that you put here in the question and manually assign in the variable $resposta and take a test to see if it will answer what you expect, if it responds correctly means that the problem is in the API request

  • I did this test, and I noticed that the error is in the request for API, you know what can be? because on my local machine works 100%

  • You debugged the variable $resposta? It returns something?

  • yes, it goes blank. the problem really is in the request. I just don’t understand why it works on my localhost

  • Yeah, I don’t get it either, try using this tool here, to see if the request will work: http://wst.mytechlabs.com/

  • You can also use it in your code instead of curl_exec: curl_errno($resposta); or curl_error($resposta); to try to dig deeper.

  • In your CURLOPT_HTTPHEADER, try to add this element to the array: 'Content-Type: application/json'

  • I tried to leave him like this: CURLOPT_HTTPHEADER => array(&#xA; "x-auth-token: xxxx",&#xA; "Content-Type: application/json"&#xA; ), but it didn’t work out

  • look at the error that returns cURL Error #:Peer certificate cannot be authenticated with known CA certificates

  • Try to add these two elements in your array: CURLOPT_SSL_VERIFYHOST => 0, CURLOPT_SSL_VERIFYPEER => 0

  • the problem was with ssl even, I updated the question, after vc post an answer for I mark ok

  • Okay, I’ll create the answer to formalize then!

Show 10 more comments

2 answers

3


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

CURLOPT_SSL_VERIFYPEER => false,

1

I don’t know if it’ll help you but it worked out well here.

$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>'; 
}

I’m sorry but I haven’t learned how to format code here :-)

  • the problem is in the API request, as the variable does not work resposta is empty.

  • Um, got it. So it’s in the api. I thought it was in the code.

  • you know what it can be?

  • No. I usually make my requests using Angular with $http. I never did using CURL.

Browser other questions tagged

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