如何从 URL-external-php-file 获取“echo”内容
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果你尝试: 会发生什么
?
What happens if you try:
?
您可以使用 CURL 修改您的第一个版本以检查是否发生任何错误。事实上,您盲目地假设curl 请求有效,并且只返回
curl_exec()
返回的任何内容。至少,你应该有这样的东西:
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:
我测试了所有这三种方法,并且所有方法都可以使用
我检查 site1 和 file1.php 的配置。也许它会阻止基于用户代理的请求?
I tested all three of those methods and all worked using
I'd check the configuration of site1 and file1.php. Perhaps it's blocking requests based on User-Agent?
这是我的错——我在 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