Ⅰ 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非同步操作執行一些代碼應該怎麼做
好的程序應該盡可能少的去讀取資料庫,而不是像你說的還100毫秒讀取一次。這樣的需求本身就很有問題哦
Ⅲ php 怎樣實現非同步處理介面
首先 php 7以下 不支持非同步方式(有個類庫 可以勉強算是支持了非同步 名字忘了)
其次 php腳本 由於是逐行解析的,不常駐線程(當然可以設置為永久連接,不自動超時退出) 非同步意義不大。
第三 我懷疑你是想問javascript的非同步請求? 如何用php處理?
如果沒問錯的話 可以用其他方式來解決非同步問題,就是同時發出多個web request請求 等多個請求成功之後將結果寫入資料庫(文件) 然後 有一個 一直在等待結果的php請求進程 一旦讀取到了這個寫入完畢的(資料庫)文件結果 馬上返回給瀏覽器
Ⅳ php 延遲非同步執行執行怎麼做
用js,給b方法加個setTimeout()定時器,設定20秒後執行b方法即可,其他方法正常運行。
setTimeout() :在指定的毫秒數後調用函數或計算表達式。
Ⅳ php 非同步上傳圖片幾種方法總結
代碼如下
form action="upload.php" id="form1" name="form1" enctype="multipart/form-data" method="post" target="uploadIframe"> <!--上傳圖片頁面 --> </form> <iframe name="uploadIframe" id="uploadIframe" style="display:none"></iframe>
然後後台處理完上傳圖片邏輯後返回給前台,利用ajax修改當前頁面DOM對象實現無刷新上傳圖片的友好功能。
實例
代碼如下
a.html <form enctype="multipart/form-data" action="a.php" target="ifram_sign" method="POST"> <input name="submit" id="submit" value="" type="hidden"> <label>上傳文件: <input name="test_file" type="file" id="test_file" size="48"></label> <input type="image" value="立即上傳" id="submit_btn"> </form><iframe name="ifram_sign" src="" frameborder="0" height="0" width="0" marginheight="0" marginwidth="0"></iframe>
php代碼:
代碼如下
<?php
if ($_files["test_file"]["error"] > 0)
{
echo "Error: " . $_files["test_file"]["error"] . "<br />";
}
else
{
//這里的判斷圖片屬性的方法就不寫了。自己擴展一下。
$filetype=strrchr($_files["test_file"]["name"],".");
$filetype=substr($filetype,1,strlen($filetype));
$filename="img/".time("YmdHis").".".$filetype;
move_uploaded_file($_files["test_file"]["tmp_name"],$filename);
echo '<script >alert(1)</script>';
$return="parent.document.getElementByIdx_x('mpic".$pageset_id."').innerhtml='".$dataimgpath."'";
echo "<script >alert('上傳成功')</script>";
echo "<script>{$return}</script>";
}
?>
其實jquery ajax圖片非同步上傳
html:
<!DOCTYPE html PUBLIC "-//W3C//dtd Xhtml 1.0 transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en_US" xml:lang="en_US">
<head>
<title>圖片非同步上傳</title>
</head>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<link type="text/css" rel="stylesheet" href="css/index.css">
<body>
<div class="frm">
<form name="uploadFrom" id="uploadFrom" action="upload.php" method="post" target="tarframe" enctype="multipart/form-data">
<input type="file" id="upload_file" name="upfile">
</form>
<iframe src="" width="0" height="0" style="display:none;" name="tarframe"></iframe>
</div>
<div id="msg">
</div>
</body>
</html>
index.js
$(function(){
$("#upload_file").change(function(){
$("#uploadFrom").submit();
});
});
function stopSend(str){
var im="<img src='upload/images/"+str+"'>";
$("#msg").append(im);
}
upload.php
<?php
$file=$_files['upfile'];
$name=rand(0,500000).dechex(rand(0,10000)).".jpg";
move_uploaded_file($file['tmp_name'],"upload/images/".$name);
//調用iframe父窗口的js 函數
echo "<script>parent.stopSend('$name')</script>";
?>
非同步上傳圖片幾種方法
Ⅵ php 同步編程和非同步編程的區別
傳統的同步編程是一種請求響應模型,調用一個方法,等待其響應返回.
非同步編程就是要重新考慮是否需要響應的問題,也就是縮小需要響應的地方。因為越快獲得響應,就是越同步化,順序化,事務化,性能差化。
非同步編程通常是通過fire and forget方式實現,發射事件後即忘記,做別的事情了,無需立即等待剛才發射的響應結果了。(發射事件的地方稱為生產者,而將在另外一個地方響應事件的處理者稱為消費者).非同步編程是一種事件驅動編程,需要完全改變思路,將「請求響應」的思路轉變到「事件驅動」思路上,是一種軟體編程思維的轉變.
Ⅶ 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是順序執行的語言,註定無法單獨依靠PHP本身去實現非同步執行。
但可以藉助比如在HTML中ajax的非同步請求去實現自己想要的效果。
Ⅸ php在CLI下能實現非同步調用嗎
php的實現是允許環境為多線程的,這樣一個php進程可以並發執行多個執行流,同時TSRM保證多個執行流的運行能同步。
但php腳本是沒法像C的pthread那樣起線程的。
一個腳本請求執行處於同一進程中的另一腳本,如果進程本身是多線程的,那兩個腳本確實是並發運行,但這跟傳統意義上的「控制線程」並不是一回事, php腳本根本無法像傳統的線程那樣與另一個腳本進行變數共享和線程同步。
Ⅹ 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如何實現腳本非同步執行的方法具體分析的詳細內容.