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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化