微信公众号开发之通过获取token等信息

<?php
 /**
* 发送post请求
* @param string $url
* @param string $param
* @return bool|mixed
*/
function request_post($url = '', $param = '')
{
if (empty($url) || empty($param)) {
return false;
}
$postUrl = $url;
$curlPost = $param;
$ch = curl_init(); //初始化curl
curl_setopt($ch, CURLOPT_URL, $postUrl); //抓取指定网页
curl_setopt($ch, CURLOPT_HEADER, 0); //设置header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_POST, 1); //post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);//可能是post的数据?
$data = curl_exec($ch); //运行curl
curl_close($ch);
return $data;
}
$data= request_post("https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token=".$token,"template_id_short=TM00001");
$content=json_decode($data);
var_dump($content);
echo "上面是POST获得模板ID</br>";
/**
* 发送get请求
* @param string $url
* @return bool|mixed
*/
function request_get($url = '')
{
if (empty($url)) {
return false;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
$tokenecho="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxbabbc15d9ee67cb7&secret=127534edc3f27ae3e10eb02e2e9887c3";//获取token
$hangye_url="https://api.weixin.qq.com/cgi-bin/template/get_industry?access_token=".$token;//获取行业信息
$mubanlist_url="https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=".$token;
$data2=request_get($tokenecho);
echo "输出token</br>";
var_dump(json_decode($data2));
echo "</br/>";

echo "输出行业信息</br>";
$data3=request_get($hangye_url);
var_dump(json_decode($data3));
echo "</br/>";
echo "输出模板列表</br>";
$data4=request_get($mubanlist_url);
var_dump(json_decode($data4));
echo "</br/>";
?>

posted @ 2017-02-20 14:42  虚幻的街景  阅读(1194)  评论(0编辑  收藏  举报