使用PHP的curl连接微信的服务器,微信菜单
分类:美高梅游戏官网网站

Get提交获取数据

$APPID = '';
$AppSecret = '';
$tokenUrl=";
$getArr=array();
$tokenArr=json_decode($this->send_post($tokenUrl,$getArr,"GET"));
$access_token=$tokenArr->access_token;
$uid = I('uid');
$name = I('name');
$path="pages/index/index?uid=".$uid.'&name='.$name;
$width=430;
$post_data='{"path":"'.$path.'","width":'.$width.'}';
$url=";
$result=$this->api_notice_increment($url,$post_data);
header('Content-Type: image/jpeg');
echo $result;
function send_post($url, $post_data,$method='POST') {
$postdata = http_build_query($post_data);
$options = array(
'http' => array(
'method' => $method, //or GET
'header' => 'Content-type:application/x-www-form-urlencoded',
'content' => $postdata,
'timeout' => 15 * 60 // 超时时间(单位:s)
)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return $result;
}
function api_notice_increment($url, $data){
$ch = curl_init();
$header = "Accept-Charset: utf-8";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$tmpInfo = curl_exec($ch);
curl_close($ch);
if (curl_errno($ch)) {
return false;
}else{
return $tmpInfo;
}
}

微信用PHP开发越来越火了,很多人都为了学习微信的公众号开发来学习PHP课程。那么,我们现在明白了未来我们要做什么。我们将会使用到curl相关的东西来连接微信的服务器。

namespace HomeController;

 /**
  * @desc 获取access_token
  * @return String access_token
  */ 
 function getAccessToken(){ 
     $AppId = '1232assad13213123'; 
     $AppSecret = '2312312321adss3123213'; 
     $getUrl = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$AppId.'&secret='.$AppSecret; 
     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_URL, $getUrl); 
     curl_setopt($ch, CURLOPT_HEADER, 0); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
     curl_setopt($ch, CURL_SSLVERSION_SSL, 2); 
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
美高梅游戏官网网站 ,     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
     $data = curl_exec($ch); 
     $response = json_decode($data); 
     return $response->access_token; 
 } 

我们可以定义三个函数:

use ThinkController;

Post提交获取数据

第一个,用来获取token。

define('appid','wx33584f71b4a84fa9');
define('appsecret','');

  /**
   * @desc 实现天气内容回复
   */ 
public function testWeixin(){ 
$access_token = $this->getAccessToken(); 
$customMessageSendUrl = '; 
$description = '今天天气的详细信息(从第三方获取)。'; 
$url = ''; 
$picurl = ''; 
$postDataArr = array( 
'touser'=>'OPENID', 
'msgtype'=>'news', 
'news'=>array( 
    'articles'=>array( 
  'title'=>'当天天气', 
  'description'=>$description, 
  'url'=>$url, 
  'picurl'=>$picurl, 
    ), 
), 
); 
$postJosnData = json_encode($postDataArr); 
$ch = curl_init($customMessageSendUrl);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postJosnData);   
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
$data = curl_exec($ch);
var_dump($data); 

第二个,拿到微信的access_token后,我们用来post传数据。

class MenuController extends Controller
{
private function access_token(){
$url="."";
$ch=curl_init();//初始化
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
$output=curl_exec($ch);
curl_close($ch);
$jsoninfo=json_decode($output,true);
$access_token=$jsoninfo['access_token'];
print_r($access_token.'n');
return $access_token;
}

例子相对来说比较简单也没有什么好详细分析的了,大家照抄就可以实现我们想要的功能了.

第三个,拿到微信的access_token后,我们用来做get得到微信的数据。

function set_menu(){
//菜单
$data = ' {
"button":[
{
"name":"PHP100",
"sub_button":[
{
"type":"click",
"name":"互联网热点",
"key":"A1"
},
{
"type":"click",
"name":"悬赏与外包",
"key":"A2"
},

你可以把这三个函数收藏起来,需要的时候,就可以用这三个函数来发数据哟。

{
"type":"click",
"name":"个人中心",
"key":"A5"
}]
},
{
"name":"编程助手",
"sub_button":[
{
"type":"click",
"name":"函数查询",
"key":"B2"
},
{
"type":"click",
"name":"热门书籍",
"key":"B3"
},
{
"type":"click",
"name":"教程与讲座",
"key":"B4"
},
{
"type":"click",
"name":"培训与课程",
"key":"B5"
}]
},
{
"name":"自娱自乐",
"sub_button":[
{
"type":"click",
"name":"发布心情",
"key":"C1"
},
{
"type":"click",
"name":"开心段子",
"key":"C2"
},
{
"type":"click",
"name":"免费与优惠",
"key":"C3"
}]
}]
}';

//定义函数得到access_token

//make post url

function get_token() {

$url="";
$ch=curl_init();//初始化
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);
curl_exec($ch);
curl_close($ch);
print_r($url);
}

//初始化curl

}

$ch = curl_init();

//你可以在前面定义两个常量,一个是APP_ID,一个是APP_SECRET

$url = '' . APP_ID . '&secret=' . APP_SECRET;

//设置连接的参数,这个是设置URL

curl_setopt($ch, CURLOPT_URL, $url);

//设置curl为返回值类型,将得到的数据返回给变量

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//不处理header

curl_setopt($ch, CURLOPT_HEADER, 0);

//超时时间定义为10

curl_setopt($ch, CURLOPT_TIMEOUT, 10);

//向微信的url发送参数得到token

$output = curl_exec($ch);

//关闭curl连接

curl_close($ch);

//json字符串变为数组

$obj = json_decode($output, true);

//返回access_token

return $obj['access_token'];

}

function post($url, $data) {

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//声明使用POST方式来进行发送

curl_setopt($ch, CURLOPT_POST, 1);

//发送什么数据呢

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

//忽略证书

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

curl_setopt($ch, CURLOPT_HEADER, 0);

curl_setopt($ch, CURLOPT_TIMEOUT, 10);

$output = curl_exec($ch);

curl_close($ch);

return $output;

}

function get($url) {

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_HEADER, 0);

curl_setopt($ch, CURLOPT_TIMEOUT, 10);

$output = curl_exec($ch);

curl_close($ch);

return $output;

}

本文由美高梅网址发布于美高梅游戏官网网站,转载请注明出处:使用PHP的curl连接微信的服务器,微信菜单

上一篇:PHP中函数gzuncompress无法使用的解决方法,php在线 下一篇:没有了
猜你喜欢
热门排行
精彩图文