Skip to content
This repository has been archived by the owner on Nov 22, 2017. It is now read-only.

Latest commit

 

History

History
431 lines (310 loc) · 11.2 KB

README_DETAILS.md

File metadata and controls

431 lines (310 loc) · 11.2 KB

Class Description - TwistException

Simply extended from RuntimeException.
Treats errors caused on Twitter.

Class Description - TwistMedia

Some TwistOAuth methods return an instance of TwistMedia when the following types of header is detected.

  • Content-Type: image/***
  • Content-Type: video/***

Properties

TwistMedia::$type

(string) $media->type

Readonly.
Content-Type. This means the following value.

'image/***'
'video/***'

TwistMedia::$data

(string) $media->data

Readonly.
This means binary media data.

Methods

TwistMedia::getDataUri()

(string) $media->getDataUri()

Return Value

Data URI. This means the following value.

'data:image/***;base64,......'
'data:video/***;base64,......'

Class Description - TwistOAuth

Properties

TwistOAuth::$ck
TwistOAuth::$cs
TwistOAuth::$ot
TwistOAuth::$os

All properties are Readonly.

(string) $to->ck // consumer_key
(string) $to->cs // consumer_secret
(string) $to->ot // oauth_token (request_token or access_token)
(string) $to->os // oauth_token_secret (request_token_secret or access_token_secret)

Basic Methods

TwistOAuth::__construct()

Constructor.

new TwistOAuth($ck, $cs, $ot = '', $os = '')

Arguments

  • (string) $ck
    consumer_key.
  • (string) $cs
    consumer_secret.
  • (string) $ot
    oauth_token. (request_token or access_token)
  • (string) $os
    oauth_token_secret. (request_token_secret or access_token_secret)

TwistOAuth::getAuthenticateUrl()
TwistOAuth::getAuthorizeUrl()

Easily generate URL for users to login.

(string) $to->getAuthenticateUrl($force_login = false)
(string) $to->getAuthorizeUrl($force_login = false)

Arguments

  • (bool) $force_login
    Whether we force logined users to relogin.

Return Value

A URL for authentication or authorization.

TwistOAuth::renewWithRequestToken()
TwistOAuth::renewWithAccessToken()
TwistOAuth::renewWithAccessTokenX()

Fetch tokens and regenerate instance with them.

(TwistOAuth) $to->renewWithRequestToken($oauth_callback = '', $proxy = '')
(TwistOAuth) $to->renewWithAccessToken($oauth_verifier, $proxy = '')
(TwistOAuth) $to->renewWithAccessTokenX($username, $password, $proxy = '')

Arguments

  • (string) $oauth_callback
  • (string) $oauth_verifier
  • (string) $username
    screen_name or email.
  • (string) $password
  • (string) $proxy
    Full proxy URL.
    e.g. https://111.222.333.444:8080

Return Value

A new TwistOAuth instance.

Exception

Throws TwistException.

TwistOAuth::get()
TwistOAuth::post()
TwistOAuth::postMultipart()

Execute a request for Twitter.

(mixed) $to->get($url, $params = array(), $proxy = '')
(mixed) $to->post($url, $params = array(), $proxy = '')
(mixed) $to->postMultipart($url, $params = array(), $proxy = '')

Arguments

  • (string) $url
    Full or partial endpoint URL.
    e.g. statuses/update https://api.twitter.com/1.1/statuses/update.json
  • (mixed) $params
    1-demensional array or query string.
    File path annotation is @ on key.
    File data annotation is # on key.
    NULL is ignored.
  • (string) $proxy
    Full proxy URL.
    e.g. https://111.222.333.444:8080

Example value of $params:

$params = 'status=test&in_reply_to_status_id=123456';
$params = array(
    'status' => 'test',
    'in_reply_to_status_id' => '123456',
);
$params = array(
    'status' => 'test',
    '@media[]' => 'test.jpg',
);
$params = array(
    'status' => 'test',
    '#media[]' => file_get_contents('test.jpg'),
);

Return Value

Return value will mainly be stdClass, array or TwistMedia.

Exception

Throws TwistException.

TwistOAuth::getOut()
TwistOAuth::postOut()
TwistOAuth::postMultipartOut()

Execute a request for third party sites using OAuth Echo.

(mixed) $to->getOut($url, $params = array(), $proxy = '')
(mixed) $to->postOut($url, $params = array(), $proxy = '')
(mixed) $to->postMultipartOut($url, $params = array(), $proxy = '')

Arguments

  • (string) $url
    Full URL.
    e.g. http://api.twitpic.com/2/upload.json
  • (mixed) $params
    1-demensional array or query string.
    File path annotation is @ on key.
    File data annotation is # on key.
    NULL is ignored.
  • (string) $proxy
    Full proxy URL.
    e.g. https://111.222.333.444:8080

Return Value

Return value will mainly be stdClass, array or TwistMedia.

Exception

Throws TwistException.

TwistOAuth::streaming()

Execute a streaming request for Twitter.

(void) $to->streaming($url, callable $callback, $params = array(), $proxy = '')

Arguments

  • (string) $url
    Full or partial endpoint URL.
    e.g. statuses/filter https://stream.twitter.com/1.1/statuses/filter.json
  • (callable) $callback
    A callback function.
    1 argument for each statuses.
    Return true for disconnecting.
  • (mixed) $params
    1-demensional array or query string.
    File path annotation is @ on key.
    File data annotation is # on key.
    NULL is ignored.
  • (string) $proxy
    Full proxy URL.
    e.g. https://111.222.333.444:8080

Example value of $callback:

// A callback closure, which displays tweets unlimitedly.
$callback = function ($status) {
    // Treat only tweets
    if (isset($status->text)) {
        printf(
            "@%s: %s\n",
            $status->user->screen_name,
            htmlspecialchars_decode($status->text, ENT_NOQUOTES)
        );
        flush(); // Required if running not on Command Line but on Apache
    }
};
// A callback closure, which displays 10 tweets and then disconnect.
$callback = function ($status) {
    static $i = 0;
    if ($i > 10) {
        // Return true for disconnecting.
        return true;
    }
    // Treat only tweets
    if (isset($status->text)) {
        printf(
            "@%s: %s\n",
            $status->user->screen_name,
            htmlspecialchars_decode($status->text, ENT_NOQUOTES)
        );
        ++$i;
        flush(); // Required if running not on Command Line but on Apache
    }
};

Exception

Throws TwistException.

Abusing Methods

static TwistOAuth::login()

Direct OAuth. (Scraping Login)

(TwistOAuth) TwistOAuth::login($ck, $cs, $username, $password, $proxy = '')
(TwistOAuth) $to->login($ck, $cs, $username, $password, $proxy = '')

Arguments

  • (string) $ck
    consumer_key.
  • (string) $cs
    consumer_secret.
  • (string) $username
    screen_name or email.
  • (string) $password
  • (string) $proxy
    Full proxy URL.
    e.g. https://111.222.333.444:8080

Return Value

A new instance of TwistOAuth.

Exception

Throws TwistException.

static TwistOAuth::multiLogin()

Multiple Direct OAuth. (Scraping Logins)

(array) TwistOAuth::multiLogin(array $credentials, $throw_in_process = false)
(array) $to->multiLogin(array $credentials, $throw_in_process = false)

Arguments

  • (array) $credentials
    See example.
  • (bool) $throw_in_process
    See information about static TwistOAuth::curlMultiExec().

Example value of $credentials:

$credentials = array(
    'YOUR SCREEN_NAME 0' => array(
        'YOUR CONSUMER KEY 0',
        'YOUR CONSUMER SECRET 0',
        'YOUR SCREEN_NAME 0',
        'YOUR PASSWORD 0',
    ),
    'YOUR SCREEN_NAME 1' => array(
        'YOUR CONSUMER KEY 1',
        'YOUR CONSUMER SECRET 1',
        'YOUR SCREEN_NAME 1',
        'YOUR PASSWORD 1',
    ),
    'YOUR SCREEN_NAME 2' => array(
        'YOUR CONSUMER KEY 2',
        'YOUR CONSUMER SECRET 2',
        'YOUR SCREEN_NAME 2',
        'YOUR PASSWORD 2',
    ),
    ...
);

Return Value

An array consisting of the following structure.

$return_value = array(
    'YOUR SCREEN_NAME 0' => new TwistOAuth(...),
    'YOUR SCREEN_NAME 1' => new TwistOAuth(...),
    'YOUR SCREEN_NAME 2' => new TwistOAuth(...),
    ...
);

Exception

Throws TwistException.

TwistOAuth::curlPostRequestToken()
TwistOAuth::curlPostAccessToken()
TwistOAuth::curlGet()
TwistOAuth::curlGetOut()
TwistOAuth::curlPost()
TwistOAuth::curlPostOut()
TwistOAuth::curlPostMultipart()
TwistOAuth::curlPostMultipartOut()
TwistOAuth::curlStreaming()

(resource) $to->curlPostRequestToken($oauth_callback = '', $proxy = '')
(resource) $to->curlPostAccessToken($oauth_verifier, $proxy = '')
(resource) $to->curlGet($url, $params = array(), $proxy = '')
(resource) $to->curlGetOut($url, $params = array(), $proxy = '')
(resource) $to->curlPost($url, $params = array(), $proxy = '')
(resource) $to->curlPostOut($url, $params = array(), $proxy = '')
(resource) $to->curlPostMultipart($url, $params = array(), $proxy = '')
(resource) $to->curlPostMultipartOut($url, $params = array(), $proxy = '')
(resource) $to->curlStreaming($url, callable $callback, $params = array(), $proxy = '')

Arguments

(Omitted)

Return Value

A cURL resource.

static TwistOAuth::curlMultiExec()
static TwistOAuth::curlMultiStreaming()

(array) TwistOAuth::curlMultiExec(array $curls, $throw_in_process = false)
(array) $to->curlMultiExec(array $curls, $throw_in_process = false)
(void) TwistOAuth::curlMultiStreaming(array $curls) // $throw_in_process is always true
(void) $to->curlMultiStreaming(array $curls) // $throw_in_process is always true

Arguments, Return Value, Exception

  • (array) $curls
    An array of cURL resources.
  • (bool) $throw_in_process
    See below.

Example:

try {
    
    $throw_in_process = /* true or false*/ ;
    $result = $to->curlMultiExec(array(
        'a' => $to->curlGet('users/show', array(
            'screen_name' => 'foofoofoobarbarbarbazbazbaz', // invalid screen_name
        )),
        'b' => $to->curlGet('users/show', array(
            'screen_name' => 'twitter', // valid screen_name
        )),
    ), $throw_in_process);
    
    echo "Flow A\n";
    foreach ($result as $k => $v) {
        printf("[%s] %s\n", $k, $v instanceof stdClass ? $v->screen_name : $v->getMessage());
    }
    
} catch (TwistException $e) {
    
    echo "Flow B\n";
    printf("%s\n", $e->getMessage());
    
}

If $throw_in_process is false...

Flow A
[a] Sorry, that page does not exist
[b] twitter

If $throw_in_process is true...

Flow B
(a) Sorry, that page does not exist