我需要運行在不同的服務器中的PHP代碼(我們服務器2稱呼它)從服務器1捲曲或的file_get_contents忽略輸出
在服務器1,我有這樣的事情
<?php
file_get_contents('http://domain_in_server_2.com/php-script.php');
?>
問題是,這個請求可能需要很長時間,我不需要輸出。我只想觸發腳本,而不必等待或獲取輸出。
無論如何要完成我想要的東西嗎?
非常感謝。
我需要運行在不同的服務器中的PHP代碼(我們服務器2稱呼它)從服務器1捲曲或的file_get_contents忽略輸出
在服務器1,我有這樣的事情
<?php
file_get_contents('http://domain_in_server_2.com/php-script.php');
?>
問題是,這個請求可能需要很長時間,我不需要輸出。我只想觸發腳本,而不必等待或獲取輸出。
無論如何要完成我想要的東西嗎?
非常感謝。
您可以使用套接字。見example。
編輯:
這裏是從上面的鏈接代碼:
// Example:
// post_async("http://www.server.com/somewhere",array("foo" => 123, "bar" => "ABC"));
function post_async($url, $params)
{
// Build POST string
foreach ($params as $key => &$val) {
if (is_array($val)) $val = implode(',', $val);
$post_params[] = $key.'='.urlencode($val);
}
$post_string = implode('&', $post_params);
// Connect to server
$parts=parse_url($url);
$fp = fsockopen($parts['host'],isset($parts['port'])?$parts['port']:80,$errno, $errstr, 30);
// Build HTTP query
$out = "$type ".$parts['path']." HTTP/1.1\r\n";
$out.= "Host: ".$parts['host']."\r\n";
$out.= "Content-Type: application/x-www-form-urlencoded\r\n";
$out.= "Content-Length: ".strlen($post_string)."\r\n";
$out.= "Connection: Close\r\n\r\n";
$out.= $post_string;
// Send data and close the connection
fwrite($fp, $out);
fclose($fp);
}
這對'domain_in_server_2.com'的配置顯著依賴 - PHP腳本[可能會退出,如果客戶端斷開連接(HTTP ://php.net/manual/en/function.ignore-user-abort.php)。 – ceejayoz
我想我的問題很清楚,我沒有使用瀏覽器。我的目標是觸發腳本,而不是關心它的工作/輸出。或者如何在您的評論 – aye
中「斷開連接」file_get_contents('http ...')將用於'domain_in_server_2.com'的目的,就像通過瀏覽器訪問一樣。你的'file_get_contents'腳本可能**有**等待輸出。 – ceejayoz