How to get data from json to php

Asked

Viewed 96 times

2

I have the following webservice script, I can connect normally with the external host (code: 200) But in echo Responsebody nothing appears. How to show the obtained data?

<?php
$USER_NAME ="abc";
$PASSWORD = "abc";
$sessionResponse = null;
$sessionId       = null;
$secretKey       = null;
 
  
$subContext = "/files/report?domain=PKE1513&entityReturnFormat=RJSON";
//$subContext = "/report";
//$subContext = "/files/reports";
 
while (true) {
  try {
    if ($sessionId == null) {
      $encodedUserAndPassword = base64_encode($USER_NAME . ":" . $PASSWORD);
      $authParameters[]       = "Authorization: Basic " . $encodedUserAndPassword;
      $sessionResponse        = requestHTTP("GET", "http://www.xx.com", "/core/session/", $authParameters);
 
      if ($sessionResponse["http_code"] == 401) {
        exit("Incorrect username or password");
      }
      echo "\n/core/session/ body: \n$sessionResponse[body]\n";
 
      $decode    = json_decode($sessionResponse["body"], TRUE);
      $sessionId = $decode["sessionId"];
      $secretKey = $decode["secretKey"];
    }
 
    $timestamp = time();
    $signature = base64_encode(hash("MD5", $sessionId . $secretKey . $timestamp, true));
 
    $authParameters   = array();
    $authParameters[] = "Authorization: StkAuth session=\"$sessionId\",signature=\"$signature\",timestamp=\"$timestamp\"";
 

    $response     = requestHTTP("GET", "externalappgw.br.xx.com", $subContext, $authParameters);
    $responseCode = $response["http_code"];

    //var_dump($response);
    //exit();
 
    if ($responseCode == "200") {
      // Aquí poner el código correspondiente al parseo del contenido.
      echo "\nResponseBody:\n$response[body]";
    
      
 
    } else if ($responseCode == "401") {
      $decodedResponse = json_decode($response["body"], TRUE);
      $errorCode       = $decodedResponse["errorCode"];
 
      if ($errorCode == "111") {
        $sessionId = null;
        $secretKey = null;
 
        try {
          //Espera 30 segundos antes de renovar sesión.
          sleep(30);
        } catch (InterruptedException $iex) {}
 
        continue;
      }
    } else {
      echo "Unknown error";
    }
  } catch (Exception $ex) {}
 
  try {
  
    sleep(30);
  } catch (InterruptedException $iex) {}
}
 
function requestHTTP($method, $server, $serviceContext, $authParameters) {
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
  curl_setopt($ch, CURLOPT_URL, $server.$serviceContext);
  curl_setopt($ch, CURLOPT_HEADER, 0);
  curl_setopt($ch, CURLOPT_HTTPHEADER, $authParameters);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $body = curl_exec($ch);
 
  $response         = curl_getinfo($ch);
  $response["body"] = $body;
 
  curl_close($ch);
  return $response;
}

Structure of the data to be read:

[
{"id":"string",
"reportDate":"datetime" 
"inputDate":"datetime"
"device_id":integer,
"holder_id":"string",
"holder_domain":"string",
"holder_name":"string",
"event_id":integer,
"event_desc":"string",
"latitude":decimal,
"longitude":decimal,
"location":"string(Calle, Municipio, Ciudad, Provincia, Pais)",
"course":short,
"speed":decimal,
"ignition":short,
"ignitionDate":"datetime",
"odometer":decimal}
]

Var dump restored:

array (size=27)
  'url' => string 'http://externalappgw.br.xx.com/files/report?domain=PKE1513&entityReturnFormat=RJSON' (length=88)
  'content_type' => string 'text/plain' (length=10)
  'http_code' => int 200
  'header_size' => int 179
  'request_size' => int 249
  'filetime' => int -1
  'ssl_verify_result' => int 0
  'redirect_count' => int 0
  'total_time' => float 0.359
  'namelookup_time' => float 0
  'connect_time' => float 0.203
  'pretransfer_time' => float 0.219
  'size_upload' => float 0
  'size_download' => float 0
  'speed_download' => float 0
  'speed_upload' => float 0
  'download_content_length' => float -1
  'upload_content_length' => float -1
  'starttransfer_time' => float 0.359
  'redirect_time' => float 0
  'redirect_url' => string '' (length=0)
  'primary_ip' => string '201.220.24.90' (length=13)
  'certinfo' => 
    array (size=0)
      empty
  'primary_port' => int 80
  'local_ip' => string '192.168.1.8' (length=11)
  'local_port' => int 64000
  'body' => string '' (length=0)
/core/session/ body: 
{"secretKey":"67433108-4ed7-4f2f-a540-d67b68c994d1","sessionId":"SIDfd135abafe6a4e0da987568a00142025"}
  • var_dump answers what to you?

  • Include in the question

  • 1

    note that the body this emptiness

  • 1

    then the error possibly is in the body declaration that will be assigned to the return ['body']

  • I was able to return the data.

No answers

Browser other questions tagged

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