接着我自己自定义了一个表单网页,用来提交total_fee以及其他参数的
<form action="http://www.test.cn/Wxpay/example/jsapi.php" method="post">
<input id="total_fee" value="55"></input>
<input type="submit"/>
</form>
然后在jsapi.php中得到total_fee
$curFee=$_POST["total_fee"];
$tools = new JsApiPay();
$openId = $tools->GetOpenid($curFee);//$curFee传递给GetOpenid()函数进而传递给STATE
public function GetOpenid($total_fee)
{
//通过code获得openid
if (!isset($_GET['code'])){
//触发微信返回code码
$baseUrl = urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING']);
$url = $this->__CreateOauthUrlForCode($baseUrl);
$state=json_encode(array(
"total_fee"=>"$total_fee"
));
$url=str_replace("STATE",$state,$url);
Header("Location: $url");
exit();
} else {
//获取code码,以获取openid
$code = $_GET['code'];
$openid = $this->getOpenidFromMp($code);
$state=$_GET['state'];
$state=str_replace("\\","",$state);
$param=json_decode($state,true);
$state["total_fee"]=$param['total_fee']*100;
return $openid;
}
但是获得的total_fee始终是null,
https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxe688d529fee63f02&redirect_uri=http%3A%2F%2Fwww.test.cn%2FWxpay%2Fexample%2Fjsapi.php&response_type=code&scope=snsapi_base&state={%22total_fee%22:%22%22}#wechat_redirect