㈠ php的CI框架如何实现异步调用
在你自定义的类库中初始化CodeIgniter资源
要你自定义的类库中访问CodeIgniter的原始资源,你必须使用 get_instance() 函数.这个函数返回一个CodeIgniter super object.
一般来说在你的控制器函数中你可以通过 $this 调用任何可用的CodeIgniter函数:
$this->load->helper('url');
$this->load->library('session');
$this->config->item('base_url');
//etc.
$this, 只直接作用在你自己的控制器,模型和视图中.当你在自定义类中想使用CodeIgniter原始类时,你可以这样做:
首先,定义CodeIgniter对象赋给一个变量:
$CI =& get_instance();
一旦定义某个对象为一个变量,你就可以使用那个变量名 取代 $this:
$CI =& get_instance();
$CI->load->helper('url');
$CI->load->library('session');
$CI->config->item('base_url');
//etc.
注意: 你将注意到get_instance()这个函数通过被引用的方式被传递:
$CI =& get_instance();
这十分重要. 通过引用的方式赋给变量将使用原始的 CodeIgniter 对象,而不是创建一个副本。
//------------------------------------------------------------------------------------------------//
我想这也许是你需要的.$CI =& get_instance();之后再用$CI->load->library('session');等方法加载你需要的
㈡ PHP异步处理有哪些方法
客户端与服务器端是通过HTTP协议进行连接通讯,客户端发起请求,服务器端接收到请求后执行处理,并返回处理结果。
有时服务器需要执行很耗时的操作,这个操作的结果并不需要返回给客户端。但因为php是同步执行的,所以客户端需要等待服务处理完才可以进行下一步。
因此对于耗时的操作适合异步执行,服务器接收到请求后,处理完客户端需要的数据就返回,再异步在服务器执行耗时的操作。
1.使用Ajax 与 img 标记
原理,服务器返回的html中插入Ajax 代码或 img 标记,img的src为需要执行的程序。
优点:实现简单,服务端无需执行任何调用
缺点:在执行期间,浏览器会一直处于loading状态,因此这种方法并不算真正的异步调用。
$.get("doRequest.php", { name: "fdipzone"} );
<img src="doRequest.php?name=fdipzone">
2.使用popen
使用popen执行命令,语法:
// popen — 打开进程文件指针
resource popen ( string $command , string $mode )
pclose(popen('php /home/fdipzone/doRequest.php &', 'r'));
优点:执行速度快
缺点:
1).只能在本机执行
2).不能传递大量参数
3).访问量高时会创建很多进程
3.使用curl
设置curl的超时时间 CURLOPT_TIMEOUT 为1 (最小为1),因此客户端需要等待1秒
<?php
$ch = curl_init();
$curl_opt = array(
CURLOPT_URL, 'http://www.example.com/doRequest.php'
CURLOPT_RETURNTRANSFER,1,
CURLOPT_TIMEOUT,1
);
curl_setopt_array($ch, $curl_opt);
curl_exec($ch);
curl_close($ch);
?>
4.使用fsockopen
fsockopen是最好的,缺点是需要自己拼接header部分。
<?php
$url = 'http://www.example.com/doRequest.php';
$param = array(
'name'=>'fdipzone',
'gender'=>'male',
'age'=>30
);
doRequest($url, $param);
function doRequest($url, $param=array()){
$urlinfo = parse_url($url);
$host = $urlinfo['host'];
$path = $urlinfo['path'];
$query = isset($param)? http_build_query($param) : '';
$port = 80;
$errno = 0;
$errstr = '';
$timeout = 10;
$fp = fsockopen($host, $port, $errno, $errstr, $timeout);
$out = "POST ".$path." HTTP/1.1\r\n";
$out .= "host:".$host."\r\n";
$out .= "content-length:".strlen($query)."\r\n";
$out .= "content-type:application/x-www-form-urlencoded\r\n";
$out .= "connection:close\r\n\r\n";
$out .= $query;
fputs($fp, $out);
fclose($fp);
}
?>
注意:当执行过程中,客户端连接断开或连接超时,都会有可能造成执行不完整,因此需要加上
ignore_user_abort(true); // 忽略客户端断开
set_time_limit(0); // 设置执行不超时
㈢ PHP怎样才能异步发送邮件
<?php
$domain="domain";
$url="/system_mail.php";
$par="email=".implode(',',$emailarr)."&........";
$header="POST$urlHTTP/1.0 ";
$header.="Content-Type:application/x-www-form-urlencoded ";
$header.="Content-Length:".strlen($par)." ";
$fp=@fsockopen($domain,80,$errno,$errstr,30);
fputs($fp,$header.$par);
fclose($fp);
echo''发送完毕';
?>
㈣ php如何实现脚本异步执行的方法具体分析
php语言得用fsockopen()函数,实现脚本异步运行,代码如下
异步请求函数(用debug参数若为true则为用为调试,开启调试可以看到异步的执行情况,但是失去异步的效果)
main.php
<?php
/**
*异步请求
*@rightCopyright(c)HangzhouTechnologyCo.,Ltd.(https://www.5wx.org)
*@author$Author:juny$
*@version$Id:main.php3322018-09-2309:15:08Zjuny$
*/
functionrequest_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)";
returnfalse;
}
$method="GET";
if(!empty($post_data)){
$method="POST";
}
$header="$method$requestPathHTTP/1.1 ";
$header.="Host:$hostname ";
if(!empty($post_data)){
$_post=strval(NULL);
foreach($post_dataas$k=>$v){
$_post[]=$k."=".urlencode($v);//必须做url转码以防模拟post提交的数据中有&符而导致post参数键值对紊乱
}
$_post=implode('&',$_post);
$header.="Content-Type:application/x-www-form-urlencoded ";//POST数据
$header.="Content-Length:".strlen($_post)." ";//POST数据的长度
$header.="Connection:Close ";//长连接关闭
$header.=$_post;//传递POST数据
}else{
$header.="Connection:Close ";//长连接关闭
}
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
<?php
header("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'successok';
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提交过来的参数,从而进行下一步操作
以上就是php如何实现脚本异步执行的方法具体分析的详细内容.
㈤ php 怎样实现异步处理接口
首先 php 7以下 不支持异步方式(有个类库 可以勉强算是支持了异步 名字忘了)
其次 php脚本 由于是逐行解析的,不常驻线程(当然可以设置为永久连接,不自动超时退出) 异步意义不大。
第三 我怀疑你是想问javascript的异步请求? 如何用php处理?
如果没问错的话 可以用其他方式来解决异步问题,就是同时发出多个web request请求 等多个请求成功之后将结果写入数据库(文件) 然后 有一个 一直在等待结果的php请求进程 一旦读取到了这个写入完毕的(数据库)文件结果 马上返回给浏览器