Jump to content
新域网络技术论坛

PHP异步访问链接


Jamers
 Share

Recommended Posts

我们有的时候要执行一个较长时间的查询动作,让客户端去等好象不太现实,让服务器去执行吧,到时候去取最终的结果就可以了。

 

调用方式:

ajax('http://www.xxx.xxx/index.php?test=1');

ajax('http://www.xxx.xxx/index.php','post','123456');

执行到这条语句的时候会延迟1ms,然后继续执行后面的代码,不会对用户造成影响。需要注意的是,这些代码是由服务端执行的,所以SESSION之类的操作无效,建议采用文件或数据库等第三方方式存储数据。否则可能造成功能无法实现,切记!

function ajax($url,$mode='get',$data='') {
    $ch = curl_init();
    // set URL and other appropriate options
    curl_setopt($ch, CURLOPT_URL,$url);
    if (strtolower($mode)=='post') {
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_TIMEOUT_MS,1);
    // grab URL, and print
    curl_exec($ch);
    curl_close($ch);
    return true;
}
Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...