我有一段非常简单的代码,它应该返回一个JSON对象,但不会返回任何值。
当我直接访问网址https://api.kraken.com/0/public/Time时,您可以看到所需的JSON输出。
然而,在调用下一个函数时(在网址https://youreka-virtualtours.be/ethereum/functions.php?call=get_kraken上),我无法获得任何输出
<?php
if (isset($_GET['call'])) {
$error = '';
switch($_GET['call']) {
case 'get_kraken':
$url = 'https://api.kraken.com/0/public/Time';
$json = file_get_contents($url);
http_response_code(200);
header('Content-Type: application/json');
echo $json;
break;
}
}
?>
编辑:问题:由于我的file_get_contents输出与网址https://api.kraken.com/0/public/Time显示的内容不同,我做错了什么
答案 0 :(得分:0)
代码工作正常,我在本地测试。这意味着发生了两件事之一。 1:您的服务器无法进行file_get_contents调用。 2:你的网址不正确,你真的应该回应一些事情。试试这个来测试:
<?php
if(!isset($_GET['call']) || "get_kraken" !== @$_GET['call'] ) {
echo "could not find a valid URL";
}elseif (isset($_GET['call'])) {
$error = '';
switch($_GET['call']) {
case 'get_kraken':
$url = 'https://api.kraken.com/0/public/Time';
$json = file_get_contents($url);
http_response_code(200);
header('Content-Type: application/json');
echo $json;
break;
}
}
?>