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

PHP调用返回JSON格式的API无法获取数据的两种实现方案

PHP调用JSON格式API接口实现方案

在PHP开发中,若遇到调用返回JSON格式数据的API接口时无法正常获取数据的问题,可参考以下两种实现方式的代码示例:


方案1:使用PHP内置file_get_contents()函数实现

$api_url = 'https://api.tg3ds.com/api/v1/scan_records?apikey=1sjQKWfPpdyxRBvfv2BuTl5JzexOIScCFN0t&limit=20&offset=0&sort=scanned_at&user_id=PGLY1096&unfold=true&filter=PGLY1096';
// 读取JSON数据
$json_data = file_get_contents($api_url);
// 将JSON数据解码为PHP数组
$response_data = json_decode($json_data);
var_dump($response_data);
exit();

方案2:使用cURL扩展实现

// 创建并初始化curl会话
$curl = curl_init();

// 使用curl_setopt()设置请求URL
curl_setopt($curl, CURLOPT_URL, "https://api.tg3ds.com/api/v1/scan_records?apikey=1sjQKWfPpdyxRBvfv2BuTl5JzexOIScCFN0t&limit=20&offset=0&sort=scanned_at&user_id=PGLY1096&unfold=true&filter=PGLY1096");

// 通过setopt()设置将传输结果以字符串形式返回
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

// curl_exec()执行已启动的curl会话
// $output存储返回的结果字符串
$output = curl_exec($curl);
var_dump($output);
exit();

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 20:18:03