1+ <?php
2+ /**
3+ ** TwitterAppAuth.py
4+ *
5+ * Created by Jon Hurlock on 2013-03-20.
6+ *
7+ * Jon Hurlock's Twitter Application-only Authentication App by Jon Hurlock (@jonhurlock)
8+ * is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
9+ * Permissions beyond the scope of this license may be available at http://www.jonhurlock.com/.
10+ */
11+
12+
13+ // fill in your consumer key and consumer secret below
14+ define ('CONSUMER_KEY ' , 'enter_your_consumer_key_here ' );
15+ define ('CONSUMER_SECRET ' , 'enter_your_consumer_secret_here ' );
16+
17+ /**
18+ * Get the Bearer Token, this is an implementation of steps 1&2
19+ * from https://dev.twitter.com/docs/auth/application-only-auth
20+ */
21+ function get_bearer_token (){
22+ // Step 1
23+ // step 1.1 - url encode the consumer_key and consumer_secret in accordance with RFC 1738
24+ $ encoded_consumer_key = urlencode (CONSUMER_KEY );
25+ $ encoded_consumer_secret = urlencode (CONSUMER_SECRET );
26+ // step 1.2 - concatinate encoded consumer, a colon character and the encoded consumer secret
27+ $ bearer_token = $ encoded_consumer_key .': ' .$ encoded_consumer_secret ;
28+ // step 1.3 - base64-encode bearer token
29+ $ base64_encoded_bearer_token = base64_encode ($ bearer_token );
30+ // step 2
31+ $ url = "https://api.twitter.com/oauth2/token " ; // url to send data to for authentication
32+ $ headers = array (
33+ "POST /oauth2/token HTTP/1.1 " ,
34+ "Host: api.twitter.com " ,
35+ "User-Agent: jonhurlock Twitter Application-only OAuth App v.1 " ,
36+ //"Authorization: Basic ".$base64_encoded_bearer_token."",
37+ "Authorization: Basic " .$ base64_encoded_bearer_token ."" ,
38+ "Content-Type: application/x-www-form-urlencoded;charset=UTF-8 " ,
39+ "Content-Length: 29 "
40+ );
41+
42+ $ ch = curl_init (); // setup a curl
43+ curl_setopt ($ ch , CURLOPT_URL ,$ url ); // set url to send to
44+ curl_setopt ($ ch , CURLOPT_HTTPHEADER , $ headers ); // set custom headers
45+ curl_setopt ($ ch , CURLOPT_POST , 1 ); // send as post
46+ curl_setopt ($ ch , CURLOPT_POSTFIELDS , "grant_type=client_credentials " ); // post body/fields to be sent
47+ $ header = curl_setopt ($ ch , CURLOPT_HEADER , 1 ); // send custom headers
48+ $ httpcode = curl_getinfo ($ ch , CURLINFO_HTTP_CODE );
49+ ob_start (); // start ouput buffering
50+ curl_exec ($ ch ); // execute the curl
51+ $ retrievedhtml = ob_get_contents (); // grab the retreived html
52+ ob_end_clean (); //End buffering and clean output
53+ curl_close ($ ch ); // close the curl
54+ $ output = explode ("\n" , $ retrievedhtml );
55+ $ bearer_token = '' ;
56+
57+ foreach ($ output as $ line )
58+ {
59+ if ($ pos === false )
60+ {
61+ }else {
62+ $ bearer_token = $ line ;
63+ }
64+ }
65+
66+ $ bearer_token = json_decode ($ bearer_token );
67+ return $ bearer_token ->{'access_token ' };
68+ }
69+
70+ /**
71+ * Search
72+ * Basic Search of the Search API
73+ */
74+ function search_for_a_term ($ query ){
75+ $ url = "https://api.twitter.com/1.1/search/tweets.json " ; // base url
76+ $ q = $ query ; // query term
77+
78+ $ formed_url ='?q= ' .$ q ; // fully formed url
79+ $ headers = array (
80+ "GET /1.1/search/tweets.json " .$ formed_url ." HTTP/1.1 " ,
81+ "Host: api.twitter.com " ,
82+ "User-Agent: jonhurlock Twitter Application-only OAuth App v.1 " ,
83+ "Authorization: Bearer " .get_bearer_token ()."" ,
84+ );
85+ $ ch = curl_init (); // setup a curl
86+ curl_setopt ($ ch , CURLOPT_URL ,$ url .$ formed_url ); // set url to send to
87+ curl_setopt ($ ch , CURLOPT_HTTPHEADER , $ headers ); // set custom headers
88+ ob_start (); // start ouput buffering
89+ $ output = curl_exec ($ ch ); // execute the curl
90+ $ retrievedhtml = ob_get_contents (); // grab the retreived html
91+ ob_end_clean (); //End buffering and clean output
92+ curl_close ($ ch ); // close the curl
93+ return $ retrievedhtml ;
94+ }
95+
96+ // lets run a search.
97+ print search_for_a_term ("test " );
98+ ?>
0 commit comments