function load_user_entity($can_refresh = true)
{
    global $vbulletin;
    if (!$vbulletin->capture_session) {
        if (!$vbulletin->session->vars['capture_access_token'] && !$vbulletin->session->vars['capture_refresh_token']) {
            return NULL;
        } else {
            $vbulletin->capture_session = array("capture_access_token" => $vbulletin->session->vars['capture_access_token'], "capture_refresh_token" => $vbulletin->session->vars['capture_refresh_token'], "capture_expires_in" => $vbulletin->session->vars['capture_expires_in']);
        }
    }
    $user_entity = NULL;
    // TODO might want to do a sanity check on capture_session,
    //      in case it is set but corrupted.
    // --------------------
    // There are two ways we check if the access token has expired:
    //  - First, check if we know it has expired based on the expiration time.
    //  - Second, try to use it, and check for error 414,
    //    a unique error code that means the token has expired.
    $need_to_refresh = false;
    // Check if we need to refresh the access token
    if (time() >= $vbulletin->capture_session['capture_expires_in']) {
        $need_to_refresh = true;
    } else {
        $user_entity = get_entity($vbulletin->capture_session['capture_access_token']);
        if (isset($user_entity['code']) && $user_entity['code'] == '414') {
            $need_to_refresh = true;
        }
    }
    // If necessary, refresh the access token and try to fetch the entity again.
    if ($need_to_refresh) {
        if ($can_refresh) {
            refresh_access_token($vbulletin->capture_session['capture_refresh_token']);
            return load_user_entity(false);
        }
    }
    return $user_entity;
}
Example #2
0
<?php

/**
 * Created by PhpStorm.
 * User: Min
 * Date: 2015/10/31
 * Time: 15:40
 */
//file_get_contents("http://localhost:8080/index.php?route=wechat/msghandler&from=".urlencode("omiagw6HuwXD95DmvmpY27rs1y1c")."&msg=".urlencode("下单成功:50"));
$operator_idlist = ['omiagw0Fg6sXNqbbq91jV9X2pS6w', 'omiagw8PiuGfQQfVabDMtAlQI_vo', 'omiagwyIv-qIg2UHGRoHAtHuN-Hk', 'omiagw6HuwXD95DmvmpY27rs1y1c', 'omiagw0V89-YrnIfT_6ZqwdNjT0E', 'omiagwzBMqBnpGLL5o6qAhUNOZlg', 'omiagwxC73z9re0wK_JCy2-YtHGQ'];
$content = file_get_contents("welcome.txt");
$lines = explode("\n", $content);
$num = count($lines);
$idx = rand(0, $num - 1);
$token = refresh_access_token();
$msg = $lines[$idx];
foreach ($operator_idlist as $openid) {
    $res = sendmsg($openid, $msg, $token);
    var_dump($res);
    $res = sendmsg($openid, "悠选祝您每日好心情", $token);
    var_dump($res);
}
function refresh_access_token()
{
    $APPID = "wx47180ba69fa68387";
    $APPSECRET = "978a3e249a1980827fed107003227cdd";
    $TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $APPID . "&secret=" . $APPSECRET;
    $json = file_get_contents($TOKEN_URL);
    //$json=$this->http_get($TOKEN_URL);
    $result = json_decode($json);
    return $result->access_token;
Example #3
0
<?php

require 'globals.php';
require 'oauth_helper.php';
// Fill in the next 3 variables.
$old_access_token = 'A=KdfjadlskfjSDFGG.ertklsioerkjhSDFGkjlhasdfik345k34897SDFgklhe4kljhdSGKLjhsdfg.mcxkhdfSGKHsdfgkjeroI.REsdFGSFDg.sdfgiwresdfgsfhg.gh.tyu.ghfj.dfghfsdg.fgsdg.sdfgiretkjsfdgkjlhertiuysdfgkjhsdfgkljertkjhsdfguyert8743508972345lkjhsdfi8g89sdfg89sdfg908sdfg897sdfg8sdfg734jk25kljhwdkjlhsdfgkjlhsfdgkjlhsdfgjkhsdfgkjhsfdgiuywert87425ksdkjhlsdfgkjlhsdfgjklcxbm.cxvb.asfdkljadsflk.jasldkj3452387wert98sdfg8sdfg897sdfg890sdfgpoiret.lsdfgkljsdfgiwret_sfgkjhmnsdfgjkcvbmsdfglkjhewrtiusdfgjkhsdfgiuret87245lkjhdsfg.mnvbkisdfwertrwt.42534wertwgsdfg.cxvbsfdgsdfg.rwetwert.452435wertwretwer.wertwergtsdfgsdfg.sdfgsdfgrewtwret4252345wtdfgsdfg.sdfgsdfgsdfgewrtwert23452345wertwgsdfgfdrtyfhdgsdfgsdfgrewtwertsdfgdfgrt2rwersdfgdfgretrwefgrwtwertwertweryrwywertwertfsgfsdgsdferw3452twresdfgwretwert45wrtertrtg-';
$old_token_secret = 'o2345w980945353478594867g3454l45lk324wrd';
$oauth_session_handle = 'kj435kj.lkjlkj.ksdfgdfi44.dsfgkoert908435lkjglgs';
// Refresh the access token using HTTP GET and HMAC-SHA1 signature
$retarr = refresh_access_token(OAUTH_CONSUMER_KEY, OAUTH_CONSUMER_SECRET, $old_access_token, $old_token_secret, $oauth_session_handle, false, true, true);
if (!empty($retarr)) {
    list($info, $headers, $body, $body_parsed) = $retarr;
    if ($info['http_code'] == 200 && !empty($body)) {
        print "Use oauth_token as the token for all of your API calls:\n" . rfc3986_decode($body_parsed['oauth_token']) . "\n";
    }
}
exit(0);
/**
 * Refresh an access token using an expired request token
 * @param string $consumer_key obtained when you registered your app
 * @param string $consumer_secret obtained when you registered your app
 * @param string $old_access_token obtained previously
 * @param string $old_token_secret obtained previously
 * @param string $oauth_session_handle obtained previously
 * @param bool $usePost use HTTP POST instead of GET (default false)
 * @param bool $useHmacSha1Sig use HMAC-SHA1 signature (default false)
 * @return response string with token or empty array on error
 */
function refresh_access_token($consumer_key, $consumer_secret, $old_access_token, $old_token_secret, $oauth_session_handle, $usePost = false, $useHmacSha1Sig = true, $passOAuthInHeader = true)
{
    $retarr = array();
Example #4
0
function delete_subscription_by_id($access_token, $api_root, $subscriptionId)
{
    global $endpoint;
    // Delete a previously created subscription.
    $auth_header = build_oauth2_auth_header($access_token);
    $header = array();
    $header = array_merge($header, $auth_header);
    // DELETE /v1/orgs/<org id>/subscriptions/<subscriptionId>
    $url = $endpoint . $api_root->_links->subscriptions->href . '/' . $subscriptionId;
    $response = Requests::delete($url, $header);
    return json_decode($response->body);
}
// Recover an access token
$grant = get_access_token($api_key, $api_key_secret);
// Refresh an access token before grant['expires_in'] has expired.
$grant = refresh_access_token($api_key, $api_key_secret, $grant->refresh_token);
// Get the root of the api
$api_root = get_api_root($grant->access_token);
// Create a new subscription for one of your users
$subscription_ref = Uuid::uuid4();
$subscription = create_subscription($grant->access_token, $api_root, array('ref' => $subscription_ref->toString(), 'name' => 'Dr. Jane Who', 'email' => '*****@*****.**'));
// Get a page of sessions for
$sessions = get_sessions($grant->access_token, $api_root);
// Get the first page of subscriptions
$subscriptions = get_subscriptions($grant->access_token, $api_root);
//var_dump(count($subscriptions->content));
// Delete the entire set of subscriptions traversing forward
for ($i = 0, $c = count($subscriptions->content); $i < $c; $i++) {
    delete_subscription($grant->access_token, $subscriptions->content[$i]);
}
/*