function getAccessToken($appid, $secret)
{
    global $db;
    //检验数据库中是否存在token,并验证是否有效
    $sql_token = "select token,date from wx_access_token where appId='{$appid}' order by date desc limit 1";
    $res_token = $db->getrow($sql_token);
    if (!empty($res_token)) {
        $start = strtotime($res_token['date']);
        $end = $start + 7000;
        $now = time();
        if ($now <= $end) {
            $token['token'] = $res_token['token'];
        } else {
            //删除掉过期的token记录
            $sql_delete = "delete from wx_access_token where appId='{$appid}'";
            $res_delete = $db->execsql($sql_delete);
            //用微信接口获取access_token,并插入到数据库中
            $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
            $access_token = gettoken($url);
            $token['token'] = $access_token;
            $token['date'] = date("Y-m-d H:i:s", time());
            $token['appId'] = $appid;
            $insert = $db->insert("wx_access_token", $token);
        }
    } else {
        $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$appid}&secret={$secret}";
        $access_token = gettoken($url);
        $token['token'] = $access_token;
        $token['date'] = date("Y-m-d H:i:s", time());
        $token['appId'] = $appid;
        $insert = $db->insert("wx_access_token", $token);
    }
    return $token['token'];
}
function querycoll($host, $db_rid, $coll_rid, $query, $apptype, $useragent, $cachecontrol, $da_date, $api_version, $master, $token, $master_key, $da_date)
{
    $header = getauthheaders($apptype, $useragent, $cachecontrol, $da_date, $api_version, getauthtoken($master, $token, gettoken($master_key, 'POST', 'docs', $coll_rid, $da_date)));
    $header[] = 'Content-Length:' . strlen($query);
    $header[] = 'Content-Type:application/sql';
    $header[] = 'x-ms-documentdb-isquery:True';
    //print "<pre>";print_r($header);print "</pre>";
    $options = array(CURLOPT_HTTPHEADER => $header, CURLOPT_POST => true, CURLOPT_POSTFIELDS => $query);
    return request($host, "/dbs/" . $db_rid . "/colls/" . $coll_rid . "/docs", $options);
}
Example #3
0
<?php

/*
 *获取微信token
*/
$appid = "wx5d12c15c30c041ff";
$secret = "feaa6e76adbe6a477dd89e445d99a520";
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $secret . "";
//$token=(array)json_decode(file_get_contents($url));
$output = gettoken($url);
$token = (array) json_decode($output);
echo $token['access_token'];
//print_r($token);
//echo $token['access_token'];
function gettoken($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22");
    curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
    //加入gzip解析
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}
Example #4
0
<?php

require_once "auth.php";
if (!isset($client_id)) {
    die("Server error: Not configured properly. Sorry for the inconvience ;(");
}
// Check if $_GET["code"] exists
if (!isset($_GET["code"])) {
    // Redirect back to Github to try again
    header("Location: https://github.com/login/oauth/authorize?client_id=e8613d2bd747516a18ac&scope=write:org,user:email");
    die("No code get parameter sent from Github");
}
$token = gettoken($client_id, $client_secret, $_GET["code"]);
header("Location: /index.php?access_token=" . $token);
/**
 * @param $client_id
 * @param $client_secret
 * @param $code
 * @return string
 *
 * Returns access_token
 */
function gettoken($client_id, $client_secret, $code)
{
    $data = "client_id=" . $client_id . "&client_secret=" . $client_secret . "&code=" . $code;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "https://github.com/login/oauth/access_token");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, count($data));
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $replyRaw = curl_exec($ch);