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

如何让PHP Requests库与file_get_contents实现相同请求效果?

解决PHP Requests库调用Roblox API无效的问题

问题出在请求头差异上:file_get_contents会使用PHP默认的User-Agent发起请求,而Requests库的默认User-Agent标识不同,导致Roblox API拒绝了请求。只需让Requests模拟file_get_contents的请求头即可实现相同效果。

修改后的Requests代码如下:

<?php
require_once "src/Autoload.php";
WpOrg\Requests\Autoload::register();

// 模拟file_get_contents的默认请求头
$headers = [
    'User-Agent' => 'PHP/' . PHP_VERSION,
    'Accept' => '*/*'
];

$request = WpOrg\Requests\Requests::get(
    "https://games.roblox.com/v1/games/list?sortToken=T638231628258597209_Curated,R,H_a79d",
    $headers
);

echo $request->body;
?>

关键说明

  • User-Agent设置为PHP/{PHP_VERSION},和file_get_contents默认发送的标识完全一致,Roblox API会认可这个请求来源。
  • 添加Accept: */*是为了匹配file_get_contents的默认请求头行为,进一步保证请求的一致性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 10:27:03