【问题标题】:How to cycle API query + array output PHP如何循环API查询+数组输出PHP
【发布时间】:2022-01-02 20:54:54
【问题描述】:

我是 PHP 新手。我想从网站 API 导出一些数据,我有带有查询和 JSON 响应的脚本,但是有大约 20 个类似的查询,我想我可以通过循环使这个脚本更简单,但真的不知道如何去做。我还想将结果插入到表格中。

示例:

$client = new \GuzzleHttp\Client();
$response = $client->request('GET', 'https://api.opensea.io/api/v1/collection/clonex');
$json = $response->getBody();
$data = json_decode($json);
print "CloneX: ";
print_r($data->collection->stats->floor_price);

$client2 = new \GuzzleHttp\Client();
$response2 = $client2->request('GET', 'https://api.opensea.io/api/v1/collection/the-solaverse-sola-stars');
$json2 = $response2->getBody();
$data2 = json_decode($json2);
print "<br />SolaVerse: ";
print_r($data2->collection->stats->floor_price);

【问题讨论】:

    标签: php arrays cycle


    【解决方案1】:

    应该这样做 - 将名称放入数组中

    <?php
    
    define('BASE_URL','https://api.opensea.io/api/v1/collection/');
    $names = ['clonex','the-solaverse-sola-stars'];
    foreach ($names as $n) {
            $json = file_get_contents(BASE_URL.$n);
            $data = json_decode($json);
            if (isset($data->collection->stats->floor_price)) {
                    echo $n.' '.$data->collection->stats->floor_price.PHP_EOL;
            }
    }
    

    【讨论】:

    • 正是我需要的!谢谢!)
    猜你喜欢
    • 1970-01-01
    • 2015-08-07
    • 1970-01-01
    • 2017-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-04
    • 1970-01-01
    相关资源
    最近更新 更多