首页 > 解决方案 > 如何在没有以 HTML 显示 JS 标签的情况下从 PHP 和 JS 中的其他站点读取数据?

问题描述

我有两个网站(support.mahtt.host 和 phonezone.ir),我希望 phonezone.ir 中的 test.php 文件从 support.mahtt.host 读取价格。根据 WHMCS 文档,一切正常,但我希望它以另一种方式工作。请检查此文件:http ://phonezone.ir/saeed/test.php 它从http://phonezone.ir/saeed/name.php 读取名称,从http://phonezone.ir/saeed/读取价格price.php,但正如您在其 HTML 代码中看到的那样,Javascript 标记显示从哪个站点读取价格和名称。

我最近开始学习 PHP,但我不知道在网络上准确地搜索什么。我曾尝试使用 file_get_contents() 和 curl() 函数,但我认为我尝试了错误的函数。

name.php 内容(省略 php 标签):

  $name = "<script language='javascript' src='https://support.mahtt.host/feeds/productsinfo.php?pid=1&get=name'></script>";

price.php 内容(省略 php 标签):

  $price = "<script language='javascript' src='https://support.mahtt.host/feeds/productsinfo.php?pid=9&get=price&billingcycle=monthly'></script>";

test.php 内容(省略 php 标签):

  include ("./name.php");
  include ("./price.php");
  echo "The price of $name is $price.";

当前的html输出是:

The price of <script language='javascript' src='https://support.mahtt.host/feeds/productsinfo.php?pid=1&get=name'></script> is <script language='javascript' src='https://support.mahtt.host/feeds/productsinfo.php?pid=9&get=price&billingcycle=monthly'></script>.

我希望 HTML 输出与浏览器中显示的一样,如下所示:

The price of IRcPanel-A is 81,000 تومان.

当我或任何其他访问者看到时,我们只能在浏览器和 html 输出中看到上述输出。

你能帮我做什么来得到我的预期结果吗?

标签: php

解决方案



我已经通过使用 php 代码而不是 JS 标签解决了这个问题。
我创建了另一个名为 db.php 的 .php 文件,其内容如下:

<?php
  $servername = "MyServerIP";
  $username = "user_name";
  $dbname = "db_name";
  $password = "some_password";

  $saeedIsCool = new mysqli($servername, $username, $password, $dbname);
  if ($saeedIsCool->connect_error) {
      die("Connection failed: " . $saeedIsCool->connect_error);
  }

  $sql = "SELECT monthly FROM tblpricing WHERE id = 1";
  $result = $saeedIsCool->query($sql);

  if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
      $id1Product = "". $row["monthly"]." Toman";
    }
  } else {
      echo "0 results";
  }
  $floorid1Product = floor($id1Product). " Toman";

  $saeedIsCool->close();
?>

并修改了我的 test.php 内容如下:

<?php
  include ("./name.php");
  include ("./db.php");
  echo "The price of {$name} is {$floorid1Product} Toman per month";
?>

现在结果如预期的那样:
IRcPanel-A 的价格是每月 11200 Toman


推荐阅读