微信開發(fā)之用戶組的示例分析

這篇文章主要介紹微信開發(fā)之用戶組的示例分析,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

網(wǎng)站建設(shè)哪家好,找成都創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、小程序設(shè)計(jì)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了北川羌族免費(fèi)建站歡迎大家使用!

1:創(chuàng)建用戶組

微信跟QQ一樣可以創(chuàng)建組名,刪除組名,修改組名,查詢組名,這些操作都是一系列的接口,只需要調(diào)用相關(guān)的接口,并以curl的形式進(jìn)行發(fā)送,便可以獲得相關(guān)的結(jié)果

創(chuàng)建分組

一個公眾賬號,最多支持創(chuàng)建100個分組。

接口調(diào)用請求說明

http請求方式: POST(請使用https協(xié)議)https://api.weixin.qq.com/cgi-bin/groups/create?access_token=ACCESS_TOKEN

POST數(shù)據(jù)格式:json

POST數(shù)據(jù)例子:{"group":{"name":"test"}}

參數(shù)說明

參數(shù) 說明

access_token 調(diào)用接口憑證

name 分組名字(30個字符以內(nèi))

返回說明 正常時(shí)的返回JSON數(shù)據(jù)包示例:

{

"group": {

"id": 107,

"name": "test"

}

}

下面是相關(guān)代碼的實(shí)現(xiàn)

我們需要通過curl的形式將數(shù)據(jù)包發(fā)送過去,返回的結(jié)果是一個StdClass形式的json數(shù)據(jù),我們需要將stdClass進(jìn)行轉(zhuǎn)換為數(shù)組形式,所以我們創(chuàng)建一個func.php文件,后面的組的相關(guān)操作都基于這些函數(shù)

<?php 
       //設(shè)定appID 和secret
	   define ("APPID","wx70fe852945a945b6",true);
	   define ("SECRET",'d05c2fc161d71c8317331a39044a7d93',true);
	   $APPID="wx70fe852945a945b6";
	   $SECRET="d05c2fc161d71c8317331a39044a7d93";
      function curl($url,$data=null)
	  {
		   //初始化
		   $curl=curl_init();
		   curl_setopt($curl, CURLOPT_URL, $url);//設(shè)置傳輸鏈接
           curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);//設(shè)置SSL憑證
           curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false );
           curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//是否顯示在瀏覽器上
		   if(!empty($data))
		   {
			   //進(jìn)行post數(shù)據(jù)
			   curl_setopt($curl,CURLOPT_POST,1);
			   curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
			   }
			$result=curl_exec($curl);//執(zhí)行curl
			curl_close($curl);//關(guān)閉curl
			return $result;
		  }
		   function transition ($data)
	  {
		    if(is_object($data))
			{
				  $data=(array)$data;
				}
			if(is_array($data))
			{
				 foreach($data as $key=>$value)
				 {
					   $data[$key]=transition($value);
					 }
				}
				return $data;
		  }
		  //將多維數(shù)組轉(zhuǎn)成字符串
		   function recount($result)
		{
			    if(is_array($result))
				{
			   foreach($result as $key=>$value)
			   {    
				  recount($value); 
				  return $value;
				   }
				  
				}
				
				
			}
		  //access_token鏈接地址
		  $access_token_url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$APPID&secret=$SECRET";
		 //獲取access_token.將返回的json格式轉(zhuǎn)成數(shù)組,返回一個數(shù)組形式的access_token
		 $access_token=(array)json_decode(curl($access_token_url));
		 $access_token=$access_token['access_token'];
		
?>

利用上面的access_token我們便可以做我們想做的任何事了

首先我們進(jìn)行組的創(chuàng)建,我們將文件命名為creategroup.php

1  界面展示

看起來很簡單的HTML的代碼,相信有HTML基礎(chǔ)的都能寫出

那么如何創(chuàng)建組呢?很簡單,我們只需將創(chuàng)建組的API鏈接提交過去就可以了

相關(guān)的代碼

  <?php  echo '<meta http-equiv="content-type" content="text/html; charset=utf-8">'; 
?>
<!doctype html>
<html>
<head>
<title>無標(biāo)題文檔</title>
</head>
<body>
      <form action="#" method="post">
         <p>新建分組</p>
         <input type="text" name="tag" placeholder="新建一個分組">
         <input type="submit" name="-1" id="sub" value="提交">
         
      </form>
      <?php 
	     //如果提交成功,那么進(jìn)行組的創(chuàng)建
	         if(isset($_POST[-1]))
			  {
	          
			     function create_group()
				{    require "func.php";
					 $create_url="https://api.weixin.qq.com/cgi-bin/tags/create?access_token=$access_token";
			         $tag=$_POST['tag'];//獲取組名
					$poststr="
					          {
								  \"tag\":
								      {
										  \"name\":\"$tag\";
									  }
								  }
					";		
					if($result=curl($create_url,$poststr))
	{
		 echo "<script type=\"text/javascript\">alert('執(zhí)行成功,三秒之后將自動跳回主頁')</script>";
		 //設(shè)置跳轉(zhuǎn)回主頁
		  echo "<script type=\"text/javascript\">setTimeout(window.navigate(\"getgroup.php\"),3000)</script>";
		}
	else
	{echo "<script type=\"text/javascript\">alert('執(zhí)行失敗')</script>";}
	
					}
					create_group();
			  }
			  
	  ?>
</body>
</html>

2:接下來是展示所有的組名,我們先看看微信官方的文檔

查詢所有分組

接口調(diào)用請求說明

http請求方式: GET(請使用https協(xié)議)https://api.weixin.qq.com/cgi-bin/groups/get?access_token=ACCESS_TOKEN

參數(shù)說明

參數(shù) 說明

access_token 調(diào)用接口憑證

返回說明 正常時(shí)的返回JSON數(shù)據(jù)包示例:

{
    "groups": [
        {
            "id": 0, 
            "name": "未分組", 
            "count": 72596
        }, 
        {
            "id": 1, 
            "name": "黑名單", 
            "count": 36
        }, 
        {
            "id": 2, 
            "name": "星標(biāo)組", 
            "count": 8
        }, 
        {
            "id": 104, 
            "name": "華東媒", 
            "count": 4
        }, 
        {
            "id": 106, 
            "name": "★不測試組★", 
            "count": 1
        }
    ]
}

參數(shù)說明

參數(shù) 說明

groups 公眾平臺分組信息列表

id 分組id,由微信分配

name 分組名字,UTF8編碼

count 分組內(nèi)用戶數(shù)量

錯誤時(shí)的JSON數(shù)據(jù)包示例(該示例為AppID無效錯誤):

官方文檔對于返回的json數(shù)據(jù)已經(jīng)很明了了,一個組名里邊包含了基本的信息,組id,組名以及組內(nèi)的用戶數(shù)量。我們無法直接將返回的json數(shù)據(jù)直接顯示到頁面上,那么我們?nèi)绾谓馕鰆son呢?微信給我們返回的是一個stdclass類型的json,所以第一步我們需要將json轉(zhuǎn)為數(shù)組,php中有一個json_decode()函數(shù),此函數(shù)能夠?qū)son數(shù)據(jù)轉(zhuǎn)為stdclass的數(shù)組,stdclass并不等于數(shù)組,所以我們還要講stdclass轉(zhuǎn)化為array形式,在func.php中transition()函數(shù)就有這樣的功能。

我們先看看頁面顯示樣子,看看我們通過curl從騰訊上獲取的數(shù)據(jù)是什么樣的

上圖我們創(chuàng)建了一個表,第一行是一個創(chuàng)建新組的功能,在以下的行中,我們能夠看到組編號,組名,以及組內(nèi)人數(shù)。那么這個是如何做到的呢?很簡單,當(dāng)我們已經(jīng)獲取到返回回來的json數(shù)據(jù)之后,我們對json進(jìn)行數(shù)據(jù)包裝,把json做成數(shù)組形式。那么如何實(shí)現(xiàn)數(shù)據(jù)包裝呢?也很簡單,我們不斷的調(diào)用解析函數(shù),將它最終變?yōu)閿?shù)組的形式,然后遍歷這個數(shù)組就行了。

展示源代碼:getGroup.php

 <body>
     <div id="box">
    <div id="group">
<?php 
      require "func.php";
  $groupurl="https://api.weixin.qq.com/cgi-bin/groups/get?access_token={$access_token}";
  $result=json_decode(curl($groupurl));//獲取包裝之后的數(shù)據(jù),以數(shù)組的形式存儲
  //$result=curl($groupurl);
   //將STDclass類型轉(zhuǎn)為數(shù)組類型
  function G_transition ($data)
  {
    if(is_object($data))
{
  $data=(array)$data;
}
if(is_array($data))
{
 foreach($data as $key=>$value)
 {
   $data[$key]=G_transition($value);
 }
}
return $data;
  }
  $result=G_transition($result);  
  function G_recount($result)
{
    if(is_array($result))
{
   foreach($result as $key=>$value)
   {    
  G_recount($value); 
  return $value;
   }
}
}
$resultG=G_recount($result);
echo "<table border=\"1px dashed\" bordercolor=\"#FF3333\">";
     echo "<tr><th colspan=\"3\"><a href=\"createGroup.php\">創(chuàng)建一個新組</a></th></tr>";
 echo "<th>編號</th><th>組名</th><th>人數(shù)</th>";
for($i=0;$i<count($resultG);$i++)
{
  echo "<tr>";
 foreach ($resultG[$i] as $key=>$value)
   { 
  if($key=='id')
  {
  echo "<td  align=\"center\"title=\"\">[$value]<a href=\"delete.php?num=$value\">刪除</a><a href=\"modify.php?num=$value\">修改</a></td>";
  }
  else 
  {
        echo "<td> $value</td>";
  }
}
echo "</tr>";
}
echo "</table>";
?>
      </div>
      <hr/> 
       <div>
          <hr/>

3:我們?nèi)绾涡薷姆纸M名

修改分組名

接口調(diào)用請求說明

http請求方式: POST(請使用https協(xié)議)https://api.weixin.qq.com/cgi-bin/groups/update?access_token=ACCESS_TOKEN

POST數(shù)據(jù)格式:json

POST數(shù)據(jù)例子:{"group":{"id":108,"name":"test2_modify2"}}

參數(shù)說明

參數(shù) 說明

access_token 調(diào)用接口憑證

id 分組id,由微信分配

name 分組名字(30個字符以內(nèi))

返回說明 正常時(shí)的返回JSON數(shù)據(jù)包示例:

{"errcode": 0, "errmsg": "ok"}

錯誤時(shí)的JSON數(shù)據(jù)包示例(該示例為AppID無效錯誤):

官方文檔提供了修改分組名的接口,所以我們可以做一個修改的鏈接和一個修改組的modify.php文件

根據(jù)官方文檔,我們需要通過組id才能進(jìn)行修改,根據(jù)我們在創(chuàng)建組的時(shí)候傳輸過來的json數(shù)據(jù)中我們可以獲取到組id,所有我們可以通過創(chuàng)建鏈接的方式,當(dāng)點(diǎn)擊鏈接的時(shí)候,會把組id以get的方式傳送到modify文件中,而modify.php可以通過$_GET的形式接收組id.

我們先寫好這個傳送組id的鏈接,

      for($i=0;$i<count($resultG);$i++)
{
  echo "<tr>";
 foreach ($resultG[$i] as $key=>$value)
   { 
  if($key=='id')
  {
  echo "<td  align=\"center\"title=\"\">[$value]<a href=\"delete.php?num=$value\">刪除</a><a href=\"modify.php?num=$value\">修改</a></td>";
  }
  else 
  {
        echo "<td> $value</td>";
  }
}
echo "</tr>";
}

代碼中,我們對返回的數(shù)組進(jìn)行遍歷,如果發(fā)現(xiàn)key值是id,那么我們將值獲取過來并且加入到鏈接尾部,注意get方式的寫法

echo "<td  align=\"center\"title=\"\">[$value]<a href=\"delete.php?num=$value\">刪除</a><a href=\"modify.php?num=$value\">修改</a></td>";

跳轉(zhuǎn)到modify.php頁面后,我們進(jìn)行相關(guān)的處理,在該頁面上,我們?nèi)匀挥幸粋€HTML輸入框

代碼如下:

    <form action="#" method="post">
      <p>更新組名</p>
      <input type="hidden" name="num" value="<?php echo $_GET['num']?>">
      <input  type="text"id="modify" name="name">
      <input type="submit" value="修改" name="-1">
  </form>
<?php 
    //此程序用于修改標(biāo)簽組
function modify()
{     $num=$_POST['num'];
      $name=$_POST['name'];
  require "func.php";
  $modify_url="https://api.weixin.qq.com/cgi-bin/tags/update?access_token=$access_token";
  //post過去的數(shù)據(jù)
  $poststr="
         {
   \"tag\":
         {
  \"id\":\"$num\",
  \"name\":\"$name\"
 }
 }
  ";
  $result=(array)json_decode(curl($modify_url,$poststr));
  $result=$result['errmsg'];
  if($result=='ok')
  {
     echo "<script type=\"text/javascript\">
         alert(\"$result\");
      </script>";
//進(jìn)行頁面跳轉(zhuǎn)
 echo "<script type=\"text/javascript\">
             setTimeout(window.location.href=\"getgroup.php\",3000);
    </script>";
  }
else
{
 echo "<script type=\"text/javascript\">
         alert('wrong');
      </script>";
}
}
if(isset($_POST['-1']))
{
modify();
}
?>

如果執(zhí)行成功,那么會進(jìn)行彈出提醒框,等待五秒后自動跳轉(zhuǎn)回getGroup.php頁面

4:刪除組

組名一般不允許刪除,但是微信平臺仍然給出了相關(guān)的刪除接口

注意本接口是刪除一個用戶分組,刪除分組后,所有該分組內(nèi)的用戶自動進(jìn)入默認(rèn)分組。 接口調(diào)用請求說明

http請求方式: POST(請使用https協(xié)議)https://api.weixin.qq.com/cgi-bin/groups/delete?access_token=ACCESS_TOKEN

POST數(shù)據(jù)格式:json

POST數(shù)據(jù)例子:{"group":{"id":108}}

參數(shù)說明

參數(shù) 說明

access_token 調(diào)用接口憑證

group 分組

id 分組的id

返回說明 正常時(shí)的返回JSON數(shù)據(jù)包示例:

通過傳遞的json數(shù)據(jù),我們只需要將組id進(jìn)行傳遞到delete.php頁面并進(jìn)行相關(guān)的刪除操作即可

代碼顯示:

    <?php 
    //該段程序用來刪除組標(biāo)簽,成功之后會給予提示,并且跳轉(zhuǎn)回getgroup.php頁面
function delete()
{
$num=$_GET['num'];//接收來自getgroup頁面的編號
require "func.php";
$delete_url="https://api.weixin.qq.com/cgi-bin/tags/delete?access_token=$access_token";
$data=json_encode(array("tag"=>array("id"=>$num)));
//如果curl函數(shù)執(zhí)行成功,那么會返回一個狀態(tài)值
if($result=curl($delete_url,$data))
{
 echo "<script type=\"text/javascript\">alert('執(zhí)行成功,三秒之后將自動跳回主頁')</script>";
 //設(shè)置跳轉(zhuǎn)回主頁
  echo "<script type=\"text/javascript\">setTimeout(window.history.back(-1),8000)</script>";
}
else
{echo "<script type=\"text/javascript\">alert('執(zhí)行失敗')</script>";}
}
delete();
?>

5:對組成員進(jìn)行批量移動

有時(shí)候我們需要對組內(nèi)的成員進(jìn)行移動到其他的組里面,在這點(diǎn)上,微信平臺也給出了相應(yīng)的接口我們先看官方的文檔說明

批量移動用戶分組

接口調(diào)用請求說明

http請求方式: POST(請使用https協(xié)議)https://api.weixin.qq.com/cgi-bin/groups/members/batchupdate?access_token=ACCESS_TOKEN

POST數(shù)據(jù)格式:json

POST數(shù)據(jù)例子:{"openid_list":["oDF3iYx0ro3_7jD4HFRDfrjdCM58","oDF3iY9FGSSRHom3B-0w5j4jlEyY"],"to_groupid":108}

參數(shù)說明

參數(shù) 說明

access_token 調(diào)用接口憑證

openid_list 用戶唯一標(biāo)識符openid的列表(size不能超過50)

to_groupid 分組id

返回說明 正常時(shí)的返回JSON數(shù)據(jù)包示例:

{"errcode": 0, "errmsg": "ok"}

從傳遞的json數(shù)據(jù)可以看到,具有一個open_list和一個to_groupid,分別表示要移動的組成員的openid和將要移動的組id.那么我們?nèi)绾伍_始移動呢?現(xiàn)在已基本清楚了,只需要把openid傳遞到open_list,將組id傳遞到to_groupid中,然后將包裝好的json數(shù)據(jù)通過curl函數(shù)post過去.在批量分組之前,我們還要知道一件事,如何獲取用戶的相關(guān)信息,這個信息指的是用戶微信上設(shè)置的性別,省份,國家,語言,所屬組等等的相關(guān)信息,同樣,微信提供了獲取用戶信息的接口,參照上述解決方法就可以獲取到用戶的相關(guān)的信息

以下是獲取到的用戶表

通過第一列的選擇之后,在選擇要分的組,點(diǎn)擊移動就可以將用戶移動到想要的組里面,下圖是移動后的展示

所屬組編號發(fā)生了變化

源代碼展示

<form action="move.php" method="post">
          <table border="1px">
               <th>選擇移動</th>
               <th>昵稱</th>
               <th>性別</th>
               <th>語言</th>
               <th>所在城市</th>
               <th>省份</th>
               <th>國家</th>
               <th>頭像</th>
               <th>加入時(shí)間</th>
               <th>備注名</th>
               <th>所屬組</th>
               <th rowspan="10">
                     <?php 
    echo " <select name=\"group\">";
 $count=count($resultG);
 foreach($resultG as $key)//遍歷包裝好的json數(shù)據(jù),已經(jīng)轉(zhuǎn)成了多維數(shù)組
 {
 echo "<option value=\"$key[id]\" >$key[name] </option>";                  $count--;  //獲取組ID
 }
echo "</select>"; 
echo "<input type=\"submit\" name=\"-1\" value=\"移動\">";
?>
               </th>
              <?php 
       foreach($list['data'] as $key)
   {
      //$list['data']是已經(jīng)包裝好的json數(shù)據(jù),將原來的stdclass轉(zhuǎn)為了多維數(shù)組
     // $result=count($key);
      //var_dump($result);
    foreach($key as $value)
{
 echo "<tr>";
   $info_url="https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$value&lang=zh_CN";
   $info=transition(json_decode(curl($info_url)));                              //var_dump($info);
   //echo "<hr>";
  //  global $userinfo;
//$userinfo=$info;
//var_dump($userinfo);
   foreach($info as $key=>$value)
   {
   //對表格進(jìn)行相關(guān)的修飾,進(jìn)行相關(guān)的判斷
    switch($key)
{ 
  //如果是id,那么做成一個復(fù)選框
 case "openid":
     echo "<td align=\"center\">
 <input type=\"checkbox\" value=\"$value\"name=\"openid[]\"/>
      </td>";
     case "subscribe"://忽略相關(guān)的描述,不對這個字段生成列
 break;
 //如果是性別,性別值1代表男,0代表女,2代表并未填寫
 case "sex":
      if($value==1)
  {
 echo "<td>男</td>"; }
  else if($value==0)
  {
 echo "<td>女</td>";  }
 else
   {
   echo "<td>暫未填寫</td>"; }
  break;
 //如果是頭像鏈接,那么生成一個50*50像素的圖片
 case "headimgurl";
 echo "<td>
      <img src=\"$value\" height=\"50px\" width=\"\50px\">
       </td>";
 break;
 //以下是默認(rèn)列
 case "nickname":
 case "language":
 case "city":
 case "province":
 case "country":
 case "subscribe_time":
 echo "<td>$value</td>";
 break;
 //如果remark的值為空,那么備注名是空值
 case "remark":
  if(empty($value))
  {
   echo "<td>暫無</td>";
  }
else
{
echo "<td>$value</td>";
}
break;
case "groupid":
      echo"<td>$value</td>";
  break;
}
   }
 echo "</tr>";
}
   }
  ?>
          </table>
        </form>
       </div>
       <hr / color=\"#9900CCd\">
   </div>
</body>
move.php的代碼 
  <?php  
    //此程序用于移動分組
  $member=array();
 $member=$_POST['openid'];//獲取選中的openid
 $groupid=$_POST['group'];//獲取組id
require "func.php";
$move_url="https://api.weixin.qq.com/cgi-bin/groups/members/update?access_token=$access_token";
    for($i=0;$i<count($member);$i++)
{
  $poststr="{\"openid\":\"$member[$i]\",\"to_groupid\":$groupid}";
   $result=curl($move_url,$poststr);
}
$result=(array)json_decode($result);
if($result['errmsg']=='ok')
{
 echo "
   <script type=\"text/javascript\">window.alert('移動成功')</script>
   <script type=\"text/javascript\">
    setTimeout(\"window.location.href='getgroup.php'\",5000);
   </script>
 ";
}
?>

以上是“微信開發(fā)之用戶組的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

本文名稱:微信開發(fā)之用戶組的示例分析
當(dāng)前路徑:http://muchs.cn/article10/jpshdo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、網(wǎng)站策劃、營銷型網(wǎng)站建設(shè)網(wǎng)站設(shè)計(jì)公司、關(guān)鍵詞優(yōu)化、Google

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化