qq_41830949 2025-09-12 08:02 采纳率: 58.8%
浏览 4
已结题

get php返回结果整理

get提交buy.php
http://4235g.com/extensions/buy.php?count=3&period=30&country=us&type=socks

buy.php 内容如下

<?php error_reporting(0);
 
$count = $_GET['count'];
$period = $_GET['period'];
$country = $_GET['country'];
$type = $_GET['type'];

$specialCountries = ['ru', 'br', 'ee', 'ua', 'us', 'ca'];

$version = in_array($country, $specialCountries) ? '3' : '4';

header('Content-Type: application/json');

// 构建API URL
$apiUrl = "https://xz1xx.net/api/a180de66d2-60d8d2ea61-4dsfgs/buy?count=$count&period=$period&version=$version&country=$country&type=$type";

echo file_get_contents($apiUrl);

?>

get提交buy返回的结果如下

得到返回结果
{
  "status": "yes",
  "user_id": "51618",
  "balance": "0.08",
  "currency": "USD",
  "date_mod": "2025-09-12 02:43:22",
  "list_count": 3,
  "list": {
    "34372615": {
      "id": "34372615",
      "version": "3",
      "ip": "70.59.205.230",
      "host": "70.59.205.230",
      "port": "9963",
      "user": "Nbg111",
      "pass": "c2g111",
      "type": "socks",
      "country": "us",
      "date": "2025-09-12 02:43:22",
      "date_end": "2025-10-12 02:43:22",
      "unixtime": 1757634202,
      "unixtime_end": 1760226202,
      "descr": "",
      "active": "1"
    },
    "34372616": {
      "id": "34372616",
      "version": "3",
      "ip": "67.59.205.242",
      "host": "67.59.205.242",
      "port": "9269",
      "user": "22nUt",
      "pass": "c222aL",
      "type": "socks",
      "country": "us",
      "date": "2025-09-12 02:43:22",
      "date_end": "2025-10-12 02:43:22",
      "unixtime": 1757634202,
      "unixtime_end": 1760226202,
      "descr": "",
      "active": "1"
    },
    "34365399": {
      "id": "34365399",
      "version": "3",
      "ip": "12.236.138.20",
      "host": "12.236.138.20",
      "port": "9679",
      "user": "9JQ3Mc",
      "pass": "Yv33ke",
      "type": "socks",
      "country": "us",
      "date": "2025-09-11 18:34:42",
      "date_end": "2025-10-11 18:34:42",
      "unixtime": 1757604882,
      "unixtime_end": 1760196882,
      "descr": "",
      "active": "1"
    }
  },
  "page": 1
}

问题:buy.php怎么写整理以上返回的数据,得到以下最终我要的结果

70.59.205.230:9963:Nbg111:c2g111
67.59.205.242:9269:22nUt;c222aL
12.236.138.20;9679:9JQ3Mc;Yv33ke

buy.php里应该怎么写,整理返回数据得到以上结果

  • 写回答

4条回答 默认 最新

  • 檀越@新空间 2025-09-12 08:03
    关注

    上午好☀️☀️☀️️
    本答案参考通义千问

    根据你提供的 buy.php 文件内容和返回的 JSON 数据,你需要将 API 返回的数据整理成特定格式:IP:PORT:USERNAME:PASSWORD。以下是详细的解决方案。


    一、问题分析

    你希望从 buy.php 获取数据后,将返回的 JSON 数据中的每个代理信息按照以下格式输出:

    IP:PORT:USERNAME:PASSWORD
    

    例如:

    70.59.230:9963:Nbg111:c2g111
    67.59.205.242:9269:22nUt:c222aL
    12.236.138.20:9679:9JQ3Mc:Yv33ke
    

    二、解决步骤

    1. 接收并解析 API 返回的 JSON 数据

    在 PHP 中使用 json_decode() 解析返回的 JSON 字符串。

    2. 遍历 list 数组

    API 返回的 list 是一个关联数组(以字符串为键),需要遍历其中的每个元素。

    3. 按照指定格式拼接字符串

    对每个代理条目,提取 ip, port, user, pass 四个字段,用 : 连接。

    4. 输出结果

    最终将所有格式化后的字符串按行输出。


    三、修改后的 buy.php 代码如下

    <?php
    error_reporting(0);
    
    // 获取 GET 参数
    $count = $_GET['count'];
    $period = $_GET['period'];
    $country = $_GET['country'];
    $type = $_GET['type'];
    
    // 定义特殊国家列表
    $specialCountries = ['ru', 'br', 'ee', 'ua', 'us', 'ca'];
    
    // 判断版本号
    $version = in_array($country, $specialCountries) ? '3' : '4';
    
    // 构建 API URL
    $apiUrl = "https://xz1xx.net/api/a180de66d2-60d8d2ea61-4dsfgs/buy?count=$count&period=$period&version=$version&country=$country&type=$type";
    
    // 获取 API 响应
    $response = file_get_contents($apiUrl);
    
    // 解析 JSON 数据
    $data = json_decode($response, true); // 将 JSON 转换为关联数组
    
    // 设置响应头为纯文本
    header('Content-Type: text/plain');
    
    // 如果有数据并且 list 存在
    if (isset($data['status']) && $data['status'] === 'yes' && isset($data['list'])) {
        foreach ($data['list'] as $item) {
            // 提取所需字段
            $ip = $item['ip'];
            $port = $item['port'];
            $user = $item['user'];
            $pass = $item['pass'];
    
            // 格式化输出
            echo "$ip:$port:$user:$pass\n";
        }
    } else {
        echo "No valid data received.";
    }
    ?>
    

    四、关键点说明

    • json_decode($response, true): 将 JSON 字符串转换为关联数组,方便访问。
    • foreach ($data['list'] as $item): 遍历 list 中的每个代理条目。
    • echo "$ip:$port:$user:$pass\n";: 每行输出一个代理信息,并使用 \n 换行。

    五、示例输出

    假设调用 buy.php 并传入参数:

    http://4235g.com/extensions/buy.php?count=3&period=30&country=us&type=socks
    

    将会得到如下输出:

    70.59.205.230:9963:Nbg111:c2g111
    67.59.205.242:9269:22nUt:c222aL
    12.236.138.20:9679:9JQ3Mc:Yv33ke
    

    六、注意事项

    • 确保 file_get_contents() 可以访问外部 API 地址。
    • 若 API 返回的是压缩数据或加密内容,可能需要额外处理。
    • 如果你希望结果以其他格式(如 CSV 或 HTML)输出,可以调整 echo 的格式。

    如果你还有其他需求(如导出到文件、添加错误处理等),也可以继续告诉我!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

问题事件

  • 系统已结题 9月20日
  • 已采纳回答 9月12日
  • 创建了问题 9月12日