Request json object always returning NULL in php

Asked

Viewed 503 times

1

I am trying to access a server and send data/receive return in json object with php language.

<?php header('content-type: application/json; charset=utf-8; Accept:application/json; Accept-Language:en-US');

To start, I set the header, the first attempt was using file_get_contents:

//Acesso ao web service
$json_string = file_get_contents('https://111.222.333.444/v1/activateCode');

$parsed_json =json_decode($json_string, true);
echo $parsed_json;

//tentativa falhada de decodificar a string em formato utf-8
$parsed_json = utf8_decode($parsed_json);

var_dump($http_response_header);

the return is NULL, then the 2nd attempt was:

//url com declaracao de variaveis
$url="https://111.222.333.444/v1/activateCode?code=0000002381237220&amount=10.00&upc=799366838258&transactionID=1317828766162&dateTime=2015-05-28T11:32:46.162Z";

//  Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);

// Will dump a beauty json :3
var_dump(json_decode($result, true));
$result = utf8_decode($result);

Return NULL also..., then the third attempt was:

//Acesso ao web service
$json_string = file_get_contents('https://111.222.333.444/v1/activateCode');

$parsed_json =json_decode($json_string, true);

//declarações das variaveis do activateCode
$code = "0000002381237220";
$amount = "10.00";
$upc = 799366838258;
$transactionID = 1317828766162;
$dateTime = "2015-05-28T11:32:46.162Z";
$retailerName = "XYZ";

if( is_null($parsed_json) ){
    // Invalid JSON, don't need to keep on working on it
    echo "<br/>======INICIO NULO=======<br/>";
    echo "codigo nulo";
    echo "<br/>======FIM NULO=======<br/>";
}else{
    // Read data
    echo "<br/>=======RETORNO DA TRANSAÇAO============<br/>";
    echo "RetailTransactionRequest is:";
    echo $parsed_json->alerts[0]->code;
    echo $parsed_json->alerts[0]->amount;
    echo $parsed_json->alerts[0]->upc;
    echo $parsed_json->alerts[0]->transactionID;
    echo $parsed_json->alerts[0]->dateTime;
    echo $parsed_json->alerts[0]->retailerName;
}

That returns only the phrase: >=======RETORNO DA TRANSAÇAO============<br/>RetailTransactionRequest is: without any variable returning from the server... and another there in the last part I tried to set the variable call $ (example echo $parsed_json->alerts[0]->$upc;) Even so, no return. Server access happens ... but I only get NULL, I want to know how to proceed... thank you.

  • Check what’s coming from the server before of trying to decode as JSON. Maybe it’s not a valid JSON, which would explain these symptoms.

No answers

Browser other questions tagged

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