【问题标题】:Display info from file externally on another site在另一个站点上显示来自外部文件的信息
【发布时间】:2015-02-15 19:09:12
【问题描述】:

基本上我想要一个集中的文件(最好是 .php 或 .txt)。在其中我将定义我的 3 个 API 的版本和在线状态(登录、注册和统计)

我会以某种方式将它链接到我的系统状态页面,并在我的 html 中使用 $version、$login、$register 或 $stats 调用它们,它们将自动显示集中文件中定义的任何内容。

我的统计页面 (https://epicmc.us/status.php).. 我想从一个单独的文件中定义它并在 HTML 中调用它。

我尝试制作一个名为 check.php 的外部文件并将其放入其中:

<?php
$version = "1.0.0";
$login = 'online';
$register = 'online';
$stats = 'online';
echo json_encode(compact('version','login','register','stats'));
?>

然后在我的统计页面中我调用了它

<?php
$data= json_decode(file_get_contents('https://epicmc.us/api/bridge/check.php'),true);
echo $version;
echo $login;
echo $register;
echo $stats;
?>

该页面只是空白。

您将如何在我的统计信息页面代码中实现这一点? http://pastebin.com/nREdfH1u

【问题讨论】:

  • 问题是什么?你怎么失败?你得到什么错误?
  • 对不起,我是新来的。我将对此进行更详细的编辑。一秒钟。

标签: php html nginx statistics


【解决方案1】:

一个好的解决方案是卷曲你的文件。

由于您已经返回了一个包含您的值的 JSON 字符串,只需卷曲您的“check.php”文件并 json_decode 响应。 此方法的优点之一是您可以从其他域访问这些信息。 您应该能够轻松获取所有值。

例子:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'check.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // to return the response in a variable and not output it
// $result contains the output string
$result = curl_exec($ch);
// close curl resource to free up system resources
curl_close($ch);
$array_response = json_decode($result, true);

// echo $array_response['version']...

【讨论】:

  • 我对这个有点陌生。当你说“卷曲你的文件”时,我该怎么做?对不起,如果那是菜鸟,但我不知道如何处理。这是我的 check.php 中的代码还是我的状态页面中的代码?
  • 此代码应该进入您的状态页面。 Curl 将“ping”您的“check.php”文件,执行它并获得响应。
猜你喜欢
  • 1970-01-01
  • 2015-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多