实例运行注意事项:
- Wxpay.config.php设置APPID、MCHID、KEY、APPSECRET(按照dome中的要求配置就OK)
- 下载证书到cert目录下(到这一步dome实例就可以运行了)
- 运行时报错:call to undefind function libxml_disable_entity_loader() in;注意是你的服务器没有配置libxml2.dll造成的。要是实在解决不了可以注释掉Wxpay.data.php中(这种解决办法不提倡,会造成安全漏洞)
//将XML转为array
//禁止引用外部xml实体
//libxml_disable_entity_loader(false);
$this->values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $this->values;
- 报错:return_msg:JSAPI支持必须传openid,Wxpay.JsApiPay.php中"Uncaught exception ' WxPayException' with message ' 参数错误' in ''。修改WxPay.JsApiPay.php中curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,TRUE);TRUE为FALSE;curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,2);2为FALSE
网站传参到微信支付接口(GET)传参解决方案:
- 传参路径:http://域名/Wxpay/example/jsapi.php?state=参数
- 修改WxPay.JsApiPay.php中JsApiPay类函数参数
public function GetOpenid()
{
//通过code获得openid
if (!isset($_GET['code'])){
//触发微信返回code码
$baseUrl = urlencode('http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']);
$url = $this->__CreateOauthUrlForCode($baseUrl,$_GET['state']);
Header("Location: $url");
exit();
} else {
//获取code码,以获取openid
$code = $_GET['code'];
$state=$_GET['state'];
$openid = $this->GetOpenidFromMp($code,$state);
return $openid;
}
}
/**
*
* 通过code从工作平台获取openid机器access_token
* @param string $code 微信跳转回来带上的code
*
* @return openid
*/
public function GetOpenidFromMp($code,$state)
{
$url = $this->__CreateOauthUrlForOpenid($code,$state);
//初始化curl
$ch = curl_init();
//设置超时
curl_setopt($ch, CURLOPT_TIMEOUT, $this->curl_timeout);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,FALSE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
if(WxPayConfig::CURL_PROXY_HOST != "0.0.0.0"
&& WxPayConfig::CURL_PROXY_PORT != 0){
curl_setopt($ch,CURLOPT_PROXY, WxPayConfig::CURL_PROXY_HOST);
curl_setopt($ch,CURLOPT_PROXYPORT, WxPayConfig::CURL_PROXY_PORT);
}
//运行curl,结果以jason形式返回
$res = curl_exec($ch);
curl_close($ch);
//取出openid
$data = json_decode($res,true);
$this->data = $data;
$openid = $data['openid'];
return $openid;
}
/**
*
* 构造获取code的url连接
* @param string $redirectUrl 微信服务器回跳的url,需要url编码
*
* @return 返回构造好的url
*/
private function __CreateOauthUrlForCode($redirectUrl,$state)
{
$urlObj["appid"] = WxPayConfig::APPID;
$urlObj["redirect_uri"] = "$redirectUrl";
$urlObj["response_type"] = "code";
$urlObj["scope"] = "snsapi_base";
$urlObj["state"] = $state."#wechat_redirect";
$bizString = $this->ToUrlParams($urlObj);
return "https://open.weixin.qq.com/connect/oauth2/authorize?".$bizString;
}
/**
*
* 构造获取open和access_toke的url地址
* @param string $code,微信跳转带回的code
*
* @return 请求的url
*/
private function __CreateOauthUrlForOpenid($code,$state)
{
$urlObj["appid"] = WxPayConfig::APPID;
$urlObj["secret"] = WxPayConfig::APPSECRET;
$urlObj["code"] = $code;
$urlObj["grant_type"] = "authorization_code";
$urlObj["state"] = $state;
$bizString = $this->ToUrlParams($urlObj);
return "https://api.weixin.qq.com/sns/oauth2/access_token?".$bizString;
}
注意:以上GET方式传参可能会导致信息泄露,谨慎使用!大家有更好的解决办法请留言,互相学习!谢谢阅读。