显示php数组api内容

Display php array api contents

我正在使用 API。 link 显示在 class 中,像这样

private $apiPath = "http://www.website.com/api/v1/users/username";

现在,如果用户名部分是 tom,我将它放在浏览器中。

private $apiPath = "http://www.website.com/api/v1/users/tom";

它将以文本格式显示所有 toms 统计信息。

我想知道如何以 HTML 渲染格式显示它 使用 php 这样我就可以在我的网站上显示它。有人告诉我它在一个数组中。

所以我假设使用变量我可以让它显示我想要的内容和位置。我真的不清楚。

我有 class 但不确定要做什么。我包括它吗?

如果您只想要 API 返回的数据,class 会告诉我它正在返回 JSON,这是一个 data interchange format. You could use PHP's cURL function to get the data from the API and then decode it into a PHP array using json_decode,然后您可以随心所欲地处理数据。

例如:

$apiUrl = "http://www.website.com/api/v1/users/$username";

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$apiURL);
$result=curl_exec($ch);
curl_close($ch);

$userArray = json_decode($result, true);

如果您需要使用 class,因为您需要将数据处理成它所需要的形式,则需要包含 class 并使用它。

import 'myclass.php';

$userData = new statsProfile('name');

然后您可以根据需要使用class的其他方法。

这将允许您从数据库中获取用户名并进行比较

function stats(){

    $user_id = $_GET['uid'];

    $sql = "SELECT * FROM users WHERE uid = $user_id ";
    $result = query($sql);

    $row = mysqli_fetch_object($result);
    $username = $row->username);

    $url = ('"http://www.website.com/api/v1/users/'.$username);
    $rCURL = curl_init();

    curl_setopt($rCURL, CURLOPT_URL, $url);
    curl_setopt($rCURL, CURLOPT_HEADER, 0);
    curl_setopt($rCURL, CURLOPT_RETURNTRANSFER, 1);

    $aData = curl_exec($rCURL);

    curl_close($rCURL);

    $response = json_decode($aData, true);    

}