コード例 #1
0
ファイル: redirect.php プロジェクト: rukshn/yummy-twitter
<?php

session_start();
require_once 'config.php';
require_once 'functions.php';
require_once 'oAuth.php';
$sig_new = array('oauth_callback' => CALLBACK_URL);
$signature = signature_generator('POST', $sig_new, 'https://api.twitter.com/oauth/request_token');
$sig_new['oauth_signature'] = $signature;
$token = get_request_token($sig_new);
if ($token['status'] == 200) {
    $_SESSION['request_token'] = $token;
    $_SESSION['oauth_token'] = $token['oauth_token'];
    $redirect_url = 'https://api.twitter.com/oauth/authorize?oauth_token=' . $token['oauth_token'];
    redirect($redirect_url);
} else {
    print_r($token);
    session_destroy();
}
コード例 #2
0
ファイル: oAuth.php プロジェクト: rukshn/yummy-twitter
function make_request($method, $sig_parm, $url)
{
    $base_url = 'https://api.twitter.com/1.1/';
    $request_url = 'https://api.twitter.com/1.1/' . $url . '.json';
    if (isset($_SESSION['access_token'])) {
        $ot = $_SESSION['access_token']['oauth_token'];
        $post_data = $sig_parm;
        $sig_parm['oauth_token'] = $ot;
        $sig_text = array('oauth_nonce' => RANDOM_STRING, 'oauth_signature_method' => "HMAC-SHA1", 'oauth_timestamp' => TIME_STAMP, 'oauth_consumer_key' => CONSUMER_KEY, 'oauth_version' => '1.0', 'oauth_token' => $ot);
        $signature = signature_generator($method, $sig_parm, $request_url);
        $sig_text['oauth_signature'] = $signature;
        ksort($sig_text);
        foreach ($sig_text as $key => $value) {
            $new_pair[] = $key . '="' . urlencode($value) . '"';
        }
        $get_header = implode(', ', $new_pair);
        $get_header = 'Authorization: OAuth ' . $get_header;
        $ch = curl_init();
        if ($method === 'get' or $method === 'GET') {
            curl_setopt($ch, CURLOPT_URL, $request_url . '?' . http_build_query($post_data));
        } elseif ($method === 'post' or $method === 'POST') {
            curl_setopt($ch, CURLOPT_URL, $request_url);
            curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
            //Post Fields
            curl_setopt($ch, CURLOPT_POST, 1);
        }
        curl_setopt($ch, CURLOPT_TIMEOUT, '3');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array($get_header));
        $result = curl_exec($ch);
        if (curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) {
            $response['status'] = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            $response['message'] = 'oAuth Error';
            $json = json_encode($response);
            return $json;
        } else {
            $response['status'] = 200;
            $response['message'] = $result;
            return $response;
        }
        curl_close($ch);
    } else {
        $response['status'] = 400;
        $response['message'] = 'not authorized, please login using twitter and make the request';
        $json = json_encode($response);
        return $response;
    }
}