1

I've been trying to do this for a couple of days through trial and error etc, but getting absolutely nowhere. PHP isn't my strong point, but I'm generally comfortable with it that I can learn as I go when I need to do specific things.

What I'm trying to do, is take the API from one platform that is used, and input it into another platform that is used. I can get the data from the API easily enough via a URL, and it runs fine on a different server so I'm pretty sure everything is fine on that side of things.

The issue is, that when I do manage to get it from a URL, it comes out looking quite messy. Nothing I've tried so far will display it as a nice tidy block. Furthermore, I'd like to be able to pull specific data from the result and display just that. The data comes out as follows when visited via the URL (have changed values for privacy etc, but the integrity should remain):

{"Data":[{"DeviceID":"1","DeviceName":"Phone 1","Platform":"Phone OS","Edition":"Deluxe","State":"0","Time":"2016-03-16T13:47:44+01:00"}]}

Essentially, what I'm trying to do is:

  • Display the data in a block list, as opposed to long lines
  • Allow selection of a specific device through "Device Name", and then display the information relevant to that device

I've tried the following scripts so far:

1:

<?php
    $json = file_get_contents('URLHERE');
    $obj = json_decode($json);
    echo $obj->DeviceID;
?>

2:

<?php
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, 'URLHERE');
    $result = curl_exec($ch);
    curl_close($ch);

    $obj = json_decode($result);
    echo $result->DeviceName;
?>

3:

<?php 
    $url = 'URLHERE';
    $obj = json_decode(file_get_contents($url), true);
    echo $obj['DeviceID'];
?>

4:

<?php
    $url = "URLHERE";
    $json = file_get_contents($url);
    $json_data = json_decode($json, true);
    echo "Device: ". $json_data["DeviceID"];
?>

5:

<?php
    $json = file_get_contents('URLHERE');
    $encodeJ = utf8_encode($json);

    $obj = json_decode($encodeJ);
    var_dump($obj-> DeviceID);
?>

The fourth one is the closest I've managed to get to it displaying data using these methods, but rather than any information I just get "Device: NULL"

Any help would be appreciated. Starting to pull my hair out here!

UPDATE: Have managed to make some progress with the following:

<?php
$data = file_get_contents('URLHERE');

$response = json_decode($data, true);

echo 'Device: ' . $response['Data']['0']['DeviceName'];
echo 'Device: ' . $response['Data']['1']['DeviceName'];

?>

This is displaying the device names from the array for value 0 and 1. So now I need to figure out how to iterate through the array and display each one in sequence, as opposed to hard coding each one.

2 Answers 2

2

Your DeviceID is in Data & it's an array so you can't access directly. When you

$data = json_decode('{"Data":[{"DeviceID":"1","DeviceName":"Phone 1","Platform":"Phone OS","Edition":"Deluxe","State":"0","Time":"2016-03-16T13:47:44+01:00"}]}', true);//I am using array so second parameter is true to easily demonstrate

Your structure is

   [
     "Data" => [
       [
         "DeviceID" => "1",
         "DeviceName" => "Phone 1",
         "Platform" => "Phone OS",
         "Edition" => "Deluxe",
         "State" => "0",
         "Time" => "2016-03-16T13:47:44+01:00",
       ],
     ],
   ]

So to get only first DeviceID if you want then

$deviceID = isset($data['Data'][0]['DeviceID']) ? $data['Data'][0]['DeviceID'] : null;

or if you want all the DeviceIDs then

    $deviceIds = [];
    if (isset($data['Data']) && is_array($data['Data']))
    {
        foreach ($data['Data'] as $row)
        {
            if (isset($row['DeviceID']))
            {
                $deviceIds[] = $row['DeviceID'];
            }
        }
    }

or you can use array_column if your php version is >= 5.5.0 or php 7

    $deviceIds = [];
    if (isset($data['Data']) && is_array($data['Data']))
    {
        $deviceIds = array_column($data['Data'], 'DeviceID');
    }
Sign up to request clarification or add additional context in comments.

Comments

1

To get the data, use:

$json = file_get_contents( $url );

Then get it into an array, as:

$arr = json_decode( $json, TRUE );

To "Display the data in a block list, as opposed to long lines", use:

foreach ( $arr AS $element ) {
  foreach ( $element AS $e ) {
    echo $e['DeviceName'] . '<br>';
  }
}

To "Allow selection of a specific device through "Device Name", and then display the information relevant to that device", use:

$deviceName = "Phone 1"; // depending upon your use case, you'll need to decide how you want to set this variable; it's hard coded here for the sake of example

foreach ( $arr AS $element ) {
  foreach ( $element AS $e ) {
    if ( $e['DeviceName'] = $deviceName ) {
      echo '<pre>';
      print_r( $e );
      echo '</pre>';    
    }
  }
}

While it's not entirely clear what you mean by "Allow selection of a specific device through "Device Name"", I'm inclined to believe you're looking for a way to let a user select a device from the list of device names. That's not a task you can accomplish with PHP alone. You'll need to build something for the front end in HTML or Javascript that interacts with your PHP on the back end.

4 Comments

This is quite useful, I'll be able to use it to display information for all devices. I've jigged it slightly, and have used: $deviceName = $arr['Data']['0']['DeviceName']; instead. My issue now is that rather than displaying each one sequentially, it only displays array value 0 on each device. So, each device is displaying with information specific to that device, apart from the name is always the value of Array space 0. How would I iterate through each one in that instance and display the correct names?
While it's not entirely clear what you mean by "Allow selection of a specific device through "Device Name"": Yep, figured that it will need some JS for that part. Will work on that once I get the core functions working. At the moment, the main task I guess is to get each one listed in full, and then work on being able to select a specific device.
I have updated my respone to the part "To "Display the data in a block list, as opposed to long lines", use:" to reflect what your comment makes clear you are looking to do.
@ChrisBarsby If my answer solved your problem, please select it as the answer to the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.