PHP语言实现脚本异步执行
来源:互联网 发布:网页美工和前端工作 编辑:程序博客网 时间:2024/06/10 05:07
php语言得用fsockopen()函数,实现脚本异步运行,代码如下
使用实例:
[运行的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提交过来的参数,从而进行下一步操作
//异步请求函数(用debug参数若为true则为用为调试,开启调试可以看到异步的执行情况,但是失去异步的效果)
main.php
<?phpfunction request_by_fsockopen($url,$post_data=array(),$debug=false){ $url_array = parse_url($url); $hostname = $url_array['host']; $port = isset($url_array['port'])? $url_array['port'] : 80; @$requestPath = $url_array['path'] ."?". $url_array['query']; $fp = fsockopen($hostname, $port, $errno, $errstr, 10); if (!$fp) { echo "$errstr ($errno)"; return false; } $method = "GET"; if(!empty($post_data)){ $method = "POST"; } $header = "$method $requestPath HTTP/1.1\r\n"; $header.="Host: $hostname\r\n"; if(!empty($post_data)){ $_post = strval(NULL); foreach($post_data as $k => $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
<?phpheader("content-type:text/html;charset=utf-8");//error_reporting(0);//ini_set('html_errors',false);//ini_set('display_errors',false);$name = isset($_POST['name'])?$_POST['name']:'';$pwd = isset($_POST['pwd'])?$_POST['pwd']:'';echo $name.$pwd;echo 'success ok';die;?>
使用实例:
[运行的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提交过来的参数,从而进行下一步操作
1 0
- PHP语言实现脚本异步执行
- php 异步执行脚本
- php异步执行shell脚本
- PHP开启异步多线程执行脚本
- PHP异步执行后台的脚本命令
- PHP实现多进程并行执行脚本
- linux下实现定时执行php脚本
- PHP实现多进程并行执行脚本
- php 异步执行
- PHP异步执行
- PHP的异步执行
- PHP的异步执行
- php 异步执行
- PHP异步执行技巧
- php异步函数执行
- php执行shell脚本
- PHP执行shell脚本
- linux 执行php脚本
- opengl坐标系
- CSS3 莲花盛开动画
- GeneNomenclatureUtils_download_urls.txt(生物信息学数据库数据下载链接)
- [BZOJ2761]JLOI2011不重复的数字
- 信号与系统
- PHP语言实现脚本异步执行
- 抽象工厂模式(三):抽象工厂模式概述
- C#身份证验证
- 第14题
- [BZOJ3110]K大数查询|线段树套线段树
- 《机器学习》学习笔记(三):支持向量机(SVM)
- zoj 3605 Find the Marble 计数dp
- hex文件转换为bin文件C语言实现
- [BZOJ3172]TJOI2013单词|AC自动机