[英]Reading remote files using PHP
誰能告訴我如何最多讀取3個遠程文件並將結果編譯成查詢字符串,然后由調用腳本使用標頭將其發送到頁面。
讓我解釋:
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)
您可能可以使用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));
另請注意,此URL應保留2000個字符以內 。
如果要使用超過2000個字符允許的更多數據,則需要對其進行POST。 這里的一種技術是使用包含您的數據的表單將一些HTML發送回客戶端,並讓javascript在頁面加載時自動提交。
表單中可能有一個默認按鈕,顯示“單擊此處繼續...”,您的JS會更改為“請稍候...”。 因此,沒有javascript的用戶將手動驅動它。
換句話說,是這樣的:
<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當然有幫助,但是對於遠程腳本來說,CURL是更好的選擇。
即使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);
您可以使用file_get_contents函數。 API文檔: http : //no.php.net/manual/zh/function.file-get-contents.php
$file_contents = file_get_contents("http://www.foo.com/bar.txt")
請注意,如果文件包含多行或非常長的行,則應考慮使用HTTP發布而不是長URL。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.