获取企业号应用
根据开发文档,可以看出,需要两个参数access_token、agentid,agentid在哪里获取,在我上一篇博文里已经讲过了,这里就不重复了,直接上代码
public function get() {
$agentid = intval($_GET['agentid']);
$info = $this->get_company_info($this->get_access_token($this->corpid, $this->corpsecret), $agentid);
}
private function get_company_info($access_token, $agentid) {
$url = 'https://qyapi.weixin.qq.com/cgi-bin/agent/get?access_token=' . $access_token . '&agentid=' . $agentid;
$array_result = json_decode($this->https_request($url), TRUE);
return $array_result;
}
请求API后返回应用的信息如下
设置企业号应用
根据开发文档,可以看出,需要一堆参数,这一堆参数一个个填上然后请求即可
先做一个表单,把参数传送过来
<form action="<{$set_url}>" method="POST">
<table>
<tr>
<td>应用ID</td>
<td><input type="text" name="agentid" /></td>
</tr>
<tr>
<td>企业应用是否打开地理位置上报</td>
<td>
<select name="report_location_flag">
<option value="0">不上报</option>
<option value="1">进入会话上报</option>
<option value="2">持续上报</option>
</select>
</td>
</tr>
<tr>
<td>应用名称</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>企业应用详情</td>
<td>
<input type="text" name="description" />
</td>
</tr>
<tr>
<td>企业应用可信域名</td>
<td><input type="text" name="redirect_domain"></td>
</tr>
<tr>
<td>是否接收用户变更通知</td>
<td>
<select name="isreportuser">
<option value="0">不接收</option>
<option value="1">接收</option>
</select>
</td>
</tr>
<tr>
<td>是否上报用户进入应用事件</td>
<td>
<select name="isreportenter">
<option value="0">不接收</option>
<option value="1">接收</option>
</select>
</td>
</tr>
<tr>
<td>主页型应用url</td>
<td>
<input type="text" name="home_url"/>
</td>
</tr>
<tr>
<td>关联会话url</td>
<td>
<input type="text" name="chat_extension_url"/>
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="设置企业号应用"/>
</td>
</tr>
</table>
</form>
获取参数并请求设置(中文参数乱码解决方法:urlencode->json_encode->urldecode)
public function set() {
$agent_id = (int)get_var_value("agentid");
$report_location_flag = (int)get_var_value("report_location_flag");
$redirect_domain = get_var_value("redirect_domain");
$isreportuser = (int)get_var_value("isreportuser");
$isreportenter = (int)get_var_value("isreportenter");
$home_url = get_var_value("home_url");
$chat_extension_url = get_var_value("chat_extension_url");
$desc = get_var_value("description");
$name = get_var_value("name");
$post_arr = array(
"logo_mediaid" => $_COOKIE["media_id"],
"agentid" => $agent_id,
"name" => urlencode($name),
"description" => urlencode($desc),
"report_location_flag" => $report_location_flag,
"redirect_domain" => $redirect_domain,
"isreportuser" => $isreportuser,
"isreportenter" => $isreportenter,
"home_url" => $home_url,
"chat_extension_url" => $chat_extension_url,
);
$token = $this->get_access_token($this->corpid, $this->corpsecret);
$tmp_flag = $this->set_company_info($token, $post_arr);
var_dump($tmp_flag);
if($tmp_flag) {
echo "设置成功";
} else {
echo "设置失败";
}
}
private function set_company_info($access_token, $post_data) {
$url = 'https://qyapi.weixin.qq.com/cgi-bin/agent/set?access_token=' . $access_token;
- $post_str = json_encode($post_data);
$post_str = urldecode($post_str);
var_dump($post_str);
$array_result = json_decode($this->https_request($url, $post_str), TRUE);
var_dump($array_result);
if($array_result['errmsg'] == "ok") {
return TRUE;
} else {
return FALSE;
}
}