简体   繁体   English

如何将cURL中的数据显示到PHP中的表中?

[英]How do I display data from cURL into a table in PHP?

I need to get data from a website and display in a table in my website. 我需要从网站获取数据并显示在网站的表格中。 I am getting the data correctly but now just need to add it to a table so that it is more legible. 我正确地获取了数据,但现在只需将其添加到表中即可,以使其更清晰。

This is the code for some of the information I am getting: 这是我得到的一些信息的代码:

echo "<div style='font-weight: bold;'>getProvinces</div>";

$args = array();
$args["strSessionId"] = $session;

$result = CurlFunction($args, "getProvinces");
echo '<pre>';
var_dump($result);
echo '</pre>';

And

case "getProvinces":
    {
        $Url = "https://apitest.axxess.co.za/" . 
"calls/rsapi/getProvinces.json";
        $curl->setBasicAuthentication($Username, $Password);
        $curl->setOpt(CURLOPT_SSL_VERIFYPEER, false);
        $curl->setOpt(CURLOPT_SSL_VERIFYHOST,2);
        $curl->get($Url, $d);

        break;
    }

I just need some help on how to get the above info into a table. 我只需要一些有关如何将以上信息放入表格的帮助。 And if possible only sections. 如果可能的话,只有节。 I have attached an image to show what I am getting at the moment as well as which data I would like to have in a Table format. 我已附上一张图像,以显示当前所获得的信息以及希望以表格格式获得的数据。

Thanks 谢谢 例

Link to complete code I am using: https://transfernow.net/923hx9h1f2er 链接到我正在使用的完整代码: https : //transfernow.net/923hx9h1f2er

Use json_decode to obtain an array representing your response data, and use a foreach loop to create key-value pairs inside a table for each data item. 使用json_decode获取表示您的响应数据的数组,并使用foreach循环在表内为每个数据项创建键值对。

The code could look like: 代码看起来像:

$jsoned = json_decode($result, true)
?>
<table>
<?php foreach ($jsoned as $k => $v) {?>
<tr><td> <?=$k?> </td><td> <?=$v?> </td></tr>
<?php } ?>

btw, <?= $var ?> is a shortcut for <? echo $var; ?> btw, <?= $var ?><? echo $var; ?>的快捷方式 <? echo $var; ?>

First of all, convert your json response into an array by using json_decode() like: 首先,使用json_decode()json响应转换为数组:

// assuming `$jsonResponse` is your json response variable
$decoded = json_decode($jsonResponse,true); // here true will use return response in array format

Then, you can use your response check like: 然后,您可以使用响应检查,例如:

if($decoded['intCode'] == 200){
    print_r($decoded['arrProvinceId']); // this will return you your response in array
}

now, you can you foreach() loop with html whatever you need with $decoded['arrProvinceId'] result. 现在,您可以根据需要使用$decoded['arrProvinceId']结果对html foreach()循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM