1 <?php 2 3 class Kefu{ 4 private $appid; 5 private $appSecret; 6 7 function __construct($appid,$appSecret){ 8 $this->appid = $appid; 9 $this->appSecret = $appSecret; 10 } 11 12 function returnKefu($openid,$content){ 13 $access_token = $this->getAccessToken(); 14 $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=$access_token"; 15 $responseText = array("touser"=>$openid,"msgtype"=>"text","text"=>array("content"=>$content)); 16 $responjson = json_encode($responseText); 17 $res = $this->httpPost($url,$responjson); 18 echo $res; 19 } 20 function returnPic($openid,$content){ 21 $access_token = $this->getAccessToken(); 22 $url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=$access_token"; 23 $responseText = array("touser"=>$openid,"msgtype"=>"image","image"=>array("media_id"=>"Xfh-2sT-4HHz0z1GrAgb1tlJowuAJuyI-O6AzFX_TT3UsZHwqdsjyUCY6GJGviMi")); 24 $responjson = json_encode($responseText); 25 $res = $this->httpPost($url,$responjson); 26 echo $res; 27 } 28 29 30 31 function getAccessToken(){ 32 $file_ticket = "access_token.json"; 33 if(!file_exists($file_ticket)){ 34 $file = fopen($file_ticket,'w'); 35 } 36 $data = json_decode(file_get_contents($file_ticket)); 37 if($data){ 38 //如果文件存在且内容过期 39 if($data->expires_in<time()){ 40 $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appid&secret=$this->appSecret"; 41 $res = json_decode($this->httpGet($url)); 42 $access_token = $res->access_token; 43 if($access_token){ 44 $data->expires_in = time() + 7000; 45 $data->access_token = $access_token; 46 $fp = fopen($file_ticket, "w"); 47 fwrite($fp, json_encode($data)); 48 fclose($fp); 49 } 50 }else{ 51 //没有过期 52 return $data->access_token; 53 54 } 55 }else{ 56 //文件存在,但是里面没有内容 57 $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appid&secret=$this->appSecret"; 58 $res = json_decode($this->httpGet($url)); 59 $access_token = $res->access_token; 60 if($access_token){ 61 @$data->expires_in = time() + 7000; 62 $data->access_token = $access_token; 63 $fp = fopen($file_ticket, "w"); 64 fwrite($fp, json_encode($data)); 65 fclose($fp); 66 } 67 } 68 return $access_token; 69 //return $data; 70 } 71 function httpGet($url){ 72 $ch = curl_init(); 73 curl_setopt($ch, CURLOPT_URL,$url); 74 // curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 75 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 76 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 77 curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)'); 78 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 79 curl_setopt($ch, CURLOPT_AUTOREFERER, 1); 80 // curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 81 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 82 83 $temp = curl_exec($ch); 84 curl_close($ch); 85 return $temp; 86 } 87 88 function httpPost($url,$data){ 89 $ch = curl_init(); 90 curl_setopt( $ch, CURLOPT_URL, $url); 91 curl_setopt( $ch, CURLOPT_POST, 1 ); 92 curl_setopt( $ch, CURLOPT_HEADER, 0 ); 93 curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); 94 //服务器没有使用https的话就不用下面两行 95 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 96 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 97 curl_setopt( $ch, CURLOPT_POSTFIELDS, $data ); 98 99 $output = curl_exec($ch); 100 curl_close($ch); 101 echo $output; 102 } 103 } 104 $k = new Kefu("appid", "appsecret"); 105 echo $k->returnPic("openid","how are you?"); 106 echo $k->returnKefu("openid","how are you?"); 107 ?>
以上是分享微信连续发送多条消息的实例代码的详细内容。更多信息请关注PHP中文网其他相关文章!

PHPSession失效的原因包括配置错误、Cookie问题和Session过期。1.配置错误:检查并设置正确的session.save_path。2.Cookie问题:确保Cookie设置正确。3.Session过期:调整session.gc_maxlifetime值以延长会话时间。

在PHP中调试会话问题的方法包括:1.检查会话是否正确启动;2.验证会话ID的传递;3.检查会话数据的存储和读取;4.查看服务器配置。通过输出会话ID和数据、查看会话文件内容等方法,可以有效诊断和解决会话相关的问题。

多次调用session_start()会导致警告信息和可能的数据覆盖。1)PHP会发出警告,提示session已启动。2)可能导致session数据意外覆盖。3)使用session_status()检查session状态,避免重复调用。

在PHP中配置会话生命周期可以通过设置session.gc_maxlifetime和session.cookie_lifetime来实现。1)session.gc_maxlifetime控制服务器端会话数据的存活时间,2)session.cookie_lifetime控制客户端cookie的生命周期,设置为0时cookie在浏览器关闭时过期。

使用数据库存储会话的主要优势包括持久性、可扩展性和安全性。1.持久性:即使服务器重启,会话数据也能保持不变。2.可扩展性:适用于分布式系统,确保会话数据在多服务器间同步。3.安全性:数据库提供加密存储,保护敏感信息。

在PHP中实现自定义会话处理可以通过实现SessionHandlerInterface接口来完成。具体步骤包括:1)创建实现SessionHandlerInterface的类,如CustomSessionHandler;2)重写接口中的方法(如open,close,read,write,destroy,gc)来定义会话数据的生命周期和存储方式;3)在PHP脚本中注册自定义会话处理器并启动会话。这样可以将数据存储在MySQL、Redis等介质中,提升性能、安全性和可扩展性。

SessionID是网络应用程序中用来跟踪用户会话状态的机制。1.它是一个随机生成的字符串,用于在用户与服务器之间的多次交互中保持用户的身份信息。2.服务器生成并通过cookie或URL参数发送给客户端,帮助在用户的多次请求中识别和关联这些请求。3.生成通常使用随机算法保证唯一性和不可预测性。4.在实际开发中,可以使用内存数据库如Redis来存储session数据,提升性能和安全性。

在无状态环境如API中管理会话可以通过使用JWT或cookies来实现。1.JWT适合无状态和可扩展性,但大数据时体积大。2.Cookies更传统且易实现,但需谨慎配置以确保安全性。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3汉化版
中文版,非常好用

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。

WebStorm Mac版
好用的JavaScript开发工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

MinGW - 适用于 Windows 的极简 GNU
这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。