Last active
March 31, 2018 15:55
-
-
Save reytech-dev/2b1f6f0f366b565188e02c23b6956451 to your computer and use it in GitHub Desktop.
OAuth code snippet for Twitter API (Use curl or GuzzleHttp)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Generates a 42 chars random string (No need to change it) | |
*/ | |
$oauth_nonce = preg_replace("/[^A-Za-z0-9 ]/", "", base64_encode(random_bytes(32))); | |
/** | |
* Get current unix timestamp (careful what is set as Locale in your PHP environment - No need to change it) | |
*/ | |
$oauth_timestamp = time(); | |
/** | |
* OAuth version which is used by Twitter (https://tools.ietf.org/html/rfc5849 - No need to change it) | |
*/ | |
$oauth_version = "1.0"; | |
/** | |
* API docs can be found here: https://developer.twitter.com/en/docs/api-reference-index (Change it to your needs) | |
*/ | |
$requestUrl = "https://api.twitter.com/1.1/account/settings.json"; | |
/** | |
* Depends on the request you make. Method is always mentioned in the docs. (Change it to your needs) | |
*/ | |
$oauth_signature_method = "GET"; | |
/** | |
* This information is available at apps.twitter.com. Requires a twitter account. You also have to create an app | |
*/ | |
$oauth_consumer_key = ""; // Application Settings: Consumer Key (API Key) | |
$oauth_token = ""; // Your Access Token: Access Token | |
$consumer_secret = ""; // Application Settings: Consumer Secret (API Secret) | |
$oauth_secret = ""; // Your Access Token: Access Token Secret | |
/** | |
* Prepare signature string ("Signature base string" based on https://developer.twitter.com/en/docs/basics/authentication/guides/creating-a-signature.html) | |
*/ | |
$signature = "GET&" . rawurlencode($requestUrl) . "&" . rawurlencode("oauth_consumer_key=" . $oauth_consumer_key . "&oauth_nonce=" . $oauth_nonce . "&oauth_signature_method=" . $oauth_signature_method . "&oauth_timestamp=" . $oauth_timestamp . "&oauth_token=" . $oauth_token . "&oauth_version=" . $oauth_version); | |
/** | |
* Prepare signature signing key ("Signing key" based on https://developer.twitter.com/en/docs/basics/authentication/guides/creating-a-signature.html) | |
*/ | |
$key = rawurlencode($consumer_secret) . "&" . rawurlencode($oauth_secret); | |
/** | |
* Create signature with hash_hmac function and encode it in base64 since hash_hmac returns the binary representation of the message digest with raw_output: true | |
*/ | |
$hashedAndEncodedSig = base64_encode(hash_hmac("sha1", $signature, $key, true)); | |
/** | |
* Generate Authorization Header which has to be sent in the "Authorization" field | |
*/ | |
$authHeader = "OAuth oauth_consumer_key=\"" . rawurlencode($oauth_consumer_key) . "\", oauth_nonce=\"" . rawurlencode($oauth_nonce) . "\", oauth_signature=\"" . rawurlencode($hashedAndEncodedSig) . "\", oauth_signature_method=\"" . rawurlencode($oauth_signature_method) . "\", oauth_timestamp=\"" . rawurlencode($oauth_timestamp) . "\", oauth_token=\"" . rawurlencode($oauth_token) . "\", oauth_version=\"" . rawurlencode($oauth_version) . "\""; | |
// For sending the request use curl, GuzzleHttp or whatever you like |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment