简体   繁体   English

使用PHP读取远程文件

[英]Reading remote files using PHP

can anyone tell me how to read up to 3 remote files and compile the results in a query string which would now be sent to a page by the calling script, using headers. 谁能告诉我如何最多读取3个远程文件并将结果编译成查询字符串,然后由调用脚本使用标头将其发送到页面。

Let me explain: 让我解释:

page1.php

$rs_1 = result from remote page a;
$rs_2 = result from remote page b;
$rs_3 = result from remote page c;

header("Location: page2.php?r1=".$rs_1."&r2=".$rs_2."&r3=".$rs_3)

You may be able to use file_get_contents , then make sure you urlencode the data when you construct the redirection url 您可能可以使用file_get_contents ,然后在构造重定向URL时确保对数据进行urlencode

$rs_1 =file_get_contents($urlA);
$rs_2 =file_get_contents($urlB);
$rs_3 =file_get_contents($urlC);


header("Location: page2.php?".
  "r1=".urlencode($rs_1).
  "&r2=".urlencode($rs_2).
  "&r3=".urlencode($rs_3));

Also note this URL should be kept under 2000 characters . 另请注意,此URL应保留2000个字符以内

Want to send more than 2000 chars? 要发送超过2000个字符?

If you want to utilize more data than 2000 characters would allow, you will need to POST it. 如果要使用超过2000个字符允许的更多数据,则需要对其进行POST。 One technique here would be to send some HTML back to the client with a form containing your data, and have javascript automatically submit it when the page loads. 这里的一种技术是使用包含您的数据的表单将一些HTML发送回客户端,并让javascript在页面加载时自动提交。

The form could have a default button which says "Click here to continue..." which your JS would change to "please wait...". 表单中可能有一个默认按钮,显示“单击此处继续...”,您的JS会更改为“请稍候...”。 Thus users without javascript would drive it manually. 因此,没有javascript的用户将手动驱动它。

In other words, something like this: 换句话说,是这样的:

<html>
<head>
<title>Please wait...</title>
<script>

function sendform()
{
   document.getElementById('go').value="Please wait...";
   document.getElementById('autoform').submit();
}
</script>
</head>    
<body onload="sendform()">
<form id="autoform" method="POST" action="targetscript.php">
<input type="hidden" name="r1" value="htmlencoded r1data here">
<input type="hidden" name="r2" value="htmlencoded r2data here">
<input type="hidden" name="r3" value="htmlencoded r3data here">
<input type="submit" name="go" id="go" value="Click here to continue">
</form>

</body>
</html>

file_get_contents certainly helps, but for remote scripting CURL is better options. file_get_contents当然有帮助,但是对于远程脚本来说,CURL是更好的选择。

This works well even if allow_url_include = On (in your php.ini) 即使allow_url_include = On (在您的php.ini中),这也能很好地工作

$target = "Location: page2.php?";

$urls = array(1=>'url-1', 2=>'url-2', 3=>'url-3');
foreach($urls as $key=>$url) {
    // get URL contents
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($ch);
    $target .= "&r".$key."=".urlencode($output));
}
header("Location: ".$target);

You can use the file_get_contents function. 您可以使用file_get_contents函数。 API documentation: http://no.php.net/manual/en/function.file-get-contents.php API文档: http : //no.php.net/manual/zh/function.file-get-contents.php

$file_contents = file_get_contents("http://www.foo.com/bar.txt")

Do note that if the file contain multiple lines or very, very long lines you should contemplate using HTTP post instead of a long URL. 请注意,如果文件包含多行或非常长的行,则应考虑使用HTTP发布而不是长URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM