public static function curlPost($url, $data) { $ch = curl_init(); $headers[] = '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($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 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); $result = curl_exec($ch); curl_close($ch); $result = json_decode($result, true); if (isset($result['errcode'])) { import('source.class.GetErrorMsg'); $errmsg = GetErrorMsg::wx_error_msg($result['errcode']); return array('errcode' => $result['errcode'], 'errmsg' => $errmsg); } else { $result['errcode'] = 0; return $result; } }
public static function curlPost($url, $data) { $ch = curl_init(); $headers[] = "Accept-Charset: utf-8"; //"Content-Type: multipart/form-data; boundary=" . uniqid('------------------'); // $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($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 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); $result = curl_exec($ch); //关闭curl curl_close($ch); $result = json_decode($result, true); if (isset($result['errcode'])) { import('ORG.Net.GetErrorMsg'); $errmsg = GetErrorMsg::wx_error_msg($result['errcode']); return array('errcode' => $result['errcode'], 'errmsg' => $errmsg); } else { $result['errcode'] = 0; return $result; } $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($ch, 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); $result = curl_exec($ch); //关闭curl curl_close($ch); return $result; }
public function weixin_back() { $referer = !empty($_SESSION['weixin']['referer']) ? $_SESSION['weixin']['referer'] : U('Home/index'); // if (isset($_GET['code']) && isset($_GET['state']) && ($_GET['state'] == $_SESSION['weixin']['state'])){ if (isset($_GET['code'])) { unset($_SESSION['weixin']['state']); import('ORG.Net.Http'); $http = new Http(); $return = $http->curlGet('https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $this->config['wechat_appid'] . '&secret=' . $this->config['wechat_appsecret'] . '&code=' . $_GET['code'] . '&grant_type=authorization_code'); $jsonrt = json_decode($return, true); if ($jsonrt['errcode']) { $error_msg_class = new GetErrorMsg(); $this->error_tips('授权发生错误:' . $error_msg_class->wx_error_msg($jsonrt['errcode']), U('Login/index')); } $return = $http->curlGet('https://api.weixin.qq.com/sns/userinfo?access_token=' . $jsonrt['access_token'] . '&openid=' . $jsonrt['openid'] . '&lang=zh_CN'); $jsonrt = json_decode($return, true); if ($jsonrt['errcode']) { $error_msg_class = new GetErrorMsg(); $this->error_tips('授权发生错误:' . $error_msg_class->wx_error_msg($jsonrt['errcode']), U('Login/index')); } /*优先使用 unionid 登录*/ if (!empty($jsonrt['unionid'])) { $this->autologin('union_id', $jsonrt['unionid'], $referer); } /*再次使用 openid 登录*/ $this->autologin('openid', $jsonrt['openid'], $referer); /*注册用户*/ $data_user = array('openid' => $jsonrt['openid'], 'union_id' => $jsonrt['unionid'] ? $jsonrt['unionid'] : '', 'nickname' => $jsonrt['nickname'], 'sex' => $jsonrt['sex'], 'province' => $jsonrt['province'], 'city' => $jsonrt['city'], 'avatar' => $jsonrt['headimgurl']); $_SESSION['weixin']['user'] = $data_user; $this->assign('referer', $referer); $this->display(); } else { $this->error_tips('访问异常!请重新登录。', U('Login/index', array('referer' => urlencode($referer)))); } }
public function authorize_openid() { if (empty($_GET["code"])) { $_SESSION["weixin"]["state"] = md5(uniqid()); $customeUrl = $this->config["site_url"] . $_SERVER["REQUEST_URI"]; //用户同意授权,获取code $oauthUrl = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" . $this->config["wechat_appid"] . "&redirect_uri=" . urlencode($customeUrl) . "&response_type=code&scope=snsapi_base&state=" . $_SESSION["weixin"]["state"] . "#wechat_redirect"; redirect($oauthUrl); exit; } else { if (isset($_GET['code']) && true == isset($_GET['state']) && $_GET['state'] == $_SESSION['weixin']['state']) { unset($_SESSION['weixin']); import('ORG.Net.Http'); $http = new Http(); //获取access_token $return = $http->curlGet("https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $this->config["wechat_appid"] . "&secret=" . $this->config["wechat_appsecret"] . "&code=" . $_GET["code"] . "&grant_type=authorization_code"); $jsonrt = json_decode($return, true); //授权失败 if ($jsonrt["errcode"]) { $error_msg_class = new GetErrorMsg(); $this->error_tips("授权发生错误:" . $error_msg_class->wx_error_msg($jsonrt["errcode"]), U("Home/index")); } //授权成功 if ($jsonrt["openid"]) { $_SESSION["openid"] = $jsonrt["openid"]; $result = D("User")->autologin("openid", $jsonrt["openid"]); if (empty($result['error_code'])) { $now_user = $result['user']; session('user', $now_user); $this->user_session = session('user'); } } else { redirect(u('Home/index')); } } else { redirect(u('Home/index')); } } }
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($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1); 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); $errorno = curl_errno($ch); if ($errorno) { return array('rt' => false, 'errorno' => $errorno); } else { $js = json_decode($tmpInfo, 1); // dump($js); if ($js['errcode'] == '0') { return array('rt' => true, 'errorno' => 0); } else { $errmsg = GetErrorMsg::wx_error_msg($js['errcode']); $this->error('发生错误:错误代码' . $js['errcode'] . ',微信返回错误信息:' . $errmsg); } } }