微信接口

微信公众平台:https://mp.weixin.qq.com/
微信公众平台测试账号:http://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login
access_token.json和jsapi_ticket.json发送消息时才会生成
验证url时无需appid和secret
<?php
if(!defined("TGF"))exit("0");
class index_controller extends hjf_controller{
	function __construct()
	{
		parent::__construct();
	}
	function main(){
		//wget与shell http://blog.csdn.net/cws1214/article/details/21440691
		echo $this->AccessToken();
	}
	function weixin(){
		$obj_article = $this->load_model("admin");
		$this->wx->valid();
		$textTpl=$this->wx->get_text_tpl();
		$imgTpl=$this->wx->get_image_tpl();
		//get post data, May be due to the different environments
		$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
		//extract post data
		if (!empty($postStr)){
			$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
			error_log(var_export($postObj,1),3,'/var/www/html/weixin/log.txt');
			$fromUsername = $postObj->FromUserName;
			$toUsername = $postObj->ToUserName;
			$MsgType = $postObj->MsgType;//消息类型 
			$MsgEvent = $postObj->Event;//获取事件类型 
			$EventKey = $postObj->EventKey;//菜单的自定义的key值,可以根据此值判断用户点击了什么内容,从而推送不同信息 
			$keyword = trim($postObj->Content);
			$time = time();
			if($MsgEvent=="CLICK"){
				$msgType = "text";
				if($EventKey=="V1001_TODAY_MUSIC"){
					$contentStr = "今日歌曲";
				}elseif($EventKey=="V1001_GOOD"){
					$contentStr = "我点赞";
				}
				$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
				echo $resultStr;
			}
			if($keyword && $MsgType=="text")
			{
				$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $MsgType, $keyword);
				echo $resultStr;
			}
			if($MsgType=="image"){
				$media_id="IOZ0TtMtjwqlY7XVmS_5x6KLW96NkVLl7dhz_IAd8dUUyWDsaFoVQWno8I5SuvmF";
				$resultStr = sprintf($imgTpl, $fromUsername, $toUsername, $time, $MsgType, $media_id);
				echo $resultStr;
			}
		}
	}
	function userList(){
		$access_token=$this->AccessToken();
		$url="https://api.weixin.qq.com/cgi-bin/user/get?access_token=$access_token";	
		$list=$this->httpGet($url);
		$array=$this->jsontoarray($list);
		echo "<pre>";print_r($array);echo "</pre>";
	}
	function userInfo(){
		$access_token=$this->AccessToken();
		$open_id="o7qRAs_Y1a1Weitn10DaKTeWGRhk";
		$url="https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$open_id&lang=zh_CN";
		$list=$this->httpGet($url);
		$list=$this->httpGet($url);
		$array=$this->jsontoarray($list);
		echo "<pre>";print_r($array);echo "</pre>";
	}
	function allGroup(){
		$access_token=$this->AccessToken();
		$url="https://api.weixin.qq.com/cgi-bin/groups/get?access_token=$access_token";
		$list=$this->httpGet($url);
		$array=$this->jsontoarray($list);
		echo "<pre>";print_r($array);echo "</pre>";
	}
	function userGroup(){
		$access_token=$this->AccessToken();
		$url="https://api.weixin.qq.com/cgi-bin/groups/getid?access_token=$access_token";
		$fields = array(  
			'openid'=>'o7qRAs_Y1a1Weitn10DaKTeWGRhk',  
		);  
		$data=json_encode($fields);
		//$str="curl -H \"Content-type: application/json\" -X POST -d '$data'  $url";
		//echo $str;
		//echo $data;
		$result = $this->httpGet($url,$data);
		echo "<pre>";print_r($result);echo "</pre>";
	}
	function editGroup(){
		$access_token=$this->AccessToken();
		$url="https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token=$access_token";
		$fields = array(  
			'openid'=>'o7qRAs_Y1a1Weitn10DaKTeWGRhk',
			'to_groupid'=>100,
		);  
		$data=json_encode($fields);
		$result = $this->httpGet($url,$data);
		echo "<pre>";print_r($result);echo "</pre>";
	}
	function myMenu(){
		$access_token=$this->AccessToken();
		//curl "https://api.weixin.qq.com/cgi-bin/menu/get?access_token=".$access_token";
		$url="https://api.weixin.qq.com/cgi-bin/menu/get?access_token=".$access_token;
		$result = $this->httpGet($url,$data);
		$array=$this->jsontoarray($result);
		echo "<pre>";print_r($array);echo "</pre>";
	}
	function uploadTemp(){
		//curl -F media=@test.jpg "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE"
		$access_token=$this->AccessToken();
		$url="https://api.weixin.qq.com/cgi-bin/media/upload?access_token=$access_token&type=image";
		$fields=array(
			"file"=>"@1.jpg",
		);
		$result = $this->httpGet($url,$fields);
		echo "<pre>";print_r($result);echo "</pre>";
	}
	function downloadTemp(){
		//curl -I -G "https://api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID"
		$access_token=$this->AccessToken();
		$media_id="gME7tm63jTXF4KekdH66nudSSvs_makSxmQ2FVzV-mv3RN-x5gKDMroz7Rkwokz9";
		$url="https://api.weixin.qq.com/cgi-bin/media/get?access_token=$access_token&media_id=$media_id";
		echo "<img src=".$url." >";	
		
	}
	function getMsg(){
		$access_token=$this->AccessToken();
		echo $access_token;
	}
	function sendMsg(){
		$access_token=$this->AccessToken();
		$url="https://api.weixin.qq.com/cgi-bin/groups/getid?access_token=$access_token";
		$array=array(
				"touser"=>"o7qRAs_Y1a1Weitn10DaKTeWGRhk",
				"msgtype"=>"text",
				"text"=>array("content"=>"sendmsg短消息"),
			   );
		$msg=json_encode($array);
		$url = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$access_token;
		$result = $this->httpGet($url,$msg);
		echo "<pre>";print_r($result);echo "</pre>";
	}
}
    A+
发布日期:2015年03月19日  所属分类:未分类

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: