时时商务社区

标题: PHP编程实现脚本异步执行的方法 [打印本页]

作者: 网络通达    时间: 2018-2-14 05:24

            本文实例讲述了PHP编程实现脚本异步执行的方法。分享给大家供大家参考,具体如下:
php语言得用fsockopen()函数,实现脚本异步运行,代码如下
异步请求函数(用debug参数若为true则为用为调试,开启调试可以看到异步的执行情况,但是失去异步的效果)
main.php
$v){
    $_post[]= $k."=".urlencode($v);//必须做url转码以防模拟post提交的数据中有&符而导致post参数键值对紊乱
    }
    $_post = implode('&', $_post);
    $header .= "Content-Type: application/x-www-form-urlencoded\r\n";//POST数据
    $header .= "Content-Length: ". strlen($_post) ."\r\n";//POST数据的长度
    $header.="Connection: Close\r\n\r\n";//长连接关闭
    $header .= $_post; //传递POST数据
  }else{
    $header.="Connection: Close\r\n\r\n";//长连接关闭
  }
  fwrite($fp, $header);
  //-----------------调试代码区间-----------------
  //注如果开启下面的注释,异步将不生效可是方便调试
  if($debug){
  $html = '';
  while (!feof($fp)) {
  $html.=fgets($fp);
  }
  echo $html;
  }
  //-----------------调试代码区间-----------------
  fclose($fp);
}
$data=array('name'=>'guoyu','pwd'=>'123456');
$url='http://localhost/test/other.php';
request_by_fsockopen($url,$data,true);//
other.php
使用实例:
[运行的main.php主脚本文件]
$data=array('name'=>'guoyu','pwd'=>'123456');
$url='http://localhost/test/other.php';
request_by_fsockopen($url,$data,true);//把应用B的用户表异步-同步数据
[导步执行文件other.php]
在other.php中便可以用$_POST接收main.php提交过来的参数,从而进行下一步操作
更多关于PHP相关内容感兴趣的读者可查看本站专题:《php socket用法总结》、《PHP网络编程技巧总结》、《php curl用法总结》、《PHP数组(Array)操作技巧大全》、《PHP数据结构与算法教程》、《php程序设计算法总结》及《php字符串(string)用法总结
希望本文所述对大家PHP程序设计有所帮助。
            
            
您可能感兴趣的文章:
  • PHP 中执行系统外部命令
  • 在PHP中执行系统外部命令
  • PHP 执行系统外部命令 system() exec() passthru()
  • PHP用反撇号执行外部命令
  • php外部执行命令函数用法小结
  • 浅谈PHP命令执行php文件需要注意的问题
  • linux系统下定时执行php脚本的方法
  • PHP在linux上执行外部命令的方法
  • 如何离线执行php任务
  • Ubuntu系统下用Crontab命令定时执行PHP文件详解
  • PHP实现执行外部程序的方法详解
            




    欢迎光临 时时商务社区 (http://bbs.4435.cn/) Powered by Discuz! X3.2