搭建openresty需要注意到的地方

openresty的完整包放在百度云盘linux目录下

一键安装openresty

./install.sh

安装好后,修改nginx.conf配置文件

cd /usr/local/openresty/nginx/conf/nginx.conf

server_name   auth_api.com

cd /usr/local/openresty/lualib/ttq

修改lua脚本配置文件

vim ini.lua,注意:redis需要设置密码,不然不能访问

还需要把purview.sql两个表导入指定数据库

导入参考数据:

INSERT INTO `ttq_appid_list` VALUES ('1', 'TTQ_iL9mT5lbtl17w5M5', 'xmcsX3fmwznd27Zq3W9h', '1466488625');

INSERT INTO `ttq_appid_white_list` VALUES ('1', 'TTQ_iL9mT5lbtl17w5M5', '127.0.0.1');

在yaf中应用的时候需要修改配置文件:

application_dev.ini

authenticator.url="http://auth.api.herry_a.com:8080"

<?php
/*
格式化输出
*/
function p($arr) {
    echo "<pre>";
    print_r($arr);die;
}
/**
 * 获取用户IP地址
 *
 * @return mixed
 */
function getRemoteAddr() {
    if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARDED_FOR'] && (!isset($_SERVER['REMOTE_ADDR']) || preg_match('/^127\..*/i', trim($_SERVER['REMOTE_ADDR'])) || preg_match('/^172\.16.*/i', trim($_SERVER['REMOTE_ADDR'])) || preg_match('/^192\.168\.*/i', trim($_SERVER['REMOTE_ADDR'])) || preg_match('/^10\..*/i', trim($_SERVER['REMOTE_ADDR']))))
    {
        if (strpos($_SERVER['HTTP_X_FORWARDED_FOR'], ','))
        {
            $ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);

            return $ips[0];
        }
        else
            return $_SERVER['HTTP_X_FORWARDED_FOR'];
    }

    return $_SERVER['REMOTE_ADDR'];
}

function object2array(&$object) {
    return json_decode(json_encode($object), true);
}

/**
 * @param        $url
 * @param string $method
 * @param null   $postFields
 * @param null   $header
 *
 * @return mixed
 * @throws Exception
 */
function curl($url, $method = 'GET', $postFields = null, $header = null, $saveCookie=FALSE, $useCookie=FALSE, $cookieFile=null) {
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
    curl_setopt($ch, CURLOPT_FAILONERROR, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);    
    if ( !is_null($cookieFile)) {
        if ( $saveCookie ) {
            curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
        } elseif ( $useCookie ) {
            curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile);
        }
        if (strlen($url) > 5 && strtolower(substr($url, 0, 5)) == "https")         {
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        }
    }
    switch ($method)
    {
        case 'POST':
            curl_setopt($ch, CURLOPT_POST, true);
            if (!empty($postFields))
            {
                if (is_array($postFields) || is_object($postFields))
                {
                    if (is_object($postFields))
                        $postFields = object2array($postFields);
                    $postBodyString = "";
                    $postMultipart = false;
                    foreach ($postFields as $k => $v)
                    {
                        if ("@" != substr($v, 0, 1))
                        { //判断是不是文件上传
                            $postBodyString .= "$k=" . urlencode($v) . "&";
                        }
                        else
                        { //文件上传用multipart/form-data,否则用www-form-urlencoded
                            $postMultipart = true;
                        }
                    }
                    unset($k, $v);
                    if ($postMultipart)
                    {
                        curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
                    }
                    else
                    {
                        curl_setopt($ch, CURLOPT_POSTFIELDS, substr($postBodyString, 0, -1));
                    }
                }
                else
                {
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
                }

            }
            break;
        default:
            if (!empty($postFields) && is_array($postFields))
                $url .= (strpos($url, '?') === false ? '?' : '&') . http_build_query($postFields);
            break;
    }
    curl_setopt($ch, CURLOPT_URL, $url);

    if (!empty($header) && is_array($header))
    {
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
    }
    $response = curl_exec($ch);
    if (curl_errno($ch))
    {
        throw new Exception(curl_error($ch), 0);
    }
    curl_close($ch);

    return $response;
}


$url = "http://auth.api.herry_a.com:8080";
$clientIP = getRemoteAddr();
$postFields = array(
    'appid' => $_GET['appid'],
    'appkey'=> $_GET['appkey'],
    'ip' => $clientIP      //测试模拟ip 真实换成$clientIP
);
$headers = array(
    'Content-Type' => 'application/x-www-form-urlencoded',
    'charset' => 'utf-8'
);
$auth = curl($url,'POST', $postFields, $headers);
$authArr=json_decode($auth,true);
p($authArr);

?>
View Code

 

posted @ 2016-10-25 16:16  星希望  阅读(444)  评论(0编辑  收藏  举报