Skip to content

Instantly share code, notes, and snippets.

@Alphadelta14
Last active November 26, 2016 01:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Alphadelta14/3e9e10767b5c5453066917527fd43608 to your computer and use it in GitHub Desktop.
Save Alphadelta14/3e9e10767b5c5453066917527fd43608 to your computer and use it in GitHub Desktop.
IPS [bad] twitter oauth
<?php
/**
* Send Request
*
* @param string $method HTTP Method
* @param string $url URL
* @param array $params Parameters
* @param string $token OAuth Token
* @return \IPS\Http\Response
* @throws \IPS\Http\Request\Exception
*/
public function sendRequest( $method, $url, $params=array(), $token='', $secret='' )
{
/* Generate the OAUTH Authorization Header */
$OAuthAuthorization = array_merge( array(
'oauth_consumer_key' => $this->settings['consumer_key'],
'oauth_nonce' => md5( \IPS\Login::generateRandomString() ),
'oauth_signature_method'=> 'HMAC-SHA1',
'oauth_timestamp' => time(),
'oauth_version' => '1.0'
) );
if ( $token ) {
$OAuthAuthorization['oauth_token'] = $token;
}
ksort( $OAuthAuthorization );
$signatureBaseString = mb_strtoupper( $method ) . '&' . rawurlencode( $url ) . '&' . rawurlencode( http_build_query( $OAuthAuthorization ) );
$signingKey = rawurlencode( $this->settings['consumer_secret'] ) . '&' . rawurlencode( $secret ?: $token );
$OAuthAuthorizationEncoded = array();
foreach ( $OAuthAuthorization as $k => $v )
{
$OAuthAuthorizationEncoded[] = rawurlencode( $k ) . '="' . rawurlencode( $v ) . '"';
if ( $k === 'oauth_nonce' )
{
$signature = base64_encode( hash_hmac( 'sha1', $signatureBaseString, $signingKey, TRUE ) );
$OAuthAuthorizationEncoded[] = rawurlencode( 'oauth_signature' ) . '="' . rawurlencode( $signature ) . '"';
}
}
$OAuthAuthorizationHeader = 'OAuth ' . implode( ', ', $OAuthAuthorizationEncoded );
/* Send the request */
return \IPS\Http\Url::external( $url )->request()->setHeaders( array( 'Authorization' => $OAuthAuthorizationHeader ) )->$method( $params );
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment