header("Content-type:text/html; charset=utf-8");
这个方法是最重要的
function getHttp($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //相当关键,这句话是让curl_exec($ch)返回的结果可以进行赋值给其他的变量进行,json的数据操作,如果没有这句话,则curl返回的数据不可以进行人为的去操作(如json_decode等格式操作)
//CURLOPT_RETURNTRANSFER 将curl_exec()获取的信息以文件流的形式返回,而不是直接输出。
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
$data=curl_exec($ch);

    curl_close($ch);
    $jsonInfo=json_decode($data,true);

  return $jsonInfo;

}

1、第一步:获取code,其中的链接可以通过任何方式生成,也可以用微信公众号菜单
<?php

$APPID=''; //填写高级调用功能的app id
$appsecret=''; //填写高级调用功能的密钥
$REDIRECT_URI='http://www.***.***:81/wechat/test/getcode_2.php';

$scope='snsapi_base';
//$scope='snsapi_userinfo';//需要授权
$state=1;

$url='https://open.weixin.qq.com/connect/oauth2/authorize?appid='.$APPID.'&redirect_uri='.urlencode($REDIRECT_URI).'&response_type=code&scope='.$scope.'&state='.$state.'#wechat_redirect';
echo "url: ".$url;
header("Location:".$url);
?>

2第二步: 按步骤获取:access_token、用户openid、用户基本信息
<?php
$APPID=''; //填写高级调用功能的app id
$APPSECRET=''; //填写高级调用功能的密钥

$code = $_GET["code"];  
//$get_token_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$APPID.'&secret='.$APPSECRET.'&code='.$code.'&grant_type=authorization_code';  
$get_token_url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$APPID&secret=$APPSECRET";  
$res = post($get_token_url);  
$json_obj = json_decode($res,true);  

//根据openid和access_token查询用户信息  
$access_token = $json_obj['access_token'];  

$get_openid_url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$APPID.'&secret='.$APPSECRET.'&code='.$code.'&grant_type=authorization_code';  
$res = post($get_openid_url);  
$json_obj = json_decode($res,true);  
$openid = $json_obj['openid'];  

$get_user_base_info_url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$access_token&openid=$openid&lang=zh_CN";  
$res = post($get_user_base_info_url);  

$param = base64_encode($res);  
$url = "http://218.***.***.10:8151/HiRunAdd.aspx?p=$param";  

header("Location:".$url);  

/*
$get_userinfo_url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
$res = post($get_userinfo_url);

//解析json 
$user_obj = json_decode($res,true); 
$_SESSION['user'] = $user_obj; 
print_r($user_obj); 
echo "<br/>"; 

*/

function post($url){
echo "url: $url <br/>";
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
$res = curl_exec($ch);
curl_close($ch);
echo "resp: $res <br/><br/>";
return $res;
}
?>