Skip to content

Commit a976530

Browse files
committed
Moved authentication dependencies out of construct & into authenticate()
Removed debugging output
1 parent 3f5a398 commit a976530

File tree

2 files changed

+50
-60
lines changed

2 files changed

+50
-60
lines changed

TwitterSearch.php

Lines changed: 48 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* Oauth.php
45
*
@@ -10,75 +11,67 @@
1011
* Permissions beyond the scope of this license may be available at http://www.jonhurlock.com/.
1112
*/
1213

13-
class twitterSearch
14+
class TwitterSearch
1415
{
1516
// url to send data to for authentication
1617
const AUTH_URL = 'https://api.twitter.com/oauth2/token';
1718
const SEARCH_URL = 'https://api.twitter.com/1.1/search/tweets.json';
1819
CONST INVALIDATE_AUTH_URL = 'https://api.twitter.com/oauth2/invalidate_token';
1920

2021
/**
21-
* @brief Set app authentication data
22+
* @brief Authenticate a session with Twitter using OAuth
2223
* @param string $consumerKey Your personal key from Twitter
2324
* @param string $consumerSecret Your personal secret from Twitter
25+
* @note https://dev.twitter.com/docs/auth/application-only-auth
2426
*/
25-
public function __construct( $consumerKey, $consumerSecret )
27+
function authenticate( $consumerKey, $consumerSecret )
2628
{
27-
// fill in your consumer key and consumer secret below
2829
$this->consumerKey = $consumerKey;
2930
$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+
);
5548

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'};
8275
}
8376

8477
/**

TwitterSearchTest.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,15 @@
44

55
class TestTwitter extends \PHPUnit_Framework_TestCase
66
{
7-
/**
8-
*
9-
*/
107
public function setUp()
118
{
12-
$this->twitter = new twitterSearch( 'consumer-key', 'consumer-secret' );
9+
$this->twitter = new twitterSearch();
1310
}
1411

1512
public function testSearch()
1613
{
1714
// authenticate using OAuth creds
18-
$this->twitter->authenticate();
15+
$this->twitter->authenticate( 'xlN7UWnd2FLWp4oxrtAA', 'nywpCALJ3ZXnlHnPwdGRmnu0jIqAbUBHhauPRCuPzMM' );
1916

2017
// search for the word 'test'
2118
$result = $this->twitter->search( "test" );

0 commit comments

Comments
 (0)