在PHP表使用JSON数据 [英] Consuming Json data in php table

查看:98
本文介绍了在PHP表使用JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我得到通过REST调用JSON数组,。这里是code。

Hi I am getting a Json array by making REST call,. Here is the code.

<?php include 'header.php' ; ?>
<?php
$base_url = "http://tic.sugarcrmdemo.com/rest/v10";
$username = "sereneintegration";
$password = "Sugar123!";

function call(
    $url,
    $oauthtoken='',
    $type='GET',
    $arguments=array(),
    $encodeData=true,
    $returnHeaders=false
)
{
    $type = strtoupper($type);

    if ($type == 'GET')
    {
        $url.= "?" . http_build_query($arguments);
    }
    $curl_request = curl_init($url);

    if ($type == 'POST')
    {
        curl_setopt($curl_request, CURLOPT_POST, 1);
    }
    elseif ($type == 'PUT')
    {
        curl_setopt($curl_request, CURLOPT_CUSTOMREQUEST, "PUT");
    }
    elseif ($type == 'DELETE')
    {
        curl_setopt($curl_request, CURLOPT_CUSTOMREQUEST, "DELETE");
    }
    curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    curl_setopt($curl_request, CURLOPT_HEADER, $returnHeaders);
    curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);
    if (!empty($oauthtoken))
    {
        $token = array("oauth-token: {$oauthtoken}");
        curl_setopt($curl_request, CURLOPT_HTTPHEADER, $token);
    }
    if (!empty($arguments) && $type !== 'GET')
    {
        if ($encodeData)
        {
            $arguments = json_encode($arguments);
        }
        curl_setopt($curl_request, CURLOPT_POSTFIELDS, $arguments);
    }
    $result = curl_exec($curl_request);
    if ($returnHeaders)
    {
        list($headers, $content) = explode("\r\n\r\n", $result ,2);
        foreach (explode("\r\n",$headers) as $header)
        {
            header($header);
        }
        return trim($content);
    }
    curl_close($curl_request);
    $response = json_decode($result);

    return $response;
}
$url = $base_url . "/oauth2/token";
$oauth2_token_arguments = array(
    "grant_type" => "password",
    "client_id" => "sugar",
    "client_secret" => "",
    "username" => $username,
    "password" => $password,
    "platform" => "base"
);

$oauth2_token_response = call($url, '', 'POST', $oauth2_token_arguments);
$url = $base_url . "/SREV1_Property/48f10d2c-eca2-9163-77fb-56c7677240e2/link/srev1_property_srev1_unit";
     $unit_response = call($url, $oauth2_token_response->access_token, 'GET');

    $json = $unit_response;

    $jd = json_decode(json_encode($json), 1);
        $floors = array();
        foreach ($jd->records AS $key => $obj) {
            $f = $obj->unit_floor;
            $id = $obj->id;
            $name = $obj->name;
            $suite = $obj->suite;
            $sf = $obj->sf;
            $floors[$f][$suite]['name'] = $name;
            $floors[$f][$suite]['id'] = $id;
            $floors[$f][$suite]['sf'] = $sf;
        }
        //sort floors in desc order
        krsort($floors);
        foreach($floors as $id => $floor){
            ksort($floors[$id]);
        }
        print '<table class="data-table" >';
        foreach($floors as $floor => $suites){
            $sqf = 0;
            print '<tr>';
                print '<td>FLOOR: '.$floor.'</td>';
                foreach($suites AS $suite => $value){
                    $sqf += $value['sf'];
                    print'<td>Suite:'.$suite.'<br>SF:'.$value['sf'].'</td>';
                }
                print '<td>'.$sqf.'</td>';
            print '</tr>';
        }
        print '</table>';
    ?>

当我执行此我越来越喜欢试图让非对象的财产提供的foreach()无效参数的错误等。

When I am executing this I am getting an error like "Invalid argument supplied for foreach() " "Trying to get property of non-object " and so on,.

推荐答案

删除 json_de code()因为你已经得到适当的 JSON stdClass的对象,即只让:

Remove json_decode() as you already receive proper JSON stdClass Object, i.e. just make it:

......
$unit_response = call($url, $oauth2_token_response->access_token, 'GET');
$jd = $unit_response;
$floors = array();
foreach ($jd->records AS $key => $obj) {
......

这篇关于在PHP表使用JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆