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

如何向PHP页面发送原始JSON?搭建模拟API实现数据传输

搭建模拟API返回指定JSON的实现方法

以下是几种简单的实现方式,可根据你的需求选择:

方法1:PHP编写模拟API接口

创建一个名为mock_api.php的文件,写入以下代码,它会直接返回你提供的JSON数据:

<?php
// 设置响应头为JSON格式,确保客户端正确解析
header('Content-Type: application/json');

// 你的原始JSON数据
$mockJson = '{
"0": {
"name": "Powerball",
"plays": [
  {
    "name": "Powerball",
    "active": null,
    "draws": [
      {
        "date": "08/22/2022",
        "nextDrawDate": "08/24/2022",
        "nextDrawJackpot": 100000000,
        "numbers": [
          {
            "value": "12",
            "order": 1,
            "specialBall": null
          },
          {
            "value": "27",
            "order": 2,
            "specialBall": null
          },
          {
            "value": "34",
            "order": 3,
            "specialBall": null
          },
          {
            "value": "55",
            "order": 4,
            "specialBall": null
          },
          {
            "value": "67",
            "order": 5,
            "specialBall": null
          },
          {
            "value": "09",
            "order": 6,
            "specialBall": {
              "name": "Powerball"
            }
          },
          {
            "value": "2",
            "order": 7,
            "specialBall": {
              "name": "Power Play"
            }
          }
        ],
        "prizes": [],
        "extraFields": []
      }
    ]
  }
}
}';

// 输出JSON数据
echo $mockJson;
?>

之后在你的getAPIData.php中,通过HTTP请求调用这个模拟接口:

<?php
// 使用file_get_contents调用模拟API(需确保php.ini中allow_url_fopen开启)
$apiResponse = file_get_contents('http://localhost/mock_api.php');

// 或者用CURL调用(兼容性更好)
// $ch = curl_init('http://localhost/mock_api.php');
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// $apiResponse = curl_exec($ch);
// curl_close($ch);

// 解析JSON为数组
$parsedData = json_decode($apiResponse, true);

// 后续业务逻辑处理
var_dump($parsedData);
?>

方法2:静态JSON文件直接读取

如果不需要模拟HTTP接口的完整流程,可直接创建一个mock_data.json文件,将你的JSON内容存入其中:

{
"0": {
"name": "Powerball",
"plays": [
  {
    "name": "Powerball",
    "active": null,
    "draws": [
      {
        "date": "08/22/2022",
        "nextDrawDate": "08/24/2022",
        "nextDrawJackpot": 100000000,
        "numbers": [
          {
            "value": "12",
            "order": 1,
            "specialBall": null
          },
          {
            "value": "27",
            "order": 2,
            "specialBall": null
          },
          {
            "value": "34",
            "order": 3,
            "specialBall": null
          },
          {
            "value": "55",
            "order": 4,
            "specialBall": null
          },
          {
            "value": "67",
            "order": 5,
            "specialBall": null
          },
          {
            "value": "09",
            "order": 6,
            "specialBall": {
              "name": "Powerball"
            }
          },
          {
            "value": "2",
            "order": 7,
            "specialBall": {
              "name": "Power Play"
            }
          }
        ],
        "prizes": [],
        "extraFields": []
      }
    ]
  }
}
}

然后在getAPIData.php中直接读取该文件:

<?php
// 读取本地静态JSON文件
$jsonContent = file_get_contents('mock_data.json');
$parsedData = json_decode($jsonContent, true);

// 业务逻辑处理
var_dump($parsedData);
?>

注意事项

  • 确保你的PHP环境(如XAMPP、WAMP)已启动,将文件放在服务器根目录下(如XAMPP的htdocs文件夹)。
  • 若使用CURL方式,需确保PHP已启用CURL扩展。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 18:06:16