public static function rest($controller, $user, $re, $parameters, $need_params) { $appid = $parameters["appid"]; $openid = $user->openid; $cookie_key = self::$bind_type . "_" . $openid; $restUrl = $re["inf_url"]; $str_para = array(); $app = new \Justsy\BaseBundle\Management\App($controller); $bindinfo = $app->getappbind(array("appid" => $appid, "openid" => $openid)); if (!empty($parameters)) { //将参数数组转化为字符串 if (is_array($parameters) && !empty($need_params)) { $parameters["uid"] = $bindinfo["bind_uid"]; for ($i = 0; $i < count($need_params); $i++) { $pname = $need_params[$i]["paramname"]; $val = isset($parameters[$pname]) ? $parameters[$pname] : $need_params[$i]["paramvalue"]; $str_para[$pname] = $val; } } } if (strpos($restUrl, "?") === false) { $restUrl = $restUrl . "?" . http_build_query($str_para); } else { $restUrl = $restUrl . "&" . http_build_query($str_para); } $controller->get("logger")->err("===============restUrl:" . $restUrl); $re = Utils::do_post_request_cookie($restUrl, null, null, $cookie_key); //session过期时自动登录 /*$sessionActive = true; if(!$sessionActive) { $appinfo = $app->getappinfo(array("appid"=>$appid)); $loginUrl = $appdata["authorization_url"]; //登录 $authResult = Utils::do_get_request_cookie($loginUrl."&".http_build_query(array("uid"=>$bindinfo["bind_uid"],"upwd"=>md5(DES::decrypt($bindinfo["authkey"])))), null, null, $cookie_key); //重新提交 $re = Utils::do_post_request_cookie($restUrl."&".http_build_query($str_para),null,null,$cookie_key); }*/ return $re; }
public static function rest($controller, $user, $re, $parameters, $need_params) { $cookie_key = $authtype . "_" . $user->openid; $loginUrl = $appdata["authorization_url"]; if (empty($loginUrl)) { throw new \Exception("认证接口未配置!"); } $bindinfo = $app->getappbind(array("appid" => $parameters["appid"], "openid" => $user->openid)); if (empty($bindinfo)) { throw new \Exception("帐号未绑定K3!"); } //$container->get("logger")->err("k3 login url:".$loginUrl); //获取绑定的k3帐号和密码进行k3登录认证 $authResult = Utils::do_get_request_cookie($loginUrl, "provider=credentials&UserName="******"bind_uid"] . "&Password="******"authkey"] . "&PasswordIsEncrypted=false&RememberMe=false", null, $cookie_key); //$container->get("logger")->err("k3 login result:".$authResult); $authResult = json_decode($authResult, true); if (!isset($authResult["Result"])) { throw new \Exception("用户认证失败!"); } if (!$authResult["Result"]["ResponseStatus"]["IsSuccess"]) { throw new \Exception($authResult["Result"]["ResponseStatus"]["Message"]); } //$container->get("logger")->err("k3 api url:".$url); $http_data = Utils::do_post_request_cookie($url, $str_para, null, $cookie_key, $method); //$container->get("logger")->err("k3 api result:".$http_data); return $http_data; }
public static function rest($controller, $user, $re, $parameters, $need_params) { $appid = $parameters["appid"]; $openid = $user->openid; $cookie_key = self::$bind_type . "_" . $openid; //获取绑定的sid $app = new \Justsy\BaseBundle\Management\App($controller); $sessioninfo = $app->getappsession(array("appid" => $appid, "openid" => $openid)); if (empty($sessioninfo)) { return array("status" => "fail", "msg" => "session已过期"); } $data = $sessioninfo["access_token"]; $appinfo = $app->getappinfo(array("appid" => $appid)); $restUrl = $re["inf_url"]; $str_para = array(); if (!empty($parameters)) { //将参数数组转化为字符串 if (is_array($parameters) && !empty($need_params)) { for ($i = 0; $i < count($need_params); $i++) { $pname = $need_params[$i]["paramname"]; $val = isset($parameters[$pname]) ? $parameters[$pname] : $need_params[$i]["paramvalue"]; $str_para[$pname] = $val; } } } $re = Utils::do_post_request_cookie($restUrl . "&" . http_build_query($str_para), null, null, $cookie_key); //对data进行2次转换 $tmpObj = json_decode($re, true); if (isset($tmpObj["data"])) { $txt = $tmpObj["data"]; $fChar = substr($txt, 0, 1); if ($fChar == "{" || $fChar == "[") { $tmpObj["data"] = json_decode($txt, true); $re = json_encode($tmpObj); } } return $re; }
public function getByDsid($user, $re, $parameters, $container) { if (isset($re["inf_url"]) && !empty($re["inf_url"])) { $url = $re["inf_url"]; $is_auth = $re["is_auth"]; //接口是否需要认证,1:需要认证 0:不认证 $str_para = ""; if (empty($parameters) || $parameters == "{}") { $parameters = $re["inf_parameter"]; } $need_para = $re["inf_parameter"]; if (!empty($need_para) && is_string($need_para)) { $need_para = json_decode($need_para, true); } $app = new \Justsy\BaseBundle\Management\App($container); //parameters为json数据格式 if (empty($parameters)) { throw new \Exception("参数appid不能为空!"); } $parameters = is_array($parameters) ? $parameters : json_decode($parameters, true); $appdata = $app->getappinfo(array("appid" => $parameters["appid"])); $authtype = isset($appdata["authtype"]) ? $appdata["authtype"] : ""; if (!empty($authtype) && $is_auth == "1") { $classname = dirname(dirname(dirname(dirname(__FILE__)))) . "/OpenAPIBundle/Controller/Sso" . ucfirst($authtype) . "Controller.php"; //$container->get("logger")->err("===========file_exists ".$classname); if (file_exists($classname)) { $classname = "\\Justsy\\OpenAPIBundle\\Controller\\Sso" . ucfirst($authtype) . "Controller"; //$container->get("logger")->err("===========load ".$classname); $re = call_user_func(array($classname, 'rest'), $container, $user, $re, $parameters, $need_para); //$container->get("logger")->err("===========load ".$classname." result:".json_encode($re)); return $re; } } if (!empty($parameters)) { //将参数数组转化为字符串 if (is_array($parameters) && !empty($need_para)) { for ($i = 0; $i < count($need_para); $i++) { $pname = $need_para[$i]["paramname"]; $val = isset($parameters[$pname]) ? $parameters[$pname] : $need_para[$i]["paramvalue"]; $str_para .= $pname . "=" . $val . "&"; } $str_para = rtrim($str_para, "&"); } } $method = $re["req_action"]; $method = $method != "GET" ? "POST" : "GET"; $container->get("logger")->err("authtype:" . $authtype); $optional_headers = null; if ($authtype == "header") { $userpara = $appdata["userdefined_para"]; if (!empty($userpara)) { $optional_headers = json_decode($userpara, true); } } else { if ($authtype == "basic") { $userpara = $appdata["userdefined_para"]; if (!empty($userpara)) { $userpara = json_decode($userpara, true); $user = $userpara["user"]; $pass = $userpara["pass"]; $optional_headers = array("Authorization" => "Basic " . base64_encode("{$user}:{$pass}")); } } } if ($method == "GET") { if (strpos($url, "?") === false) { $url = $url . "?" . $str_para; } else { $url = $url . "&" . $str_para; } $container->get("logger")->err($url); return Rest::getByURL($url); } else { $method = "POST"; if (strpos($url, "?") === false) { $url = $url . "?" . $str_para; } else { $url = $url . "&" . $str_para; } $str_para = ""; } $container->get("logger")->err($url); //,CURLOPT_COOKIE $http_data = Utils::do_post_request_cookie($url, null, $optional_headers, $_COOKIE, $method); /* $params = array('http' => array('timeout'=>3,'method' => $method,'content' => $str_para)); $ctx = stream_context_create($params); $fp = @fopen($url, 'r', false, $ctx); if (!$fp) { throw new \Exception("接口地址出错,请检查您的接口地址(".$url.")"); } //获取数据 $http_data = @stream_get_contents($fp); if ($http_data === false) { throw new \Exception("访问的接口地址(".$url.")服务器出错!"); }*/ return $http_data; } else { throw new \Exception("接口地址为空,请检查!"); } }