|
1 | 1 | <?php
|
| 2 | + |
2 | 3 | /**
|
3 | 4 | * Oauth.php
|
4 | 5 | *
|
|
10 | 11 | * Permissions beyond the scope of this license may be available at http://www.jonhurlock.com/.
|
11 | 12 | */
|
12 | 13 |
|
13 |
| -class twitterSearch |
| 14 | +class TwitterSearch |
14 | 15 | {
|
15 | 16 | // url to send data to for authentication
|
16 | 17 | const AUTH_URL = 'https://api.twitter.com/oauth2/token';
|
17 | 18 | const SEARCH_URL = 'https://api.twitter.com/1.1/search/tweets.json';
|
18 | 19 | CONST INVALIDATE_AUTH_URL = 'https://api.twitter.com/oauth2/invalidate_token';
|
19 | 20 |
|
20 | 21 | /**
|
21 |
| - * @brief Set app authentication data |
| 22 | + * @brief Authenticate a session with Twitter using OAuth |
22 | 23 | * @param string $consumerKey Your personal key from Twitter
|
23 | 24 | * @param string $consumerSecret Your personal secret from Twitter
|
| 25 | + * @note https://dev.twitter.com/docs/auth/application-only-auth |
24 | 26 | */
|
25 |
| - public function __construct( $consumerKey, $consumerSecret ) |
| 27 | + function authenticate( $consumerKey, $consumerSecret ) |
26 | 28 | {
|
27 |
| - // fill in your consumer key and consumer secret below |
28 | 29 | $this->consumerKey = $consumerKey;
|
29 | 30 | $this->consumerSecret = $consumerSecret;
|
30 |
| - } |
31 |
| - |
32 |
| - /** |
33 |
| - * @brief Authenticate a session with Twitter using OAuth |
34 |
| - * @note https://dev.twitter.com/docs/auth/application-only-auth |
35 |
| - */ |
36 |
| - function authenticate() |
37 |
| - { |
38 |
| - // Step 1 |
39 |
| - // step 1.1 - url encode the consumer_key and consumer_secret in accordance with RFC 1738 |
40 |
| - $encoded_consumer_key = urlencode($this->consumerKey); |
41 |
| - $encoded_consumer_secret = urlencode($this->consumerSecret); |
42 |
| - // step 1.2 - concatinate encoded consumer, a colon character and the encoded consumer secret |
43 |
| - $bearer_token = $encoded_consumer_key.':'.$encoded_consumer_secret; |
44 |
| - // step 1.3 - base64-encode bearer token |
45 |
| - $base64_encoded_bearer_token = base64_encode($bearer_token); |
46 |
| - // step 2 |
47 |
| - $headers = array( |
48 |
| - "POST /oauth2/token HTTP/1.1", |
49 |
| - "Host: api.twitter.com", |
50 |
| - "User-Agent: jonhurlock Twitter Application-only OAuth App v.1", |
51 |
| - "Authorization: Basic ".$base64_encoded_bearer_token."", |
52 |
| - "Content-Type: application/x-www-form-urlencoded;charset=UTF-8", |
53 |
| - "Content-Length: 29" |
54 |
| - ); |
| 31 | + // Step 1 |
| 32 | + // step 1.1 - url encode the consumer_key and consumer_secret in accordance with RFC 1738 |
| 33 | + $encoded_consumer_key = urlencode($this->consumerKey); |
| 34 | + $encoded_consumer_secret = urlencode($this->consumerSecret); |
| 35 | + // step 1.2 - concatinate encoded consumer, a colon character and the encoded consumer secret |
| 36 | + $bearer_token = $encoded_consumer_key.':'.$encoded_consumer_secret; |
| 37 | + // step 1.3 - base64-encode bearer token |
| 38 | + $base64_encoded_bearer_token = base64_encode($bearer_token); |
| 39 | + // step 2 |
| 40 | + $headers = array( |
| 41 | + "POST /oauth2/token HTTP/1.1", |
| 42 | + "Host: api.twitter.com", |
| 43 | + "User-Agent: jonhurlock Twitter Application-only OAuth App v.1", |
| 44 | + "Authorization: Basic ".$base64_encoded_bearer_token."", |
| 45 | + "Content-Type: application/x-www-form-urlencoded;charset=UTF-8", |
| 46 | + "Content-Length: 29" |
| 47 | + ); |
55 | 48 |
|
56 |
| - $ch = curl_init(); // setup a curl |
57 |
| - curl_setopt($ch, CURLOPT_URL, SELF::AUTH_URL); // set url to send to |
58 |
| - curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // set custom headers |
59 |
| - curl_setopt($ch, CURLOPT_POST, 1); // send as post |
60 |
| - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return output |
61 |
| - curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials"); // post body/fields to be sent |
62 |
| - $header = curl_setopt($ch, CURLOPT_HEADER, 1); // send custom headers |
63 |
| - $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
64 |
| - $response = curl_exec ($ch); // execute the curl |
65 |
| - curl_close($ch); // close the curl |
66 |
| - $output = explode("\n", $response);var_dump($output); |
67 |
| - $bearer_token = ''; |
68 |
| - |
69 |
| - foreach ( $output as $line ) { |
70 |
| - if ( $line === false ) { |
71 |
| - // there was no bearer token |
72 |
| - } else { |
73 |
| - $bearer_token = $line; |
74 |
| - } |
75 |
| - } |
76 |
| - |
77 |
| - // decode token |
78 |
| - $bearer_token = json_decode( $bearer_token ); |
79 |
| - |
80 |
| - // set token for reuse |
81 |
| - $this->token = $bearer_token->{'access_token'}; |
| 49 | + $ch = curl_init(); // setup a curl |
| 50 | + curl_setopt($ch, CURLOPT_URL, SELF::AUTH_URL); // set url to send to |
| 51 | + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // set custom headers |
| 52 | + curl_setopt($ch, CURLOPT_POST, 1); // send as post |
| 53 | + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // return output |
| 54 | + curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials"); // post body/fields to be sent |
| 55 | + $header = curl_setopt($ch, CURLOPT_HEADER, 1); // send custom headers |
| 56 | + $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
| 57 | + $response = curl_exec ($ch); // execute the curl |
| 58 | + curl_close($ch); // close the curl |
| 59 | + $output = explode("\n", $response); |
| 60 | + $bearer_token = ''; |
| 61 | + |
| 62 | + foreach ( $output as $line ) { |
| 63 | + if ( $line === false ) { |
| 64 | + // there was no bearer token |
| 65 | + } else { |
| 66 | + $bearer_token = $line; |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // decode token |
| 71 | + $bearer_token = json_decode( $bearer_token ); |
| 72 | + |
| 73 | + // set token for reuse |
| 74 | + $this->token = $bearer_token->{'access_token'}; |
82 | 75 | }
|
83 | 76 |
|
84 | 77 | /**
|
|
0 commit comments