dta38159 2017-10-13 17:19
浏览 54
已采纳

curl或file_get_contents忽略输出

I need to run a php code in a different server (let's call it server 2) from server 1.

In server 1, I have something like this

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

The problem is, this request may take long time, and I don't need to get the output. I just want to trigger the script without having to wait or getting the output.

Is there anyway to accomplish what I want?

Thank you very much.

  • 写回答

1条回答 默认 最新

  • dongnvwang8591 2017-10-13 17:26
    关注

    You can use a socket. See this example.

    Edit:

    Here is the code from the above link:

    // 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
    ";
        $out.= "Host: ".$parts['host']."
    ";
        $out.= "Content-Type: application/x-www-form-urlencoded
    ";
        $out.= "Content-Length: ".strlen($post_string)."
    ";
        $out.= "Connection: Close
    
    ";
        $out.= $post_string;
    
        // Send data and close the connection
        fwrite($fp, $out);
        fclose($fp);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度