首页 > 解决方案 > 从 API 令牌获取数据到 php 表

问题描述

我有一个包含大约 1030 个 json 对象的 API,我需要通过搜索找到其中的几个,并只在下面显示与我的搜索匹配的那个,我该怎么做?谢谢你的回复

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Campaign Exclude App</title>
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<h1>Campaigns</h1><br>
<input type="text" name="" id="">
<input type="submit">
<table class="table">
  <thead class="thead-dark">
    <tr>
      <th scope="col">Id</th>
      <th scope="col">Campaign Name</th>
      <th scope="col">CPA Goal</th>
      <th scope="col">Click Amount</th>
    </tr>
  </thead>
</table>

<?php
$url='https://www.popads.net/api/campaign_list?key=xxx';
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
curl_close($ch);
$resultArray = json_decode($result);
echo "<pre>";
print_r($resultArray);
?>
</body>
</html>

标签: phpapicurlget

解决方案


这是如何使用 foreach 循环 $resultArray 的简单示例,因此您可以通过名称找到任何对象:

$resultArray = json_decode($result);

$name = "type the name you looking for";

$myArray = array();
foreach ($resultArray as $val){
    if ($val->name == $name){
        $myArray[] = $val;
    }
}

print_r($myArray); // print what you find

推荐阅读