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?
– JuniorNunes
ok posted the entire php file
– Hugo Borges
Dude, after that line
$resposta = curl_exec($curl);
puts aprint_r($resposta); exit;
to see what is being assigned to the variable$resposta
– JuniorNunes
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– JuniorNunes
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%
– Hugo Borges
You debugged the variable
$resposta
? It returns something?– JuniorNunes
yes, it goes blank. the problem really is in the request. I just don’t understand why it works on my localhost
– Hugo Borges
Yeah, I don’t get it either, try using this tool here, to see if the request will work: http://wst.mytechlabs.com/
– JuniorNunes
You can also use it in your code instead of curl_exec:
curl_errno($resposta);
orcurl_error($resposta);
to try to dig deeper.– JuniorNunes
In your CURLOPT_HTTPHEADER, try to add this element to the array: 'Content-Type: application/json'
– JuniorNunes
I tried to leave him like this:
CURLOPT_HTTPHEADER => array(
 "x-auth-token: xxxx",
 "Content-Type: application/json"
 ),
but it didn’t work out– Hugo Borges
look at the error that returns
cURL Error #:Peer certificate cannot be authenticated with known CA certificates
– Hugo Borges
Try to add these two elements in your array: CURLOPT_SSL_VERIFYHOST => 0, CURLOPT_SSL_VERIFYPEER => 0
– JuniorNunes
the problem was with ssl even, I updated the question, after vc post an answer for I mark ok
– Hugo Borges
Okay, I’ll create the answer to formalize then!
– JuniorNunes