从URL中提取JSON API结果并在PHP中显示

问题描述:

我已经尝试了几天,通过反复试验等等,但是却一无所获. PHP不是我的强项,但是我通常对它感到满意,可以在需要做特定事情时随心所欲地学习.

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.

我想做的是从一个使用的平台中获取API,然后将其输入到另一个使用的平台中.我可以通过URL足够容易地从API中获取数据,并且该数据可以在其他服务器上正常运行,因此我很确定在这方面一切都很好.

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.

问题是,当我设法从URL中获取它时,它看起来非常混乱.到目前为止,我还没有尝试过将其显示为一个整洁的块.此外,我希望能够从结果中提取特定数据并显示出来.通过URL访问时,数据显示如下(更改了隐私等值,但完整性应保持不变):

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):

{数据":[{设备ID":"1",设备名称":电话1",平台":电话OS",版本":豪华",状态":"0 ,"时间:" 2016-03-16T13:47:44 + 01:00}]}

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

本质上,我想做的是:

  • 将数据显示在阻止列表中,而不是长行显示
  • 允许通过设备名称"选择特定设备,然后显示与该设备相关的信息

到目前为止,我已经尝试了以下脚本:

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);
?>

第四个是我设法使用这些方法显示数据的最接近的结果,而不是我只得到"Device:NULL"的任何信息

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'];

?>

这是从数组中显示值0和1的设备名称.因此,现在我需要弄清楚如何遍历数组并依次显示每个设备,而不是对每个设备进行硬编码.

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.

您的DeviceID位于Data&中.这是 array ,因此您无法直接访问.当你

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

您的结构是

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

因此,如果需要,只获取第一个DeviceID

So to get only first DeviceID if you want then

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

或者,如果您需要所有的DeviceID,则

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

或者您可以使用 array_column 如果您的php版本> = 5.5.0或php 7

    $deviceIds = [];
    if (isset($data['Data']) && is_array($data['Data']))
    {
        $deviceIds = array_column($data['Data'], 'DeviceID');
    }