1: ユーザーグループの作成
QQ と同様に、WeChat はグループ名の作成、グループ名の変更、グループ名のクエリを行うことができます。これらの操作は、関連するインターフェイスを呼び出して、curl を使用するだけです。送信すると、関連する結(jié)果が得られます
グループを作成する
公開アカウントでは、最大 100 個(gè)のグループの作成がサポートされています。
インターフェース呼び出しリクエストの説明
httpリクエストメソッド:POST(httpsプロトコルを使用してください)https://api.weixin.qq.com/cgi-bin/groups/create?access_token=ACCESS_TOKEN
POSTデータ形式:json
POSTデータ例: {"group":{"name":"test"}}
パラメータの説明
パラメータの説明
access_token 呼び出し元インターフェース認(rèn)証情報(bào)
name グループ名(30文字以內(nèi))
戻りの説明は通常返される JSON データ パケットの例:
{
"group": {
"id": 107,
"name": "test"
}
}
以下は、関連する実裝です。コード
curl を介してデータ パケットを送信する必要があり、返される結(jié)果は StdClass 形式の json データです。stdClass を配列形式に変換する必要があるため、func.php ファイルと関連する操作を作成します。次のグループはこれらの関數(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ù)組,返回一個(gè)數(shù)組形式的access_token
?$access_token=(array)json_decode(curl($access_token_url));
?$access_token=$access_token['access_token'];
?>
上記の access_token を使用すると、やりたいことが何でもできます
まずグループを作成し、ファイルに creategroup.php という名前を付けます
1 インターフェイスの表示
非常にシンプルな HTML に見えますコードは、HTML の基本的な知識(shí)があれば誰でも記述できると思います
では、グループを作成するにはどうすればよいでしょうか?とても簡単です。API リンクを送信してグループを作成するだけです
関連コード
??<?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="新建一個(gè)分組">
?????????<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í)行成功,三秒之后將自動(dòng)跳回主頁')</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: 次に、すべてのグループ名を表示します。最初に公式 WeChat ドキュメントを見てみましょう
すべてのグループをクエリします
インターフェース呼び出しリクエストの説明
httpリクエストメソッド: GET (httpsプロトコルを使用してください) https://api.weixin.qq.com/cgi-bin/groups/get?access_token=ACCESS_TOKEN
パラメータの説明
パラメータの説明
access_token 呼び出しインターフェース認(rèn)証情報(bào)
戻りの説明が正常な場合に返される JSON データ パケットの例:
{
????"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
????????}
????]
}
パラメータの説明
パラメータの説明
groups パブリック プラットフォーム グループ情報(bào)リスト
id WeChat によって割り當(dāng)てられたグループ ID
name グループ名、UTF8 エンコード
グループ內(nèi)のユーザーの數(shù)をカウント
エラーが発生した場合の JSON データ パケットの例 (この例は AppID が無効なエラーです):
公式ドキュメントでは返される JSON データについて非常に明確であり、グループ名には基本的な內(nèi)容が含まれています情報(bào)、グループ ID、グループ名、グループ內(nèi)のユーザー數(shù)。返された json データをページに直接表示することはできません。では、json をどのように解析すればよいでしょうか? WeChat が返すのは stdclass 型の json なので、最初のステップで json を配列に変換する必要があります。この関數(shù)は、json データを stdclass の配列に変換できます。配列と等しくないので、func.php のtransition() 関數(shù)にもそのような関數(shù)があります。
まずページの表示を見て、curl を通じて Tencent から取得したデータがどのようなものかを見てみましょう。上の図では、最初の行は新しいグループを作成する関數(shù)です。次の行には、グループ番號(hào)、グループ名、グループ內(nèi)の人數(shù)が表示されます。では、これはどのように行われるのでしょうか?非常に簡単です。返された json データを取得したら、json データをラップして配列形式にします。では、データパッケージ化を?qū)g裝するにはどうすればよいでしょうか?これも非常に簡単です。解析関數(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ù)組的形式存儲(chǔ)
//$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)建一個(gè)新組</a></th></tr>";
echo "<th>編號(hào)</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: グループ名を変更するにはどうすればよいですか
修改分組名
接口調(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個(gè)字符以內(nèi))
返回說明 正常時(shí)的返回JSON數(shù)據(jù)包示例:
{"errcode": 0, "errmsg": "ok"}
錯(cuò)誤時(shí)的JSON數(shù)據(jù)包示例(該示例為AppID無效錯(cuò)誤):
官方文檔提供了修改分組名的接口,所以我們可以做一個(gè)修改的鏈接和一個(gè)修改組的modify.php文件
根據(jù)官方文檔,我們需要通過組id才能進(jìn)行修改,根據(jù)我們在創(chuàng)建組的時(shí)候傳輸過來的json數(shù)據(jù)中我們可以獲取到組id,所有我們可以通過創(chuàng)建鏈接的方式,當(dāng)點(diǎn)擊鏈接的時(shí)候,會(huì)把組id以get的方式傳送到modify文件中,而modify.php可以通過$_GET的形式接收組id.
我們先寫好這個(gè)傳送組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 "
[$value]刪除修改 | ";
跳轉(zhuǎn)到modify.php頁面后,我們進(jìn)行相關(guān)的處理,在該頁面上,我們?nèi)匀挥幸粋€(gè)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í)行成功,那么會(huì)進(jìn)行彈出提醒框,等待五秒后自動(dòng)跳轉(zhuǎn)回getGroup.php頁面
4:刪除組
組名一般不允許刪除,但是微信平臺(tái)仍然給出了相關(guān)的刪除接口
注意本接口是刪除一個(gè)用戶分組,刪除分組后,所有該分組內(nèi)的用戶自動(dòng)進(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)簽,成功之后會(huì)給予提示,并且跳轉(zhuǎn)回getgroup.php頁面
function delete()
{
$num=$_GET['num'];//接收來自getgroup頁面的編號(hào)
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í)行成功,那么會(huì)返回一個(gè)狀態(tài)值
if($result=curl($delete_url,$data))
{
echo "<script type=\"text/javascript\">alert('執(zhí)行成功,三秒之后將自動(dòng)跳回主頁')</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)行批量移動(dòng)
有時(shí)候我們需要對組內(nèi)的成員進(jìn)行移動(dòng)到其他的組里面,在這點(diǎn)上,微信平臺(tái)也給出了相應(yīng)的接口我們先看官方的文檔說明
批量移動(dò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)識(shí)符openid的列表(size不能超過50)
to_groupid 分組id
返回說明 正常時(shí)的返回JSON數(shù)據(jù)包示例:
{"errcode": 0, "errmsg": "ok"}
從傳遞的json數(shù)據(jù)可以看到,具有一個(gè)open_list和一個(gè)to_groupid,分別表示要移動(dòng)的組成員的openid和將要移動(dòng)的組id.那么我們?nèi)绾伍_始移動(dòng)呢?現(xiàn)在已基本清楚了,只需要把openid傳遞到open_list,將組id傳遞到to_groupid中,然后將包裝好的json數(shù)據(jù)通過curl函數(shù)post過去.在批量分組之前,我們還要知道一件事,如何獲取用戶的相關(guān)信息,這個(gè)信息指的是用戶微信上設(shè)置的性別,省份,國家,語言,所屬組等等的相關(guān)信息,同樣,微信提供了獲取用戶信息的接口,參照上述解決方法就可以獲取到用戶的相關(guān)的信息
以下是獲取到的用戶表
通過第一列的選擇之后,在選擇要分的組,點(diǎn)擊移動(dòng)就可以將用戶移動(dòng)到想要的組里面,下圖是移動(dòng)后的展示
所屬組編號(hào)發(fā)生了變化
源代碼展示
<form action="move.php" method="post">
<table border="1px">
<th>選擇移動(dòng)</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=\"移動(dòng)\">";
?>
</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,那么做成一個(gè)復(fù)選框
case "openid":
echo "<td align=\"center\">
<input type=\"checkbox\" value=\"$value\"name=\"openid[]\"/>
</td>";
case "subscribe"://忽略相關(guān)的描述,不對這個(gè)字段生成列
break;
//如果是性別,性別值1代表男,0代表女,2代表并未填寫
case "sex":
if($value==1)
{
echo "<td>男</td>"; }
else if($value==0)
{
echo "<td>女</td>"; }
else
{
echo "<td>暫未填寫</td>"; }
break;
//如果是頭像鏈接,那么生成一個(gè)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
//此程序用于移動(dòng)分組
$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('移動(dòng)成功')</script>
<script type=\"text/javascript\">
setTimeout(\"window.location.href='getgroup.php'\",5000);
</script>
";
}
?>
以上がWeChat 開発ユーザー グループの概要の詳細(xì)內(nèi)容です。詳細(xì)については、PHP 中國語 Web サイトの他の関連記事を參照してください。