<?php

require __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'tmhOAuthExample.php';
$tmhOAuth = new tmhOAuthExample();
$code = $tmhOAuth->user_request(array('url' => $tmhOAuth->url('1.1/account/verify_credentials')));
$tmhOAuth->render_response();
예제 #2
0
<?php

require __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'tmhOAuthExample.php';
$tmhOAuth = new tmhOAuthExample();
function request()
{
    global $tmhOAuth;
    // verify crendentials using one set of credentials
    $code = $tmhOAuth->user_request(array('url' => $tmhOAuth->url('1.1/account/verify_credentials')));
    $d = json_decode($tmhOAuth->response['response']);
    if ($code == 200) {
        echo 'hello @' . $d->screen_name . PHP_EOL;
    } elseif (isset($d->errors[0]->message)) {
        echo 'hmm, the API returned an error: ' . $d->errors[0]->message . PHP_EOL;
    } else {
        echo 'hmm, the API returned an error: ' . $tmhOAuth->response['response'] . PHP_EOL;
    }
}
request();
// change to another set of credentials by copying the current config and merging new values on top
$tmhOAuth->reconfigure(array_merge($tmhOAuth->config, array('token' => 'ANOTHER_USER_TOKEN', 'secret' => 'ANOTHER_USER_SECRET')));
request();
예제 #3
0
<?php

# https://dev.twitter.com/docs/api/1.1/get/friends/list
function check_rate_limit($response)
{
    $headers = $response['headers'];
    if (0 === intval($headers['x-rate-limit-remaining'])) {
        $reset = $headers['x-rate-limit-reset'];
        $sleep = intval($reset) - time();
        echo 'rate limited. reset time is ' . $reset . PHP_EOL;
        echo 'sleeping for ' . $sleep . ' seconds';
        sleep($sleep);
    }
}
require __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'tmhOAuthExample.php';
$tmhOAuth = new tmhOAuthExample();
$screen_name = 'tmhoauth';
$cursor = '-1';
$friends = array();
while (true) {
    if ($cursor == '0') {
        break;
    }
    echo '.';
    $code = $tmhOAuth->request('GET', $tmhOAuth->url('1.1/friends/list'), array('cursor' => $cursor, 'skip_status' => true, 'screen_name' => $screen_name));
    check_rate_limit($tmhOAuth->response);
    if ($code == 200) {
        $data = json_decode($tmhOAuth->response['response'], true);
        $friends = array_merge($friends, $data['users']);
        $cursor = $data['next_cursor_str'];
    } else {
예제 #4
0
<?php

require __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'tmhOAuthExample.php';
$tmhOAuth = new tmhOAuthExample();
$image = __DIR__ . DIRECTORY_SEPARATOR . 'photo.jpg';
$name = basename($image);
$status = "Picture time";
if (class_exists('CurlFile', false)) {
    $media = new CurlFile($image);
} else {
    $media = "@{$image};type=image/jpeg;filename={$name}";
}
// ref: https://dev.twitter.com/rest/public/uploading-media
// first we post to the media upload endpoint
// then we use the returned information to post to the tweet endpoint
$code = $tmhOAuth->user_request(array('method' => 'POST', 'url' => 'https://upload.twitter.com/1.1/media/upload.json', 'params' => array('media' => $media), 'multipart' => true));
$tmhOAuth->render_response();
// response looks like this:
/*
{
  "media_id": 553656900508606464,
  "media_id_string": "553656900508606464",
  "size": 998865,
  "image": {
    "w": 2234,
    "h": 1873,
    "image_type": "image/jpeg"
  }
}
*/
if ($code == 200) {
예제 #5
0
<?php

require __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'tmhOAuthExample.php';
$tmhOAuth = new tmhOAuthExample();
$code = $tmhOAuth->user_request(array('method' => 'POST', 'url' => $tmhOAuth->url('1.1/friendships/create'), 'params' => array('screen_name' => 'tmhOAuth')));
$tmhOAuth->render_response();
예제 #6
0
<?php

require __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'tmhOAuthExample.php';
$tmhOAuth = new tmhOAuthExample();
$code = $tmhOAuth->apponly_request(array('method' => 'GET', 'url' => $tmhOAuth->url('1.1/search/tweets'), 'params' => array('q' => 'tmhoauth')));
$tmhOAuth->render_response();
예제 #7
0
<?php

require __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'tmhOAuthExample.php';
$tmhOAuth = new tmhOAuthExample();
$image = __DIR__ . DIRECTORY_SEPARATOR . 'photo.jpg';
$name = basename($image);
$status = "Picture time";
$code = $tmhOAuth->user_request(array('method' => 'POST', 'url' => $tmhOAuth->url('1.1/statuses/update_with_media'), 'params' => array('media[]' => "@{$image};type=image/jpeg;filename={$name}", 'status' => $status), 'multipart' => true));
$tmhOAuth->render_response();
예제 #8
0
<?php

require __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'tmhOAuthExample.php';
$tmhOAuth = new tmhOAuthExample();
$code = $tmhOAuth->user_request(array('method' => 'POST', 'url' => $tmhOAuth->url('1.1/statuses/update'), 'params' => array('status' => 'MTBLS166-1H NMR and LC–MS/MS based urine metabolomic investigation of the subacute effects of HBCD in mice')));
$tmhOAuth->render_response();
예제 #9
0
<?php

require __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'tmhOAuthExample.php';
$tmhOAuth = new tmhOAuthExample();
function my_streaming_callback($data, $length, $metrics)
{
    $file = __DIR__ . '/metrics.txt';
    echo $data . PHP_EOL;
    if (!is_null($metrics)) {
        if (!file_exists($file)) {
            $line = 'time' . "\t" . implode("\t", array_keys($metrics)) . PHP_EOL;
            file_put_contents($file, $line);
        }
        $line = time() . "\t" . implode("\t", $metrics) . PHP_EOL;
        file_put_contents($file, $line, FILE_APPEND);
    }
    return file_exists(dirname(__FILE__) . '/STOP');
}
$code = $tmhOAuth->streaming_request('GET', 'https://stream.twitter.com/1.1/statuses/sample.json', array(), 'my_streaming_callback');
$tmhOAuth->render_response();
예제 #10
0
<?php

require __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'tmhOAuthExample.php';
$tmhOAuth = new tmhOAuthExample();
$code = $tmhOAuth->user_request(array('method' => 'POST', 'url' => $tmhOAuth->url('1.1/statuses/update'), 'params' => array('status' => 'Something for the weekend')));
$tmhOAuth->render_response();
예제 #11
0
<?php

// this example follows the guidance given on dev.twitter.com for obtaining an app-only bearer token
require __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'tmhOAuthExample.php';
$tmhOAuth = new tmhOAuthExample();
$bearer = $tmhOAuth->bearer_token_credentials();
$params = array('grant_type' => 'client_credentials');
$code = $tmhOAuth->request('POST', $tmhOAuth->url('/oauth2/token', null), $params, false, false, array('Authorization' => "Basic {$bearer}"));
if ($code == 200) {
    $data = json_decode($tmhOAuth->response['response']);
    if (isset($data->token_type) && strcasecmp($data->token_type, 'bearer') === 0) {
        $new_bearer = $data->access_token;
        echo 'Your app-only bearer token is: ' . $new_bearer . PHP_EOL;
    }
} else {
    $tmhOAuth->render_response();
}
예제 #12
0
<?php

require __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'tmhOAuthExample.php';
$tmhOAuth = new tmhOAuthExample();
$code = $tmhOAuth->user_request(array('method' => 'GET', 'url' => $tmhOAuth->url('1.1/users/lookup'), 'params' => array('user_id' => array(777925), 'screen_name' => array('tmhoauth'), 'map' => 1)));
$tmhOAuth->render_response();
예제 #13
0
<?php

require __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'tmhOAuthExample.php';
$tmhOAuth = new tmhOAuthExample();
$code = $tmhOAuth->apponly_request(array('without_bearer' => true, 'method' => 'POST', 'url' => $tmhOAuth->url('oauth/access_token', ''), 'params' => array('x_auth_username' => 'USERNAME', 'x_auth_password' => 'PASSWORD', 'x_auth_mode' => 'client_auth', 'send_error_codes' => true)));
if ($code == 200) {
    $data = $tmhOAuth->extract_params($tmhOAuth->response['response']);
    if (isset($data['oauth_token'])) {
        echo 'OAuth Credentials for @' . $data['screen_name'] . ' (' . $data['user_id'] . ')' . PHP_EOL;
        echo 'Token: ' . $data['oauth_token'] . PHP_EOL;
        echo 'Secret: ' . $data['oauth_token_secret'] . PHP_EOL;
        // instead of printing these to screen you would typically save them to a database
        // if you do store the oauth_token and oauth_secret to a database it's best practice
        // store them with some kind of encryption. e.g. mcrypt_encrypt
    }
} else {
    $tmhOAuth->render_response();
}
예제 #14
0
<?php

error_reporting(E_ERROR | E_PARSE);
require __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'tmhOAuthExample.php';
$tmhOAuth = new tmhOAuthExample();
function CallAPI($method, $url, $data = false)
{
    $curl = curl_init();
    $headers = array();
    $headers[] = 'user_token: abcdefg';
    switch ($method) {
        case "POST":
            curl_setopt($curl, CURLOPT_POST, 1);
            if ($data) {
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            }
            break;
        case "PUT":
            curl_setopt($curl, CURLOPT_PUT, 1);
            break;
        default:
            if ($data) {
                $url = sprintf("%s?%s", $url, http_build_query($data));
            }
    }
    // Optional Authentication:
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($curl);
    curl_close($curl);