【问题标题】:file_get_contents works right on localhost not on shared hostingfile_get_contents 适用于本地主机而不是共享主机
【发布时间】:2020-01-31 20:50:18
【问题描述】:

这是一个简单的示例,此代码无需 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 函数运行良好,但仅适用于内部文件而非在线链接。

有人知道为什么吗?

【问题讨论】:

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


    【解决方案1】:

    这可能是因为托管服务提供商在配置中禁用了allow_url_fopen

    默认选项是启用的,这可能在您/您朋友的服务器上都是如此,共享主机提供商通常会禁用此功能以及其他可能是恶意的/有可能影响其他网站运行的东西同一个主机。

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

    echo ini_get("allow_url_fopen");
    

    【讨论】:

    • 非常感谢您,我会尝试联系他们为我启用此功能
    • 在我的 php.init 文件中启用了 allow_url_fopen = On
    • 另外,我已经通过类似这样的方式进行了检查:if (ini_get('allow_url_fopen') == 1) 并且它返回 true
    • 有时您可以在托管服务提供商的 PHP 配置面板上启用/禁用 allow_url_fopen
    猜你喜欢
    • 2019-07-29
    • 2013-11-19
    • 2016-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    • 2016-01-14
    • 2014-10-01
    相关资源
    最近更新 更多