如何从 URL-external-php-file 获取“echo”内容

发布于 2024-09-07 12:56:55 字数 1374 浏览 6 评论 0原文

我在 2 个不同的服务器上有 2 个文件:

file1.php - 位于站点 1 - 我传递一个参数,脚本回显答案取决于传递的参数(是其函数) 等浏览器访问文件时,everithink 没问题

   http://site1.com/file1.php?parameterValue

- 当我通过file2.php - 位于站点 2 - file2 必须向 file1.php 发送一个参数并从中获取回显输出作为变量。

我尝试了 3 种不同的方法,但没有一种有效。

方式 1. ----------

function get_data($url)
{
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}  

$f="http://site1.com/file1.php?parameterValue";
$returned_content = get_data($f);
echo "=== $returned_content ===";exit;

方式 2. ----------

$f="http://site1.com/file1.php?parameterValue";
$returned_content='';
$file = fopen ($f, "r");
if (!$file) {
    echo "<p>Unable to open remote file.\n";
    exit;
    }
while (!feof ($file))  $returned_content.= fgets ($file, 1024);
fclose($file);
echo "=-= $returned_content =-=";exit;

方式 3. -------

$f="http://site1.com/file1.php?parameterValue";
$returned_content=implode('',file($f));
echo "=-= $returned_content =-=";exit;

但是 $returned_content 是空字符串...

任何人都可以帮助我吗? 提前致谢!

赫里斯托

I have 2 files on 2 different servers:

file1.php
- resides on site 1
- I pass a parameter, and the script echo-ed answer which depends on (is function of) passed parameter
- everithink is OK when I access file by browser like

   http://site1.com/file1.php?parameterValue

file2.php
- resides on site 2
- file2 has to send a parameter to file1.php AND get echo-ed output from it as variable.

I tried to do it by 3 diferent ways but no ones worked.

way 1. -------

function get_data($url)
{
    $ch = curl_init();
    $timeout = 5;
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
}  

$f="http://site1.com/file1.php?parameterValue";
$returned_content = get_data($f);
echo "=== $returned_content ===";exit;

way 2. -------

$f="http://site1.com/file1.php?parameterValue";
$returned_content='';
$file = fopen ($f, "r");
if (!$file) {
    echo "<p>Unable to open remote file.\n";
    exit;
    }
while (!feof ($file))  $returned_content.= fgets ($file, 1024);
fclose($file);
echo "=-= $returned_content =-=";exit;

way 3. -------

$f="http://site1.com/file1.php?parameterValue";
$returned_content=implode('',file($f));
echo "=-= $returned_content =-=";exit;

BUT $returned_content is empty string ...

Can anybody help me?
Thanks in advance!

Hristo

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

凉宸 2024-09-14 12:56:55

如果你尝试: 会发生什么

<?PHP
$f="http://site1.com/file1.php?parameterValue";
$data = file_get_contents($f);
echo $data;

What happens if you try:

<?PHP
$f="http://site1.com/file1.php?parameterValue";
$data = file_get_contents($f);
echo $data;

?

遗弃M 2024-09-14 12:56:55

您可以使用 CURL 修改您的第一个版本以检查是否发生任何错误。事实上,您盲目地假设curl 请求有效,并且只返回curl_exec() 返回的任何内容。

至少,你应该有这样的东西:

$data = curl_exec($ch)
$err = curl_error($ch);
curl_close($ch);
if ($data === FALSE) { // curl_exec returns boolean FALSE if something blew up
   return($err);
} else {
   return($data);
}

You could modify your first version with CURL to check if any errors occured. As is, you're blindly assuming that the curl request worked and just return whatever curl_exec() returned.

At mininum, you should have something like:

$data = curl_exec($ch)
$err = curl_error($ch);
curl_close($ch);
if ($data === FALSE) { // curl_exec returns boolean FALSE if something blew up
   return($err);
} else {
   return($data);
}
蘸点软妹酱 2024-09-14 12:56:55

我测试了所有这三种方法,并且所有方法都可以使用

$f = "http://google.com/";

我检查 site1 和 file1.php 的配置。也许它会阻止基于用户代理的请求?

I tested all three of those methods and all worked using

$f = "http://google.com/";

I'd check the configuration of site1 and file1.php. Perhaps it's blocking requests based on User-Agent?

自此以后,行同陌路 2024-09-14 12:56:55

这是我的错——我在 file1 中的脚本很复杂,而且我错过了它所依赖的另一个参数。因此,在我更正脚本后,一切正常。

方式2和3工作正确;
方式1我没有测试过。使用

TIMDEV提出的MARC B方式的建议是个好主意,也可以很好地工作。

我要感谢你们所有人给我如此友善的帮助!

谢谢朋友们!

此致
赫里斯托

It was my fault -- my script in file1 was a complicated and I missed one other parameter on which it depends. So, after I correct the script all works fine.

Way 2 and 3 works correct;
Way 1 I didnt tested. It is goog idea to use suggestion of MARC B

way supposed by TIMDEV works fine too.

I would like to thank all of you giving help to me so kindly!

Thank you friends!

Best regards
Hristo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文