file_get_contents可以在本地主机上正常运行,而不是在共享主机上运行

时间:2020-01-31 20:50:18

标签: php instagram file-get-contents shared-hosting

这是一个简单的示例,此代码可在不使用API​​的情况下获取所有Instagram用户的关注者计数。

$otherPage = 'nasa';
$response = file_get_contents("https://www.instagram.com/$otherPage/?__a=1");
if ($response !== false) {
    $data = json_decode($response, true);
    if ($data !== null) {
        $follows = $data['graphql']['user']['edge_follow']['count'];
        $followedBy = $data['graphql']['user']['edge_followed_by']['count'];
        echo $follows . ' and ' . $followedBy;
    }
}

此代码在我的本地主机和朋友的服务器上均有效,但在我的共享主机中却不起作用:(

file_get_contents函数运行良好,但仅适用于内部文件,而不能在线链接。

有人知道为什么吗?

1 个答案:

答案 0 :(得分:2)

这很可能是由于托管服务提供商已禁用配置中的allow_url_fopen

要启用默认选项,这可能在您的/和您朋友的服务器上都是正确的,共享托管服务提供商通常会禁用此选项以及其他可能有害的东西/可能影响正在运行的其他站点同一主机。

要确认,请尝试获取该值,如果此值被禁用,则将无法从远程URI调用 fopen

echo ini_get("allow_url_fopen");