
本文详解如何在 php 中正确使用 curl 发起带认证头的 get 请求,解析 json 响应,并结构化输出 api 数据(如用户存款列表),避免常见语法错误与安全疏漏。
本文详解如何在 php 中正确使用 curl 发起带认证头的 get 请求,解析 json 响应,并结构化输出 api 数据(如用户存款列表),避免常见语法错误与安全疏漏。
要在 PHP 网页中调用外部 API(如 iHostMN 的存款列表接口)并安全、可靠地展示数据,不能直接在 PHP 中写 Shell 风格的 curl 命令(如 curl -H ...),而必须使用 PHP 的 cURL 扩展函数进行程序化请求。
以下是一个完整、健壮且可直接运行的示例代码:
<?php $api_url = 'https://ihostmn.com/api/v1/sharing/user/list_all_deposits';
$api_key = 'YourIhostmnApiKeyHere'; // ✅ 替换为你的实际 API Key
// 初始化 cURL 会话
$ch = curl_init();
// 设置请求选项
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // 关键:让 curl_exec() 返回响应内容而非直接输出
curl_setopt($ch, CURLOPT_HEADER, false); // 不返回 HTTP 头
curl_setopt($ch, CURLOPT_TIMEOUT, 10); // 设置超时(秒),防阻塞
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 生产环境建议设为 true 并配置 CA 证书
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
// 设置认证请求头(注意:Header 名大小写不敏感,但建议按文档规范)
$headers = [
"IHOSTMN-API-KEY: {$api_key}",
"Content-Type: application/json",
];
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
// 执行请求
$response = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// 检查错误
if (curl_errno($ch)) {
echo '<p class="error">cURL 错误: ' . htmlspecialchars(curl_error($ch)) . '';
curl_close($ch);
exit;
}
// 关闭 cURL 句柄
curl_close($ch);
// 验证 HTTP 状态码(2xx 表示成功)
if ($http_code = 300) {
echo "<p class='\"error\"'>API 请求失败,HTTP 状态码: {$http_code}</p>";
exit;
}
// 解析 JSON 响应(第二个参数为 true 可转为关联数组;false 则为对象——此处推荐对象写法)
$data = json_decode($response, false);
// 检查 JSON 解析是否成功
if (json_last_error() !== JSON_ERROR_NONE) {
echo '<p class="error">JSON 解析失败: ' . htmlspecialchars(json_last_error_msg()) . '</p>';
exit;
}
// ✅ 正确访问嵌套结构:$data->result->deposits 是对象数组
if (!isset($data->result->deposits) || !is_array($data->result->deposits)) {
echo '<p>暂无存款记录或数据格式异常。</p><div class="aritcle_card flexRow artxards">
<div class="artcardd flexRow">
<a class="aritcle_card_img" rel="nofollow" href="/xiazai/skill5820" title="CurlShip Directory Submission"><img
src="https://img.php.cn/upload/skill/000/000/081/179066902235280.jpg" alt="CurlShip Directory Submission" onerror="this.onerror='';this.src='/static/lhimages/moren/morentu.png'" ></a>
<div class="aritcle_card_info flexColumn">
<a rel="nofollow" href="/xiazai/skill5820" title="CurlShip Directory Submission" class="overflowclass">CurlShip Directory Submission</a>
<p class="overflowclass">向CurlShip提交产品,这是一个对机器人友好的SaaS目录。只需一条curl命令即可发布产品,支持OG标签抓取、带徽章的dofollow链接及层级升级。</p>
</div>
<a rel="nofollow" href="/xiazai/skill5820" title="CurlShip Directory Submission" class="aritcle_card_btn flexRow flexcenter"><b></b><span>下载</span>
</a>
</div>
</div>';
} else {
echo '<h3>用户存款列表</h3>';
echo '
| ID | 共享ID | 当前金额 | 总存入 | 总收益 | 提现地址 | 充值地址 |
|---|---|---|---|---|---|---|
| ' . htmlspecialchars($deposit->deposit_id) . ' | '; echo '' . htmlspecialchars($deposit->share_id) . ' | '; echo '' . number_format($deposit->current_amount, 6) . ' | '; echo '' . number_format($deposit->total_deposited, 5) . ' | '; echo '' . number_format($deposit->total_earned, 6) . ' | '; echo '' . htmlspecialchars(substr($deposit->withdraw_address, 0, 12) . '…') . ' | ';
echo '' . htmlspecialchars(substr($deposit->deposit_address, 0, 12) . '…') . ' | ';
echo '
? 关键要点说明:
- ❌ 错误做法:在 PHP 中直接写 curl -H ... 命令(这是 Shell 命令,PHP 不识别);
- ✅ 正确路径:使用 curl_init() + curl_setopt() + curl_exec() 构建请求;
- ? 安全提示:API Key 应避免硬编码上线,推荐通过环境变量(如 getenv('IHOSTMN_API_KEY'))或配置文件管理;
- ?️ 输出防护:所有用户数据(尤其是地址字段)必须经 htmlspecialchars() 转义,防止 XSS;长字符串建议截断显示;
- ⚠️ 错误处理:务必检查 cURL 错误、HTTP 状态码及 JSON 解析结果,不可假设 API 总是可用或返回预期结构;
- ? 数据访问:json_decode($str) 默认返回 stdClass 对象,用 -> 访问属性(如 $obj->result->deposits),而非数组语法 [] —— 后者仅适用于 json_decode($str, true) 返回的关联数组。
将上述代码保存为 .php 文件(如 deposits.php),部署到支持 cURL 和 OpenSSL 的 PHP 环境(PHP ≥ 7.4 推荐),即可安全拉取并渲染实时存款数据。
大量免费API接口:立即使用
涵盖生活服务API、金融科技API、企业工商API、等相关的API接口服务。免费API接口可安全、合规地连接上下游,为数据API应用能力赋能!










