<?php
namespace App\Http\Controllers\Admin\Official;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
define('TOKEN', 'yunlive11');
class WxresponseMsg extends Controller
{
protected $appid = 'wxecbfea5580fb894c';
protected $secret = 'daabe60820d93278cd1988e796b16fa5a';
public function wxtoken(Request $request)
{
if($request->method()=='GET'){
$signature = $request->input('signature');
$timestamp = $request->input('timestamp');
$nonce = $request->input('nonce');
$echoStr = $request->input('echostr');
if ($this->checkSignature($signature, $timestamp, $nonce)) {
ob_end_clean();
return $echoStr;
} else {
return 'Token verification failed.';
}
} elseif ($request->method()=='POST') {
$postStr = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : file_get_contents("php://input");
if (!empty($postStr)) {
$postObj = simplexml_load_string($postStr);
$RX_TYPE = trim($postObj->MsgType);
switch ($RX_TYPE) {
case "text":
$resultStr = $this->handleText($postObj);
break;
case "event":
$resultStr = $this->handleEvent($postObj);
break;
default:
$resultStr = "Unknow msg type: " . $RX_TYPE;
break;
}
echo $resultStr;
} else {
echo "";
exit;
}
}
}
public function handleText($postObj)
{
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$keyword = trim($postObj->Content);
$time = time();
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
if (!empty($keyword)) {
$msgType = "text";
$contentStr = "欢迎您关注".json_encode($object);
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
} else {
echo "lalala";
}
}
public function handleEvent($object)
{
$contentStr = "";
file_put_contents(ROOT_PATH.'acart111.txt', $object->Event, FILE_APPEND);
$EventKey = explode('_', $object->EventKey);
$Ticket = $object->Ticket;
switch ($object->Event) {
case "subscribe":
if(!empty($EventKey[0]) || !empty($Ticket)){
$toUser = $object->ToUserName;
$foUser = (string)$object->FromUserName;
$msgType = 'news';
$createTime = time();
$content = '扫描带参数二维码,未关注推送'.$EventKey[0].'++'.$Ticket.'++'.$EventKey[1].'++'.$EventKey[2];
$array = array(
array(
"title"=>"XXX",
"description"=>"XXXXXXX",
"picUrl"=>"https://s.yun-live.com/images/20200707/fc3bdd153e6136c058b0214fe8ab05d3.jpg",
"url"=>"httpsXXX.com/",
),
array(
"title"=>"XXX",
"description"=>"XXXXXXX",
"picUrl"=>"https://s.yun-live.com/images/20200707/fc3bdd153e6136c058b0214fe8ab05d3.jpg",
"url"=>"httpsXXX.com/",
),
array(
"title"=>"XXX",
"description"=>"XXXXXXX",
"picUrl"=>"https://s.yun-live.com/images/20200707/fc3bdd153e6136c058b0214fe8ab05d3.jpg",
"url"=>"httpsXXX.com/",
),
array(
"title"=>"XXX",
"description"=>"XXXXXXX",
"picUrl"=>"https://s.yun-live.com/images/20200707/fc3bdd153e6136c058b0214fe8ab05d3.jpg",
"url"=>"httpsXXX.com/",
),
array(
"title"=>"XXX",
"description"=>"XXXXXXX",
"picUrl"=>"https://s.yun-live.com/images/20200707/fc3bdd153e6136c058b0214fe8ab05d3.jpg",
"url"=>"httpsXXX.com/",
),
array(
"title"=>"XXX",
"description"=>"XXXXXXX",
"picUrl"=>"https://s.yun-live.com/images/20200707/fc3bdd153e6136c058b0214fe8ab05d3.jpg",
"url"=>"httpsXXX.com/",
),
array(
"title"=>"XXX",
"description"=>"XXXXXXX",
"picUrl"=>"https://s.yun-live.com/images/20200707/fc3bdd153e6136c058b0214fe8ab05d3.jpg",
"url"=>"httpsXXX.com/",
),
array(
"title"=>"XXX",
"description"=>"XXXXXXX",
"picUrl"=>"https://s.yun-live.com/images/20200707/fc3bdd153e6136c058b0214fe8ab05d3.jpg",
"url"=>"httpsXXX.com/",
),
);
$template = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<ArticleCount>".count($array)."</ArticleCount>
<Articles>";
foreach ($array as $key => $v) {
$template .= "<item>
<Title><![CDATA[".$v['title']."]]></Title>
<Description><![CDATA[".$v['description']."]]></Description>
<PicUrl><![CDATA[".$v['picUrl']."]]></PicUrl>
<Url><![CDATA[".$v['url']."]]></Url>
</item>";
}
$template.= "</Articles></xml>";
$temp = sprintf($template,$foUser,$toUser,$createTime,$msgType,$content);
echo $temp;
}else{
$toUser = $object->ToUserName;
$foUser = (string)$object->FromUserName;
$msgType = 'text';
$createTime = time();
$content = '欢迎关注我的微信公众平台111'.$EventKey[0].'++'.$Ticket.'++'.$EventKey[1].'++'.$EventKey[2].json_encode($object);;
$temp = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$temp = sprintf($temp,$foUser,$toUser,$createTime,$msgType,$content);
echo $temp;
}
break;
case "SCAN":
$toUser = $object->ToUserName;
$foUser = $object->FromUserName;
$msgType = 'text';
$createTime = time();
$content = '您已关注过,谢谢!'.json_encode($object);
$temp = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$temp = sprintf($temp,$foUser,$toUser,$createTime,$msgType,$content);
echo $temp;
break;
case "unsubscribe":
$toUser = $object->ToUserName;
$foUser = $object->FromUserName;
$msgType = 'text';
$createTime = time();
$content = '您已关注过,谢谢!'.json_encode($object);
$temp = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$temp = sprintf($temp,$foUser,$toUser,$createTime,$msgType,$content);
echo $temp;
break;
}
$resultStr = $this->responseText($object, $contentStr);
return $resultStr;
}
public function responseText($object, $content, $flag = 0)
{
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>%d</FuncFlag>
</xml>";
$resultStr = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content, $flag);
return $resultStr;
}
private function checkSignature($signature, $timestamp, $nonce)
{
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr, SORT_STRING);
$tmpStr = implode($tmpArr);
$tmpStr = sha1($tmpStr);
if ($tmpStr == $signature) {
return true;
} else {
return false;
}
}
public function createuserinfo($openid, $refer_id)
{
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" . $this->getToken($this->appid, $this->secrect) . "&openid=" . $openid;
$user = request_get($url);
$user = json_decode($user, true);
$users = array(
'openid' => $openid,
'nickname' => $user['nickname'],
'avatar' => $user['headimgurl'],
'sex' => $user['sex'],
'unionid' => $user['unionid'],
'status' => 1,
'reg_time' => $user['subscribe_time'],
'bind_user' => $refer_id
);
$user_str = date('Y-m-d H:i:s') . "\t";
foreach ($users as $key => $value) {
$user_str .= $key . '=' . $value . "\t";
}
$user_str .= "\n";
file_put_contents(LOG_PATH . "wx.log", $user_str, FILE_APPEND);
}
public function getdump($postStr)
{
file_put_contents(ROOT_PATH.'acart.txt', $postStr, FILE_APPEND);
$user_str = date('Y-m-d H:i:s') . "\t";
foreach ($users as $key => $value) {
$user_str .= $key . '=' . $value . "\t";
}
$user_str .= "\n";
file_put_contents(LOG_PATH . "wx.log", $user_str, FILE_APPEND);
}
public function access_token(){
$appid =$this->appid;
$secret =$this->secret;
$sta=Db::table('xcx_access_token')->where('id',2)->first();
if(!empty($sta)){
$ts=time()-$sta->time;
if($ts>7200){
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
$arr = json_decode($this->curlGet($url), true);
if($arr['errcode']==40164){
return '不在白名单提示中'.$arr['errmsg'];
}
$access_token=$arr['access_token'];
$arrayName = array('access_token' =>$access_token ,'time'=>time());
Db::table('xcx_access_token')->where('id',2)->update($arrayName);
}else{
$access_token=$sta->access_token;
$URL = "https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=".$access_token;
$apiData=file_get_contents($URL);
$arr =json_decode($apiData,true);
if($arr['errcode']==40164){
return '不在白名单提示中'.$arr['errmsg'];
}
if(empty($arr['ip_list'])){
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
$result = json_decode($this->curlGet($url), true);
if($result['errcode']==40164){
return '不在白名单提示中'.$result['errmsg'];
}
$access_token=$result ['access_token'];
$arrayName = array('access_token' =>$access_token ,'time'=>time());
Db::table('xcx_access_token')->where('id',2)->update($arrayName);
}
}
}else{
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
$arr = json_decode($this->curlGet($url), true);
if($arr['errcode']==40164){
var_dump($arr);exit;
return '不在白名单提示中'.$arr['errmsg'];
}
$access_token=$arr['access_token'];
$arrayName = array('access_token' =>$access_token ,'time'=>time());
Db::table('xcx_access_token')->insert($arrayName);
}
return $access_token;
}
protected function http_post_data($url, $data_string) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Content-Length: ' . strlen($data_string))
);
ob_start();
curl_exec($ch);
if (curl_errno($ch)) {
$this->ErrorLogger('curl falied. Error Info: '.curl_error($ch));
}
$return_content = ob_get_contents();
ob_end_clean();
$return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
return array($return_code, $return_content);
}
public function curlGet($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_URL, $url);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
public function httpPost($url, $data)
{
$ch = curl_init();
if (class_exists('\CURLFile')) {
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
$data = array('media' => new \CURLFile($data));
} else {
if (defined('CURLOPT_SAFE_UPLOAD')) {
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
}
$data = array('media' => '@' . realpath($data));
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "TEST");
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
public function https_post($url, $post_data = null)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
}
<?php
namespace App\Http\Controllers\Admin\Official;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
class OfficialController extends Controller
{
protected $appid = 'wxecbfea5580fb894c';
protected $secret = 'daabe60820d93278cd1988e796b16fa5a';
public function official_add()
{
$accesstoken = $this->access_token();
$img=file_get_contents("https://s.yun-live.com/images/20200703/5ddaee820294c320e2ed471815a200d3.jpg");
$url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={$accesstoken}&type=image";
$dir ="/official_img/";
!file_exists(".".$dir) && mkdir(".".$dir, 0777, true);
$fileName = time().rand(10000,999999);
file_put_contents("." .$dir.$fileName . ".jpg", $img);
$filename = realpath('official_img/'.$fileName.".jpg");
$result = $this->httpPost($url, $filename);
unlink($filename);
$std=json_decode($result);
if(empty($std->media_id)){
echo "图片上传失败,无法获取图片id";exit;
}
var_dump($std->media_id);
$url = "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=$accesstoken";
$arr = array();
$arr["title"] = "这是个测试ffffff2";
$arr["thumb_media_id"] ="lyNi35Z9oAwi9iLfxN3-RkRVZmbYbd5Fsdybrz6NWKY";
$arr["author"] = 'jh';
$arr["digest"] = 'bbb';
$arr["show_cover_pic"] = 1;
$arr["content"] = "<p>22222222222222";
$arr["content_source_url"] = "https://blog.csdn.net/weixin_35746281/article/details/79178442";
$arr1 = array();
$arr1["title"] = "这是个测试ffffff2";
$arr1["thumb_media_id"] ="lyNi35Z9oAwi9iLfxN3-RkRVZmbYbd5Fsdybrz6NWKY";
$arr1["author"] = 'jh';
$arr1["digest"] = 'bbb';
$arr1["show_cover_pic"] = 1;
$arr1["content"] = "<p>22222222222222";
$arr1["content_source_url"] = "https://blog.csdn.net/weixin_35746281/article/details/79178442";
$arr2 = array();
$arr2["title"] = "这是个测试ffffff2";
$arr2["thumb_media_id"] ="lyNi35Z9oAwi9iLfxN3-RkRVZmbYbd5Fsdybrz6NWKY";
$arr2["author"] = 'jh';
$arr2["digest"] = 'bbb';
$arr2["show_cover_pic"] = 1;
$arr2["content"] = "<p>22222222222222";
$arr2["content_source_url"] = "https://blog.csdn.net/weixin_35746281/article/details/79178442";
$arr3 = array();
$arr3["title"] = "这是个测试ffffff2";
$arr3["thumb_media_id"] ="lyNi35Z9oAwi9iLfxN3-RkRVZmbYbd5Fsdybrz6NWKY";
$arr3["author"] = 'jh';
$arr3["digest"] = 'bbb';
$arr3["show_cover_pic"] = 1;
$arr3["content"] = "<p>22222222222222";
$arr3["content_source_url"] = "https://blog.csdn.net/weixin_35746281/article/details/79178442";
$arr4 = array();
$arr4["title"] = "这是个测试ffffff2";
$arr4["thumb_media_id"] ="lyNi35Z9oAwi9iLfxN3-RkRVZmbYbd5Fsdybrz6NWKY";
$arr4["author"] = 'jh';
$arr4["digest"] = 'bbb';
$arr4["show_cover_pic"] = 1;
$arr4["content"] = "<p>22222222222222";
$arr4["content_source_url"] = "https://blog.csdn.net/weixin_35746281/article/details/79178442";
$data["articles"][0] = $arr;
$data["articles"][1] = $arr1;
$data["articles"][2] = $arr2;
$data["articles"][3] = $arr3;
$data["articles"][4] = $arr4;
$data = json_encode($data,JSON_UNESCAPED_UNICODE);
$result = $this->https_post($url, $data);
var_dump($result);exit;
var_dump(json_decode($result));exit;
return view('admin.official.official_add',$data);
}
public function official_get_push()
{
$data='
{
"touser":[
"oEG8_w4TRGx1832QiRiUs0qRqLIQ",
"oEG8_wxmcEDsq6dXtJdNLWbes4_0",
"oEG8_w3V-ydXhPx0bk3flKMdNpBg",
"oEG8_w2-pSuN4Xh3xShj9lD3V1pM"
],
"mpnews":{
"media_id":"lyNi35Z9oAwi9iLfxN3-Rm6RQXPwkqb75hI3bTml5gU"
},
"msgtype":"mpnews",
"send_ignore_reprint":0
}';
$url = 'https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token='.$this->access_token();
return $this->https_post($url,$data);
}
public function get_mediaupinfo()
{
$media_id=request()->get('media_id');
$url = "https://api.weixin.qq.com/cgi-bin/material/update_news?access_token=".$this->access_token();
$data = '{
"media_id":"'.$media_id.'",
"index":0,
"articles": {
"title": "这是个标题ee11",
"thumb_media_id":"lyNi35Z9oAwi9iLfxN3-RjYRqOjTEaKWyq3VftfJmxs",
"show_cover_pic":"1",
"author":"作者",
"digest":"图文消息的摘要",
"content": "内容2222",
"content_source_url": "https://blog.csdn.net/weixin_35746281/article/details/79178442",
},
"articles": {
"title": "2这是个测试ffffff2",
"thumb_media_id":"lyNi35Z9oAwi9iLfxN3-RkRVZmbYbd5Fsdybrz6NWKY",
"show_cover_pic":"1",
"author":"22222222222",
"digest":"",
"content": "测试内容",
"content_source_url": "https://blog.csdn.net/weixin_35746281/article/details/79178442",
},
"articles": {
"title": "3这是个测试ffffff2",
"thumb_media_id":"lyNi35Z9oAwi9iLfxN3-Rjp3ZSXGwV3YIRByXKPWr18",
"show_cover_pic":"1",
"author":"22222222222",
"digest":"",
"content": "测试内容",
"content_source_url": "https://blog.csdn.net/weixin_35746281/article/details/79178442",
},
}';
$result = $this->https_post($url,$data);
$json = json_decode($result,true);
return $json;
}
public function official_add_materialcount()
{
$url = 'https://api.weixin.qq.com/cgi-bin/material/get_materialcount?access_token='.$this->access_token();
$result = $this->curlGet($url);
$res = json_decode($result, true);
echo "<pre>";
var_dump($res);exit;
}
public function del_material(){
$media_id=request()->get('media_id');
if (!$this->access_token()) return false;
$curl = 'https://api.weixin.qq.com/cgi-bin/material/del_material?access_token='.$this->access_token();
$data= array('media_id'=>$media_id);
$result = $this->https_post($curl,json_encode($data));
if ($result){
$json = json_decode($result,true);
if($result['errcode']==0){
return "素材删除成功";
}elseif ($result['errcode']==40007) {
return "素材ID不正确";
}
}
return '素材删除失败';
}
public function get_mediaList($offset=0,$count=20){
$type=request()->get('type');
if($type==1){
$type="image";
}else if($type==2){
$type="news";
}
if (!$this->access_token()) return false;
$data = array(
'type'=>$type,
'offset'=>$offset,
'count'=>$count
);
$curl = 'https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token='.$this->access_token();
$result = $this->https_post($curl,json_encode($data));
if ($result){
$json = json_decode($result,true);
return $json;
}
return false;
}
public function get_mediaInfo(){
$media_id=request()->get('media_id');
if (!$this->access_token()) return false;
$curl = 'https://api.weixin.qq.com/cgi-bin/material/get_material?access_token='.$this->access_token();
$data= array('media_id'=>$media_id);
$result = $this->https_post($curl,json_encode($data));
if ($result){
$json = json_decode($result,true);
return $json;
}
return false;
}
public function official_AddUser_Group()
{
$name=request()->get('name');
if (!$this->access_token()) return false;
$curl = 'https://api.weixin.qq.com/cgi-bin/groups/create?access_token='.$this->access_token();
$data = array('group' =>['name'=>$name],);
$data = json_encode($data,JSON_UNESCAPED_UNICODE);
$result = $this->https_post($curl,$data);
if ($result){
$json = json_decode($result,true);
return $json;
}
return false;
}
public function official_GetUser_Group()
{
if (!$this->access_token()) return false;
$curl = 'https://api.weixin.qq.com/cgi-bin/groups/get?access_token='.$this->access_token();
$result = $this->curlGet($curl,$data);
if ($result){
$json = json_decode($result,true);
return $json;
}
return false;
}
public function official_GetUser_Group_GroupID()
{
$openid=request()->get('openid');
if (!$this->access_token()) return false;
$curl = 'https://api.weixin.qq.com/cgi-bin/groups/getid?access_token='.$this->access_token();
$data = array('group' =>['name'=>$openid]);
$data = json_encode($data,JSON_UNESCAPED_UNICODE);
$result = $this->https_post($curl,$data);
if ($result){
$json = json_decode($result,true);
return $json;
}
return false;
}
public function official_GetUser_Group_up()
{
$name=request()->get('name');
$id=request()->get('id');
if (!$this->access_token()) return false;
$curl = 'https://api.weixin.qq.com/cgi-bin/groups/update?access_token='.$this->access_token();
$data = array('group' =>['id'=>$id,'name'=>$name]);
$data = json_encode($data,JSON_UNESCAPED_UNICODE);
$result = $this->https_post($curl,$data);
if ($result){
$json = json_decode($result,true);
return $json;
}
return false;
}
public function official_GetUser_Group_upOpenid()
{
$openid=request()->get('openid');
$to_groupid=request()->get('to_groupid');
if (!$this->access_token()) return false;
$curl = 'https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token='.$this->access_token();
$data = array('openid'=>$openid,'to_groupid'=>$to_groupid);
$data = json_encode($data,JSON_UNESCAPED_UNICODE);
$result = $this->https_post($curl,$data);
if ($result){
$json = json_decode($result,true);
return $json;
}
return false;
}
public function getWxQrcode($fqid,$type = 1){
$url ='https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token='.$this->access_token();
if($type == 1){
$qrcode= '{"action_name": "QR_LIMIT_STR_SCENE", "action_info": {"scene": {"scene_str":"1070_66"}}}';
}else{
$qrcode = '{"expire_seconds": 604800, "action_name": "QR_STR_SCENE", "action_info": {"scene": {"scene_str":"1070_66"}}}';
}
$result = $this->http_post_data($url,$qrcode);
$oo = json_decode($result[1]);
if (empty($oo->ticket)){
return false;
}
if(!$oo->ticket){
var_dump('getQrcodeurl falied. Error Info: getQrcodeurl get failed');
exit();
}
$url ='https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket='.$oo->ticket.'';
var_dump($url);
echo "<img src=".$url."></img>";
}
public function access_token(){
$appid =$this->appid;
$secret =$this->secret;
$sta=Db::table('xcx_access_token')->where('id',2)->first();
if(!empty($sta)){
$ts=time()-$sta->time;
if($ts>72){
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
$arr = json_decode($this->curlGet($url), true);
if($arr['errcode']==40164){
return '不在白名单提示中'.$arr['errmsg'];
}
$access_token=$arr['access_token'];
$arrayName = array('access_token' =>$access_token ,'time'=>time());
Db::table('xcx_access_token')->where('id',2)->update($arrayName);
}else{
$access_token=$sta->access_token;
$URL = "https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=".$access_token;
$apiData=file_get_contents($URL);
$arr =json_decode($apiData,true);
if($arr['errcode']==40164){
return '不在白名单提示中'.$arr['errmsg'];
}
if(empty($arr['ip_list'])){
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
$result = json_decode($this->curlGet($url), true);
if($result['errcode']==40164){
return '不在白名单提示中'.$result['errmsg'];
}
$access_token=$result ['access_token'];
$arrayName = array('access_token' =>$access_token ,'time'=>time());
Db::table('xcx_access_token')->where('id',2)->update($arrayName);
}
}
}else{
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
$arr = json_decode($this->curlGet($url), true);
if($arr['errcode']==40164){
var_dump($arr);exit;
return '不在白名单提示中'.$arr['errmsg'];
}
$access_token=$arr['access_token'];
$arrayName = array('access_token' =>$access_token ,'time'=>time());
Db::table('xcx_access_token')->insert($arrayName);
}
return $access_token;
}
protected function http_post_data($url, $data_string) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Content-Length: ' . strlen($data_string))
);
ob_start();
curl_exec($ch);
if (curl_errno($ch)) {
$this->ErrorLogger('curl falied. Error Info: '.curl_error($ch));
}
$return_content = ob_get_contents();
ob_end_clean();
$return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
return array($return_code, $return_content);
}
public function curlGet($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_TIMEOUT, 500);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_URL, $url);
$res = curl_exec($curl);
curl_close($curl);
return $res;
}
public function httpPost($url, $data)
{
$ch = curl_init();
if (class_exists('\CURLFile')) {
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, true);
$data = array('media' => new \CURLFile($data));
} else {
if (defined('CURLOPT_SAFE_UPLOAD')) {
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
}
$data = array('media' => '@' . realpath($data));
}
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "TEST");
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
public function https_post($url, $post_data = null)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
}