You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP新手求助:循环Curl调用API后无法输出数据

问题解决:从Gametools API提取成员数据

核心问题分析

你当前代码的主要问题是:

  • cURL请求返回的是JSON格式的字符串,但你直接将其存入数组,未转换成PHP可操作的关联数组,因此无法通过$data[0]['userName']这种方式取值。
  • 后续输出代码未考虑$data是包含多个用户数据的数组,直接使用$data['avatar']会导致索引错误。
  • 使用了PHP短标签<?,部分服务器可能未开启该配置,导致代码无法执行。

解决方案

1. 解析JSON字符串为PHP关联数组

在curl_exec获取响应后,用json_decode()将JSON字符串转换成关联数组,第二个参数设为true确保返回数组而非对象。同时添加错误处理,避免请求失败或JSON解析出错导致的问题。

2. 循环遍历所有用户数据

因为批量查询了多个成员,$data数组中每个元素对应一个用户的解析后数据,需要通过循环逐个输出每个用户的信息。

3. 替换短标签为标准PHP标签

将<? echo替换为<?php echo,保证在所有PHP环境中都能正常运行。

修改后的完整代码

<?php

// skip_battlelog allows you to skip battlelog when it searches a name
// available options: true, false
$skip_battlelog = 'true';

// available options: de-de, en-us
$lang = 'de-de';

// available options: stats, weapons, vehicles, all
$stats = 'stats';

// available options: pc, ps4, xboxone
$platform = 'ps4';

// available options: bf4, bf1, bfv
$game = 'bfv';

// player psn-id 改成数组更便于维护
$psidList = ['WODAN-HRAFNAGUD', 'DerTobe']; 
$data = [];

foreach ($psidList as $psid) {
    // 动态拼接URL,避免重复编写多个URL
    $url = "https://api.gametools.network/$game/$stats/?format_values=true&name=$psid&platform=$platform&lang=$lang&skip_battlelog=$skip_battlelog";
    
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_USERAGENT, "PHP Curl");
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_TIMEOUT, 10);

    $response = curl_exec($curl);
    $curlError = curl_error($curl);
    curl_close($curl);

    // 处理cURL请求错误
    if ($curlError) {
        $data[] = ['error' => "请求失败: $curlError"];
        continue;
    }

    // 解析JSON为关联数组
    $parsedData = json_decode($response, true);
    // 处理JSON解析错误
    if (json_last_error() !== JSON_ERROR_NONE) {
        $data[] = ['error' => "JSON解析失败: " . json_last_error_msg()];
        continue;
    }

    $data[] = $parsedData;
}

// 循环输出所有用户数据
foreach ($data as $userData) {
    // 检查是否存在错误信息
    if (isset($userData['error'])) {
        echo "<p>错误: {$userData['error']}</p>";
        continue;
    }
?>
    <h2><img src="<?php echo $userData['avatar']; ?>" width="20px" height="auto" /> <?php echo $userData['userName'];?></h2>
    <table>
    <tr>
        <td>Rang:</td>
        <td><?php echo $userData['rank'];?></td>
    </tr>
    <tr>
        <td>K/D:</td>
        <td><?php echo $userData['killDeath'];?></td>
    </tr>
    <tr>
        <td>SPM:</td>
        <td><?php echo $userData['scorePerMinute'];?></td>
    </tr>
    <tr>
        <td>Kills:</td>
        <td><?php echo $userData['kills'];?></td>
    </tr>
    <tr>
        <td>Kopfschüsse:</td>
        <td><?php echo $userData['headShots']; ?></td>
    </tr>
    <tr>
        <td>Höchste Killstreak:</td>
        <td><?php echo $userData['highestKillStreak'];?></td>
    </tr>
    <tr>
        <td>Präzision:</td>
        <td><?php echo $userData['accuracy'];?></td>
    </tr>
    <tr>
        <td>Beste Klasse:</td>
        <td><?php echo $userData['bestClass'];?></td>
    </tr>
    </table>
    <hr> <!-- 分隔不同用户的信息 -->
<?php
}
?>

额外优化说明

  • 将玩家ID改为数组$psidList,后续添加更多成员只需在数组中追加,无需重复编写URL和循环逻辑,更易维护。
  • 添加了cURL请求错误和JSON解析错误的处理,方便排查问题。
  • 使用foreach遍历$data数组,自动输出每个成员的信息,避免手动索引导致的错误。

内容的提问来源于stack exchange,提问作者Tim

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.24 10:18:32