PHP code example of serendipity_hq / oauth-guzzle-middleware

1. Go to this page and download the library: Download serendipity_hq/oauth-guzzle-middleware library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

serendipity_hq / oauth-guzzle-middleware example snippets


/**
 * Sends ALL THE REQUESTS authenticated with OAuth.
 * @see [docs/examples/Twitter/](docs/examples/Twitter/) for more details.
 */

use GuzzleHttp\Client;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware\OpenAuthentication\OAuth10a;
use GuzzleHttp\RequestOptions;

// Consumer Credentials: created at Precondition 2
$consumerKey    = 'your-consumer-key';
$consumerSecret = 'your-consumer-secret';

// Set here the access token generated at Precondition 3
$accessTokenKey    = 'your-token-key';
$accessTokenSecret = 'your-token-secret';

// The home_timeline endpoint
$resourceUrl = 'statuses/home_timeline.json';

try {
    // Instantiate the Guzzle Client and the OAuth10a middleware
    $handler = new CurlHandler();
    $stack = HandlerStack::create($handler);
    $middleware = new OAuth10a([
        'consumer_key'     => $consumerKey,
        'consumer_secret'  => $consumerSecret,
        'token'            => $accessTokenKey,
        'token_secret'     => $accessTokenSecret,
        'request_method'   => OAuth10a::REQUEST_METHOD_HEADER,
        'signature_method' => OAuth10a::SIGNATURE_METHOD_HMAC,
    ]);
    $stack->push($middleware);

    // Set the client params
    $clientParams = [
        'base_uri'                      => 'https://api.twitter.com/1.1/',
        'handler'                       => $stack,
        // Set the oauth authentication for ALL REQUESTS
        RequestOptions::AUTH            => 'oauth',
        RequestOptions::HTTP_ERRORS     => false,
        RequestOptions::DEBUG           => true,
        RequestOptions::ALLOW_REDIRECTS => ['track_redirects' => true]
    ];

    $guzzleClient = new Client($clientParams);

    $tweetsList = $guzzleClient->get($resourceUrl);
    dump(json_decode($tweetsList->getBody()->__toString(), true));
} catch (\Exception $e) {
    dump($e->getMessage());
    echo "&lt;br/&gt;";
    dump($e);
}

/**
 * @see [docs/examples/Twitter/](docs/examples/Twitter/) for more details.
 */

...

try {
    ...
    $stack->push($middleware);

    // Set the client params
    $clientParams = [
        'base_uri'                      => 'https://api.twitter.com/1.1/',
        'handler'                       => $stack,
        // Remove the option from the CLient parameters
        // RequestOptions::AUTH            => 'oauth',
        RequestOptions::HTTP_ERRORS     => false,
        RequestOptions::DEBUG           => true,
        RequestOptions::ALLOW_REDIRECTS => ['track_redirects' => true]
    ];

    $guzzleClient = new Client($clientParams);

    $requestParams = [
        RequestOptions::AUTH => 'oauth'
    ];

    // Use OAuth on a pre Request basis
    $tweetsList = $guzzleClient->get($resourceUrl, $requestParams);

    dump(json_decode($tweetsList->getBody()->__toString(), true));
} catch (\Exception $e) {
    dump($e->getMessage());
    echo "&lt;br/&gt;";
    dump($e);
}

/**
 * Sends ALL THE REQUESTS authenticated with OAuth.
 * @see [docs/examples/Twitter/](docs/examples/Twitter/) for more details.
 */

use GuzzleHttp\Client;
use GuzzleHttp\Handler\CurlHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware\OpenAuthentication\OAuth10a;
use GuzzleHttp\RequestOptions;

// Consumer Credentials: created at Precondition 2
$consumerKey    = 'your-consumer-key';
$consumerSecret = 'your-consumer-secret';

// DO NOT SET THOSE PARAMETERS
// $accessTokenKey    = 'your-token-key';
// $accessTokenSecret = 'your-token-secret';

// The home_timeline endpoint
$resourceUrl = 'statuses/home_timeline.json';

try {
    // Instantiate the Guzzle Client and the OAuth10a middleware
    $handler = new CurlHandler();
    $stack = HandlerStack::create($handler);
    $middleware = new OAuth10a([
        'consumer_key'     => $consumerKey,
        'consumer_secret'  => $consumerSecret,
        // DO NOT SET THOSE PARAMETERS
        // 'token'            => $accessTokenKey,
        // 'token_secret'     => $accessTokenSecret,
        'request_method'   => OAuth10a::REQUEST_METHOD_HEADER,
        'signature_method' => OAuth10a::SIGNATURE_METHOD_HMAC,
    ]);
    $stack->push($middleware);

    // Set the client params
    $clientParams = [
        'base_uri'                      => 'https://api.twitter.com/1.1/',
        'handler'                       => $stack,
        // Set the oauth authentication for ALL REQUESTS
        RequestOptions::AUTH            => 'oauth',
        RequestOptions::HTTP_ERRORS     => false,
        RequestOptions::DEBUG           => true,
        RequestOptions::ALLOW_REDIRECTS => ['track_redirects' => true]
    ];

    $guzzleClient = new Client($clientParams);

    $tweetsList = $guzzleClient->get($resourceUrl);
    dump(json_decode($tweetsList->getBody()->__toString(), true));
} catch (\Exception $e) {
    dump($e->getMessage());
    echo "&lt;br/&gt;";
    dump($e);
}


    use GuzzleHttp\Middleware\OpenAuthentication\Oauth10a;

    $stack = HandlerStack::create();

    $middleware = new Oauth10a([
        'consumer_key'           => 'my_key',
        'consumer_secret'        => 'my_secret',
        'private_key_file'       => 'my_path_to_private_key_file',
        'private_key_passphrase' => 'my_passphrase',
        'signature_method'       => Oauth10a::SIGNATURE_METHOD_RSA,
    ]);
    $stack->push($middleware);

    $client = new Client([
        'handler' => $stack
    ]);

    $response = $client->get('http://httpbin.org', ['auth' => 'oauth']);