2017-10-13 51 views
-1

我需要运行在不同的服务器中的PHP代码(我们服务器2称呼它)从服务器1卷曲或的file_get_contents忽略输出

在服务器1,我有这样的事情

<?php 
file_get_contents('http://domain_in_server_2.com/php-script.php'); 
?> 

问题是,这个请求可能需要很长时间,我不需要输出。我只想触发脚本,而不必等待或获取输出。

无论如何要完成我想要的东西吗?

非常感谢。

+0

这对'domain_in_server_2.com'的配置显著依赖 - PHP脚本[可能会退出,如果客户端断开连接(HTTP ://php.net/manual/en/function.ignore-user-abort.php)。 – ceejayoz

+0

我想我的问题很清楚,我没有使用浏览器。我的目标是触发脚本,而不是关心它的工作/输出。或者如何在您的评论 – aye

+0

中“断开连接”file_get_contents('http ...')将用于'domain_in_server_2.com'的目的,就像通过浏览器访问一样。你的'file_get_contents'脚本可能**有**等待输出。 – ceejayoz

回答

1

您可以使用套接字。见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); 
} 
+0

非常感谢。我会尝试看看它是否工作,并在这里报告 – aye

+0

嗨@Joel,你的方法工作,服务器1中的脚本不会等待,但从服务器2如何提取发布数据。 'file_put_contents('file.txt',$ _ POST [“foo”])'创建一个空的'file.txt'文件。非常感谢 – aye

+0

无论如何,我并不需要发布数据,只是为了让问题更有用。我已经接受了答案。非常感谢你! – aye