【问题标题】:PHP file_get_contents not working correctlyPHP file_get_contents 无法正常工作
【发布时间】:2014-09-15 09:56:42
【问题描述】:

我昨天在这里问了一个问题,得到了以下代码。

<?php 

        function usd_rate(){
        $json = file_get_contents("https://bitpay.com/api/rates");
            $obj = json_decode($json);
                if($obj->code == 'USD') return $o->rate;    
        }
        echo usd_rate()
?>

起初它运行良好,但我离开了共享主机并转移到专用服务器主机。

我已经检查了 PHP.ini 以确保以下内容

; Whether to allow the treatment of URLs (like http:// or ftp://) as files.
; http://php.net/allow-url-fopen
allow_url_fopen = On

; Whether to allow include/require to open URLs (like http:// or ftp://) as fil$
; http://php.net/allow-url-include
allow_url_include = On

但它仍然没有显示任何值。我已经坚持了大约 2 个小时,所以我认为寻求帮助可能是一个明智的主意。

谢谢你能提供给我的任何东西。

还有

php --version的输出

PHP 5.5.9-1ubuntu4.4 (cli) (built: Sep  4 2014 06:56:34)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
    with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies

不确定我的上一个主机在运行什么

【问题讨论】:

  • error_reporting(-1)? Apache 错误日志?
  • 试试 $_SERVER['DOCUMENT_ROOT'].'api/rates' 这样的..
  • 您在更改 php.ini 后是否重新启动了您的网络服务器?
  • 是的,重新启动了整台机器,当我对 json 对象执行 var_dump 时,它显示数据“array(158) { [0]=> object(stdClass)#1 (3) { [” code"]=> string(3) "USD" ["name"]=> string(9) "US Dollar" ["rate"]=> float(471.76) }",我想得到 ['rate' ] 但是当我尝试获取它时,它只是显示为空白,也许我错过了一些东西(我是 PHP 新手 :()

标签: php json function fopen file-get-contents


【解决方案1】:

你给的代码有错误试试这个代码

function usd_rate(){
    $json = file_get_contents("https://bitpay.com/api/rates");
    $obj = json_decode($json);
    foreach($obj as $o) 
        if($o->code == 'USD') return $o->rate;    
}
echo usd_rate();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-11
    • 1970-01-01
    • 2015-11-15
    • 1970-01-01
    • 2019-01-13
    • 2021-08-16
    • 2012-10-28
    • 2013-10-13
    相关资源
    最近更新 更多