Overview

Namespaces

  • Codebird

Classes

  • Codebird
  • Overview
  • Namespace
  • Class

Class Codebird

A Twitter library in PHP.

Namespace: Codebird
Package: codebird-php
Located at codebird.php
Methods summary
public
# __construct( )

Class constructor

public static Codebird\Codebird
# getInstance( )

Returns singleton class instance Always use this method unless you're working with multiple authenticated users at once

Returns singleton class instance Always use this method unless you're working with multiple authenticated users at once

Returns

Codebird\Codebird
The instance
public static
# setConsumerKey( string $key, string $secret )

Sets the OAuth consumer key and secret (App key)

Sets the OAuth consumer key and secret (App key)

Parameters

$key
OAuth consumer key
$secret
OAuth consumer secret
public static
# setBearerToken( string $token )

Sets the OAuth2 app-only auth bearer token

Sets the OAuth2 app-only auth bearer token

Parameters

$token
OAuth2 bearer token
public string
# getVersion( )

Gets the current Codebird version

Gets the current Codebird version

Returns

string
The version number
public
# setToken( string $token, string $secret )

Sets the OAuth request or access token and secret (User key)

Sets the OAuth request or access token and secret (User key)

Parameters

$token
OAuth request or access token
$secret
OAuth request or access token secret
public boolean
# logout( )

Forgets the OAuth request or access token and secret (User key)

Forgets the OAuth request or access token and secret (User key)

Returns

boolean
public
# setUseCurl( boolean $use_curl )

Sets if codebird should use cURL

Sets if codebird should use cURL

Parameters

$use_curl
Request uses cURL or not
public
# setTimeout( integer $timeout )

Sets request timeout in milliseconds

Sets request timeout in milliseconds

Parameters

$timeout
Request timeout in milliseconds
public
# setConnectionTimeout( integer $timeout )

Sets connection timeout in milliseconds

Sets connection timeout in milliseconds

Parameters

$timeout
Connection timeout in milliseconds
public
# setRemoteDownloadTimeout( integer $timeout )

Sets remote media download timeout in milliseconds

Sets remote media download timeout in milliseconds

Parameters

$timeout
Remote media timeout in milliseconds
public
# setReturnFormat( integer $return_format )

Sets the format for API replies

Sets the format for API replies

Parameters

$return_format

One of these: CODEBIRD_RETURNFORMAT_OBJECT (default) CODEBIRD_RETURNFORMAT_ARRAY

public
# setProxy( string $host, integer $port )

Sets the proxy

Sets the proxy

Parameters

$host
Proxy host
$port
Proxy port
public
# setProxyAuthentication( string $authentication )

Sets the proxy authentication

Sets the proxy authentication

Parameters

$authentication
Proxy authentication
public
# setStreamingCallback( callable $callback )

Sets streaming callback

Sets streaming callback

Parameters

$callback
The streaming callback
public array
# getApiMethods( )

Get allowed API methods, sorted by GET or POST Watch out for multiple-method API methods!

Get allowed API methods, sorted by GET or POST Watch out for multiple-method API methods!

Returns

array
$apimethods
public string
# __call( string $fn, array $params )

Main API handler working on any requests you issue

Main API handler working on any requests you issue

Parameters

$fn
The member function you called
$params
The parameters you sent along

Returns

string
The API reply encoded in the set return_format
protected array
# _parseApiParams( array|string $params )

Parse given params, detect query-style params

Parse given params, detect query-style params

Parameters

$params
Parameters to parse

Returns

array
$apiparams
protected array
# _stringifyNullBoolParams( array $apiparams )

Replace null and boolean parameters with their string representations

Replace null and boolean parameters with their string representations

Parameters

$apiparams
Parameter array to replace in

Returns

array
$apiparams
protected string[]
# _mapFnToApiMethod( string $fn, array & $apiparams )

Maps called PHP magic method name to Twitter API method

Maps called PHP magic method name to Twitter API method

Parameters

$fn
Function called
$apiparams
byref API parameters

Returns

string[]
(string method, string method_template)
protected string
# _mapFnInsertSlashes( string $fn )

API method mapping: Replaces _ with / character

API method mapping: Replaces _ with / character

Parameters

$fn
Function called

Returns

string
API method to call
protected string
# _mapFnRestoreParamUnderscores( string $method )

API method mapping: Restore _ character in named parameters

API method mapping: Restore _ character in named parameters

Parameters

$method
API method to call

Returns

string
API method with restored underscores
public string
# oauth_authenticate( optional $force_login = NULL, optional $screen_name = NULL, optional $type = 'authenticate' )

Gets the OAuth authenticate URL for the current request token

Gets the OAuth authenticate URL for the current request token

Parameters

$force_login
$force_login Whether to force the user to enter their login data
$screen_name
$screen_name Screen name to repopulate the user name with
$type
$type 'authenticate' or 'authorize', to avoid duplicate code

Returns

string
The OAuth authenticate/authorize URL
public string
# oauth_authorize( optional $force_login = NULL, optional $screen_name = NULL )

Gets the OAuth authorize URL for the current request token

Gets the OAuth authorize URL for the current request token

Parameters

$force_login
$force_login Whether to force the user to enter their login data
$screen_name
$screen_name Screen name to repopulate the user name with

Returns

string
The OAuth authorize URL
public string
# oauth2_token( )

Gets the OAuth bearer token

Gets the OAuth bearer token

Returns

string
The OAuth bearer token
protected resource
# getCurlInitialization( string $url )

Gets a cURL handle

Gets a cURL handle

Parameters

$url
the URL for the curl initialization

Returns

resource
handle
protected array
# getNoCurlInitialization( string $url, array $contextOptions, string $hostname = '' )

Gets a non cURL initialization

Gets a non cURL initialization

Parameters

$url
the URL for the curl initialization
$contextOptions
the options for the stream context
$hostname
the hostname to verify the SSL FQDN for

Returns

array
the read data
protected
# hasProxy( )
protected
# hasProxyAuthentication( )
protected string
# getProxyHost( )

Gets the proxy host

Gets the proxy host

Returns

string
The proxy host
protected string
# getProxyPort( )

Gets the proxy port

Gets the proxy port

Returns

string
The proxy port
protected string
# getProxyAuthentication( )

Gets the proxy authentication

Gets the proxy authentication

Returns

string
The proxy authentication
protected string
# _oauth2TokenCurl( )

Gets the OAuth bearer token, using cURL

Gets the OAuth bearer token, using cURL

Returns

string
The OAuth bearer token
protected string
# _oauth2TokenNoCurl( )

Gets the OAuth bearer token, without cURL

Gets the OAuth bearer token, without cURL

Returns

string
The OAuth bearer token
protected string
# _parseBearerReply( string $result, integer $httpstatus )

Parse oauth2_token reply and set bearer token, if found

Parse oauth2_token reply and set bearer token, if found

Parameters

$result
Raw HTTP response
$httpstatus
HTTP status code

Returns

string
reply
protected null|array
# _getRateLimitInfo( array $headers )

Extract rate-limiting data from response headers

Extract rate-limiting data from response headers

Parameters

$headers
The CURL response headers

Returns

null|array
The rate-limiting information
protected
# _validateSslCertificate( integer $validation_result )

Check if there were any SSL certificate errors

Check if there were any SSL certificate errors

Parameters

$validation_result
The curl error number
protected mixed
# _url( mixed $data )

URL-encodes the given data

URL-encodes the given data

Parameters

$data

Returns

mixed
The encoded data
protected string
# _sha1( string $data )

Gets the base64-encoded SHA1 hash for the given data

Gets the base64-encoded SHA1 hash for the given data

Parameters

$data
The data to calculate the hash from

Returns

string
The hash
protected string
# _nonce( integer $length = 8 )

Generates a (hopefully) unique random string

Generates a (hopefully) unique random string

Parameters

$length
$length The length of the string to generate

Returns

string
The random string
protected string
# _getSignature( string $httpmethod, string $method, array $base_params )

Signature helper

Signature helper

Parameters

$httpmethod
Usually either 'GET' or 'POST' or 'DELETE'
$method
The API method to call
$base_params
The signature base parameters

Returns

string
signature
protected string
# _sign( string $httpmethod, string $method, array $params = [], boolean $append_to_get = false )

Generates an OAuth signature

Generates an OAuth signature

Parameters

$httpmethod
Usually either 'GET' or 'POST' or 'DELETE'
$method
The API method to call
$params
$params The API call parameters, associative
$append_to_get
append_to_get Whether to append the OAuth params to GET

Returns

string
Authorization HTTP header
protected string
# _detectMethod( string $method, array & $params )

Detects HTTP method to use for API call

Detects HTTP method to use for API call

Parameters

$method
The API method to call
$params
$params The parameters to send along

Returns

string
The HTTP method that should be used
protected boolean
# _detectMultipart( string $method )

Detects if API call should use multipart/form-data

Detects if API call should use multipart/form-data

Parameters

$method
The API method to call

Returns

boolean
Whether the method should be sent as multipart
protected string
# _getMultipartRequestFromParams( string $method_template, string $border, array $params )

Merge multipart string from parameters array

Merge multipart string from parameters array

Parameters

$method_template
The method template to call
$border
The multipart border
$params
The parameters to send along

Returns

string
request
protected mixed
# _checkForFiles( string $method_template, string $key, array $value )

Check for files

Check for files

Parameters

$method_template
The method template to call
$key
The parameter name
$value
The possible file name or URL

Returns

mixed
protected null|string
# _buildMultipart( string $method, array $params )

Detect filenames in upload parameters, build multipart request from upload params

Detect filenames in upload parameters, build multipart request from upload params

Parameters

$method
The API method to call
$params
The parameters to send along

Returns

null|string
protected null|string
# _buildBinaryBody( mixed $input )

Detect filenames in upload parameters

Detect filenames in upload parameters

Parameters

$input
The data or file name to parse

Returns

null|string
protected mixed
# _fetchRemoteFile( string $url )

Fetches a remote file

Fetches a remote file

Parameters

$url
The URL to download from

Returns

mixed
The file contents or FALSE
protected boolean
# _detectMedia( string $method )

Detects if API call should use media endpoint

Detects if API call should use media endpoint

Parameters

$method
The API method to call

Returns

boolean
Whether the method is defined in media API
protected boolean
# _detectJsonBody( string $method )

Detects if API call should use JSON body

Detects if API call should use JSON body

Parameters

$method
The API method to call

Returns

boolean
Whether the method is defined as accepting JSON body
protected boolean
# _detectBinaryBody( string $method_template )

Detects if API call should use binary body

Detects if API call should use binary body

Parameters

$method_template
The API method to call

Returns

boolean
Whether the method is defined as accepting binary body
protected string|false
# _detectStreaming( string $method )

Detects if API call should use streaming endpoint, and if yes, which one

Detects if API call should use streaming endpoint, and if yes, which one

Parameters

$method
The API method to call

Returns

string|false
Variant of streaming API to be used
protected string
# _getEndpoint( string $method, string $method_template )

Builds the complete API endpoint url

Builds the complete API endpoint url

Parameters

$method
The API method to call
$method_template
The API method to call

Returns

string
The URL to send the request to
protected string
# _callApi( string $httpmethod, string $method, string $method_template, array $params = [], boolean $multipart = false, boolean $app_only_auth = false )

Calls the API

Calls the API

Parameters

$httpmethod
The HTTP method to use for making the request
$method
The API method to call
$method_template
The API method template to call
$params
$params The parameters to send along
$multipart
$multipart Whether to use multipart/form-data
$app_only_auth
$app_only_auth Whether to use app-only bearer authentication

Returns

string
The API reply, encoded in the set return_format
protected string
# _callApiCurl( string $httpmethod, string $method, string $method_template, array $params = [], boolean $multipart = false, boolean $app_only_auth = false )

Calls the API using cURL

Calls the API using cURL

Parameters

$httpmethod
The HTTP method to use for making the request
$method
The API method to call
$method_template
The API method template to call
$params
$params The parameters to send along
$multipart
$multipart Whether to use multipart/form-data
$app_only_auth
$app_only_auth Whether to use app-only bearer authentication

Returns

string
The API reply, encoded in the set return_format
protected string
# _callApiNoCurl( string $httpmethod, string $method, string $method_template, array $params = [], boolean $multipart = false, boolean $app_only_auth = false )

Calls the API without cURL

Calls the API without cURL

Parameters

$httpmethod
The HTTP method to use for making the request
$method
The API method to call
$method_template
The API method template to call
$params
$params The parameters to send along
$multipart
$multipart Whether to use multipart/form-data
$app_only_auth
$app_only_auth Whether to use app-only bearer authentication

Returns

string
The API reply, encoded in the set return_format
protected string[]
# _callApiPreparationsGet( string $httpmethod, string $url, array $params, boolean $app_only_auth )

Do preparations to make the API GET call

Do preparations to make the API GET call

Parameters

$httpmethod
The HTTP method to use for making the request
$url
The URL to call
$params
The parameters to send along
$app_only_auth
Whether to use app-only bearer authentication

Returns

string[]
(string authorization, string url)
protected array
# _callApiPreparationsPost( string $httpmethod, string $url, string $method, string $method_template, array $params, boolean $multipart, boolean $app_only_auth )

Do preparations to make the API POST call

Do preparations to make the API POST call

Parameters

$httpmethod
The HTTP method to use for making the request
$url
The URL to call
$method
The API method to call
$method_template
The API method template to call
$params
The parameters to send along
$multipart
Whether to use multipart/form-data
$app_only_auth
Whether to use app-only bearer authentication

Returns

array
(string authorization, array params, array request_headers)
protected string
# _getBearerAuthorization( )

Get Bearer authorization string

Get Bearer authorization string

Returns

string
authorization
protected array
# _callApiPreparations( string $httpmethod, string $method, string $method_template, array $params, boolean $multipart, boolean $app_only_auth )

Do preparations to make the API call

Do preparations to make the API call

Parameters

$httpmethod
The HTTP method to use for making the request
$method
The API method to call
$method_template
The API method template to call
$params
The parameters to send along
$multipart
Whether to use multipart/form-data
$app_only_auth
Whether to use app-only bearer authentication

Returns

array
(string authorization, string url, array params, array request_headers)
protected
# _callApiStreaming( string $httpmethod, string $method, string $method_template, array $params = [], boolean $app_only_auth = false )

Calls the streaming API

Calls the streaming API

Parameters

$httpmethod
The HTTP method to use for making the request
$method
The API method to call
$method_template
The API method template to call
$params
$params The parameters to send along
$app_only_auth
$app_only_auth Whether to use app-only bearer authentication
protected boolean
# _deliverStreamingMessage( string|array|object $message )

Calls streaming callback with received message

Calls streaming callback with received message

Parameters

$message
message

Returns

boolean
Whether to cancel streaming
protected array
# _parseApiHeaders( string $reply )

Parses the API reply to separate headers from the body

Parses the API reply to separate headers from the body

Parameters

$reply
The actual raw HTTP request reply

Returns

array
(headers, reply)
protected string
# _parseApiReplyPrefillHeaders( array $headers, string $reply )

Parses the API headers to return Location and Ton API headers

Parses the API headers to return Location and Ton API headers

Parameters

$headers
The headers list
$reply
The actual HTTP body

Returns

string
$reply
protected array|string|object
# _parseApiReply( string $reply )

Parses the API reply to encode it in the set return_format

Parses the API reply to encode it in the set return_format

Parameters

$reply
The actual HTTP body, JSON-encoded or URL-encoded

Returns

array|string|object
The parsed reply
Properties summary
protected static $_oauth_consumer_key

The OAuth consumer key of your registered app

The OAuth consumer key of your registered app

# null
protected static $_oauth_consumer_secret

The corresponding consumer secret

The corresponding consumer secret

# null
protected static $_oauth_bearer_token

The app-only bearer token. Used to authorize app-only requests

The app-only bearer token. Used to authorize app-only requests

# null
protected static string $_endpoint

The API endpoint to use

The API endpoint to use

# 'https://api.twitter.com/1.1/'
protected static string $_endpoint_media

The media API endpoint to use

The media API endpoint to use

# 'https://upload.twitter.com/1.1/'
protected static array $_endpoints_streaming

The Streaming API endpoints to use

The Streaming API endpoints to use

# [ 'public' => 'https://stream.twitter.com/1.1/', 'user' => 'https://userstream.twitter.com/1.1/', 'site' => 'https://sitestream.twitter.com/1.1/' ]
protected static string $_endpoint_ton

The TON API endpoint to use

The TON API endpoint to use

# 'https://ton.twitter.com/1.1/'
protected static string $_endpoint_ads

The Ads API endpoint to use

The Ads API endpoint to use

# 'https://ads-api.twitter.com/0/'
protected static string $_endpoint_ads_sandbox

The Ads Sandbox API endpoint to use

The Ads Sandbox API endpoint to use

# 'https://ads-api-sandbox.twitter.com/0/'
protected static string $_endpoint_oauth

The API endpoint base to use

The API endpoint base to use

# 'https://api.twitter.com/'
protected static array $_possible_files

Possible file name parameters

Possible file name parameters

# [ // Tweets 'statuses/update_with_media' => ['media[]'], 'media/upload' => ['media'], // Accounts 'account/update_profile_background_image' => ['image'], 'account/update_profile_image' => ['image'], 'account/update_profile_banner' => ['banner'] ]
protected $_oauth_token

The Request or access token. Used to sign requests

The Request or access token. Used to sign requests

# null
protected $_oauth_token_secret

The corresponding request or access token secret

The corresponding request or access token secret

# null
protected $_return_format

The format of data to return from API calls

The format of data to return from API calls

# CODEBIRD_RETURNFORMAT_OBJECT
protected array $_supported_media_files

The file formats that Twitter accepts as image uploads

The file formats that Twitter accepts as image uploads

# [ IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_BMP, IMAGETYPE_GIF //, IMAGETYPE_WEBP ]
protected $_streaming_callback

The callback to call with any new streaming messages

The callback to call with any new streaming messages

# null
protected string $_version

The current Codebird version

The current Codebird version

# '3.0.0-dev'
protected boolean $_use_curl

Auto-detect cURL absence

Auto-detect cURL absence

# true
protected integer $_timeout

Request timeout

Request timeout

# 10000
protected integer $_connectionTimeout

Connection timeout

Connection timeout

# 3000
protected integer $_remoteDownloadTimeout

Remote media download timeout

Remote media download timeout

# 5000
protected array $_proxy

Proxy

Proxy

# []
API documentation generated by ApiGen