PHPackages                             mpyw/twistoauth - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. mpyw/twistoauth

Abandoned → [mpyw/cowitter](/?search=mpyw%2Fcowitter)ArchivedLibrary[Authentication &amp; Authorization](/categories/authentication)

mpyw/twistoauth
===============

Advanced PHP Twitter library.

3.5.2(10y ago)229.0k7BSD-2-ClausePHPPHP &gt;=5.3.2

Since Jan 24Pushed 9y ago1 watchersCompare

[ Source](https://github.com/mpyw/TwistOAuth)[ Packagist](https://packagist.org/packages/mpyw/twistoauth)[ RSS](/packages/mpyw-twistoauth/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (1)Dependencies (4)Versions (2)Used By (0)

TwistOAuth
==========

[](#twistoauth)

**Warning:****This package is abandoned and no longer maintained. Use [mpyw/cowitter](https://github.com/mpyw/cowitter) package instead.**

Advanced PHP Twitter library.
Version 3.5.2

Requirements
============

[](#requirements)

- PHP version **5.3.2** or later
- **libcurl** (Sorry, required version is unknown)

Features
========

[](#features)

Basic:

- Using **GZIP compressed** connections
- Automatically decode responses
- Automatically fix weird responses
- Exception handling
- Requests for **REST** API
- Requests for **Streaming** API
- Requests using **OAuth Echo**
- Requests via **Proxy**
- Multipart requests

Abusing:

- **Asynchronous Multiple requests**
- **Asynchronous Multiple streaming**
- **Direct OAuth** authentication

Preparation
===========

[](#preparation)

### 1. Download this library

[](#1-download-this-library)

You can choose one of the following methods.

#### Direct Download

[](#direct-download)

Click [here](https://github.com/mpyw/TwistOAuth/raw/master/build/TwistOAuth.phar) to save `TwistOAuth.phar` in your working directory.

#### Composer

[](#composer)

Modify `require` directive in `composer.json`.

```
{
    "require": {
        "mpyw/twistoauth": "~3.0"
    }
}
```

If you choose this, replace all

```
require __DIR__ . '/TwistOAuth.phar';
```

into

```
require __DIR__ . '/vendor/autoload.php';
```

in examples.

### 2. Register your application

[](#2-register-your-application)

You can manage your API keys in [https://apps.twitter.com](https://apps.twitter.com/). Now, let's register your own application.

1. Click `Create New App`
2. Fill `Name` `Description` `WebSite`.
3. Fill `Callback URL`. By default, users are redirected here after successfully authenticating.
4. Read rules and check `Yes, I agree`.
5. Click `Create your Twitter application`.

**NOTE: `localhost` is not available for Callback URL. Use `127.0.0.1` instead.**

### 3. Change application permissions

[](#3-change-application-permissions)

By default, you can only read tweets but cannot post tweets. You have to configure permission settings.

1. Open detail page of your application.
2. Click `Permissions` Tab.
3. Select **`Read, Write and Access direct messages`**.
4. Click `Update settings`.

### 4. Note your *consumer\_key* and *consumer\_secret*

[](#4-note-your-consumer_key-and-consumer_secret)

These parameters are identifier for **your application**.

1. Open detail page of your application.
2. Click `API Keys` Tab.
3. Note `API key` and `API secret`. They mean *consumer\_key* and *consumer\_secret*.

### 5. Generate your *access\_token* and *access\_token\_secret*

[](#5-generate-your-access_token-and-access_token_secret)

These parameters are identifier for **your account**.

1. Open detail page of your application.
2. Click `API Keys` Tab.
3. Click `Generate my access token`.
4. Note `Access token` and `Access token secret`.

Contents
========

[](#contents)

- **[Examples](https://github.com/Certainist/TwistOAuth/blob/master/README_EXAMPLES.md)**
- **[Details](https://github.com/Certainist/TwistOAuth/blob/master/README_DETAILS.md)**

FAQ
===

[](#faq)

- [How can I learn about Twitter API?](#how-can-i-learn-about-twitter-api)
- [Aren't there any nice authentication tools for obtaining tokens?](#arent-there-any-nice-authentication-tools-for-obtaining-tokens)
- [How do I use OAuth 2.0 authentication flow?](#how-do-i-use-oauth-20-authentication-flow)
- [What is `oauth_verifier`?](#what-is-oauth_verifier-)
- [What is `oauth_callback`?](#what-is-oauth_callback-)
- [How do I use `$to` in callback closure?](#how-do-i-use-to-in-callback-closure)
- [Are all classes immutable?](#are-all-classes-immutable)
- [Why don't you use namespace?](#why-dont-you-use-namespace)
- [Tweets are already escaped... wtf!?](#tweets-are-already-escaped-wtf)
- [User description contains unescaped `&`... wtf!?](#user-description-contains-unescaped--wtf)
- [cURL causes `SSL certificate problem` error in Windows!](#curl-causes-ssl-certificate-problem-error-in-windows)

### How can I learn about Twitter API?

[](#how-can-i-learn-about-twitter-api)

Learn from documentation.

- **[REST API: endpoints](https://dev.twitter.com/rest/public)**
- **[REST API: field guides](https://dev.twitter.com/overview/api)**
- [Streaming API: endpoints and field guides](https://dev.twitter.com/streaming/overview)

Or watch actual response. The following tool is very very useful.

- **[twitSandbox](http://twitsandbox.com/)**

### Aren't there any nice authentication tools for obtaining tokens?

[](#arent-there-any-nice-authentication-tools-for-obtaining-tokens)

Try the following commandline utility.

- **[mpyw/twhelp](https://github.com/mpyw/twhelp)** **(Cross-compiled binaries)**

### How do I use OAuth 2.0 authentication flow?

[](#how-do-i-use-oauth-20-authentication-flow)

Sorry, it is not available with this library. Use OAuth 1.0a instead.

### What is `oauth_verifier` ?

[](#what-is-oauth_verifier-)

It is **required** for calling the following methods.

- `TwistOAuth::renewWithAccessToken()`
- `TwistOAuth::curlPostAccessToken()`

You can get it after user redirecting.

```
$oauth_verifier = filter_input(INPUT_GET, 'oauth_verifier');
```

### What is `oauth_callback` ?

[](#what-is-oauth_callback-)

It is **not required**, but you can apply it for calling the following methods.

- `TwistOAuth::renewWithRequestToken()`
- `TwistOAuth::curlPostRequestToken()`

There are three value types.

NameExample ValueAuthentication TypeEmpty String`""`PIN or URL (Use default setting)URL`"http://example.com/callback.php"`URLOut-Of-Band`"oob"`PIN**WARNING:**
You can only use URL if your application is configured as **Browser Application**.
This means `Callback URL` is not empty.

### How do I use `$to` in callback closure?

[](#how-do-i-use-to-in-callback-closure)

Use `use()`.

```
$to->streaming('user', function ($status) use ($to) { ... });
```

### How do I ignore `TwistException` thrown?

[](#how-do-i-ignore-twistexception-thrown)

Now your code is:

```
try {
    $to->post('statuses/update', array('status' => 'test'));
} catch (TwistException $e) { } // This is very lengthy!!!
```

To ignore all responses...

```
curl_exec($to->curlPost('statuses/update', array('status' => 'test'))); // Wow, cool
```

### Are all classes immutable?

[](#are-all-classes-immutable)

Yes.

```
$a = new TwistOAuth('CK', 'CS');
$b = $a->renewWithRequestToken();
var_dump($a === $b); // false
```

However, you can change propety values by directly calling `__construct()`.

```
$obj = new TwistOAuth('a', 'b');
$obj->__construct('c', 'd'); // Break immutable rules
```

### Why don't you use namespace?

[](#why-dont-you-use-namespace)

This is because of the compatibility with previous versions of [abraham/twitteroauth](https://github.com/abraham/twitteroauth).
I believe that the prefix `Twist-` will never collide with any other libraries.

### Tweets are already escaped... wtf!?

[](#tweets-are-already-escaped-wtf)

HTML special chars in texts of statuses are already escaped by Twitter like this.

```
$status->text = htmlspecialchars($status->text, ENT_NOQUOTES, 'UTF-8');
```

**WARNING:**
The flag is **`ENT_NOQUOTES`**, not `ENT_QUOTES` or `ENT_COMPAT`.
The following snippet may print broken HTML.

```

```

You should do like this.

```
name:
