登录  /  注册

PHP实现脚本异步执行的方法

墨辰丷
发布: 2018-05-18 15:33:58
原创
1753人浏览过

这篇文章主要介绍了php编程实现脚本异步执行的方法,结合实例形式分析了php使用fsockopen()实现的脚本异步调用与执行操作技巧,需要的朋友可以参考下

具体如下:

php语言得用fsockopen()函数,实现脚本异步运行,代码如下

异步请求函数(用debug参数若为true则为用为调试,开启调试可以看到异步的执行情况,但是失去异步的效果)

main.php

<?php
function request_by_fsockopen($url,$post_data=array(),$debug=false){
  $url_array = parse_url($url);
  $hostname = $url_array[&#39;host&#39;];
  $port = isset($url_array[&#39;port&#39;])? $url_array[&#39;port&#39;] : 80;
  @$requestPath = $url_array[&#39;path&#39;] ."?". $url_array[&#39;query&#39;];
  $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(&#39;&&#39;, $_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 = &#39;&#39;;
  while (!feof($fp)) {
  $html.=fgets($fp);
  }
  echo $html;
  }
  //-----------------调试代码区间-----------------
  fclose($fp);
}
$data=array(&#39;name&#39;=>&#39;guoyu&#39;,&#39;pwd&#39;=>&#39;123456&#39;);
$url=&#39;http://localhost/test/other.php&#39;;
request_by_fsockopen($url,$data,true);//
登录后复制

other.php

<?php
header("content-type:text/html;charset=utf-8");
//error_reporting(0);
//ini_set(&#39;html_errors&#39;,false);
//ini_set(&#39;display_errors&#39;,false);
$name = isset($_POST[&#39;name&#39;])?$_POST[&#39;name&#39;]:&#39;&#39;;
$pwd = isset($_POST[&#39;pwd&#39;])?$_POST[&#39;pwd&#39;]:&#39;&#39;;
echo $name.$pwd;
echo &#39;success ok&#39;;
die;
?>
登录后复制

使用实例:

[运行的main.php主脚本文件]

$data=array(&#39;name&#39;=>&#39;guoyu&#39;,&#39;pwd&#39;=>&#39;123456&#39;);
$url=&#39;http://localhost/test/other.php&#39;;
request_by_fsockopen($url,$data,true);//把应用B的用户表异步-同步数据
登录后复制

[导步执行文件other.php]

在other.php中便可以用$_POST接收main.php提交过来的参数,从而进行下一步操作

相关推荐:

php如何实现脚本异步执行的方法具体分析

PHP语言实现脚本异步执行

PHP语言实现脚本异步执行_PHP教程

以上就是PHP实现脚本异步执行的方法的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号