Issues (144)

examples/provider/twitter.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * Example of retrieving an authentication token of the Twitter service.
5
 *
6
 * PHP version 5.4
7
 *
8
 * @author     David Desberg <[email protected]>
9
 * @author     Pieter Hordijk <[email protected]>
10
 * @license    http://www.opensource.org/licenses/mit-license.html  MIT License
11
 */
12
13
use OAuth\Common\Consumer\Credentials;
14
use OAuth\Common\Storage\Session;
15
use OAuth\OAuth1\Service\Twitter;
16
17
/**
18
 * Bootstrap the example.
19
 */
20
require_once __DIR__ . '/bootstrap.php';
21
22
// We need to use a persistent storage to save the token, because oauth1 requires the token secret received before'
23
// the redirect (request token request) in the access token request.
24
$storage = new Session();
25
26
// Setup the credentials for the requests
27
$credentials = new Credentials(
28
    $servicesCredentials['twitter']['key'],
29
    $servicesCredentials['twitter']['secret'],
30
    $currentUri->getAbsoluteUri()
31
);
32
33
// Instantiate the twitter service using the credentials, http client and storage mechanism for the token
34
/** @var Twitter $twitterService */
35
$twitterService = $serviceFactory->createService('twitter', $credentials, $storage);
36
37
if (!empty($_GET['oauth_token'])) {
38
    $token = $storage->retrieveAccessToken('Twitter');
39
40
    // This was a callback request from twitter, get the token
41
    $twitterService->requestAccessToken(
42
        $_GET['oauth_token'],
43
        $_GET['oauth_verifier'],
44
        $token->getRequestTokenSecret()
45
    );
46
47
    // Send a request now that we have access token
48
    $result = json_decode($twitterService->request('account/verify_credentials.json'));
49
50
    echo 'result: <pre>' . print_r($result, true) . '</pre>';
0 ignored issues
show
Are you sure print_r($result, true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
    echo 'result: <pre>' . /** @scrutinizer ignore-type */ print_r($result, true) . '</pre>';
Loading history...
51
} elseif (!empty($_GET['go']) && $_GET['go'] === 'go') {
52
    // extra request needed for oauth1 to request a request token :-)
53
    $token = $twitterService->requestRequestToken();
54
55
    $url = $twitterService->getAuthorizationUri(['oauth_token' => $token->getRequestToken()]);
56
    header('Location: ' . $url);
57
} else {
58
    $url = $currentUri->getRelativeUri() . '?go=go';
59
    echo "<a href='$url'>Login with Twitter!</a>";
60
}
61