PHP file_get_contents无法正常工作

时间:2014-09-15 09:56:43

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

我昨天在这里问了一个问题,并给出了以下代码。

<?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

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

1 个答案:

答案 0 :(得分:2)

代码中有错误你试过这段代码

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();