· 8 years ago · Jan 23, 2017, 08:40 PM
1<?php
2 $token = 'generoi twitterist tokeni';
3 $token_secret = 'generoi secret tokeni';
4 $consumer_key = 'generoi consumer key';
5 $consumer_secret = 'generoi consume secret';
6
7 $host = 'api.twitter.com';
8 $method = 'GET';
9 $path = '/1.1/statuses/user_timeline.json'; // api call path
10 $content = explode("sun twitter name", file_get_contents("php://input"));
11
12 $query = array( // query parameters
13 'screen_name' => $content[0],
14 'count' => $content[1]
15 );
16
17 $oauth = array(
18 'oauth_consumer_key' => $consumer_key,
19 'oauth_token' => $token,
20 'oauth_nonce' => (string)mt_rand(), // a stronger nonce is recommended
21 'oauth_timestamp' => time(),
22 'oauth_signature_method' => 'HMAC-SHA1',
23 'oauth_version' => '1.0'
24 );
25
26 $oauth = array_map("rawurlencode", $oauth); // must be encoded before sorting
27 $query = array_map("rawurlencode", $query);
28
29 $arr = array_merge($oauth, $query); // combine the values THEN sort
30
31 asort($arr); // secondary sort (value)
32 ksort($arr); // primary sort (key)
33
34 // http_build_query automatically encodes, but our parameters
35 // are already encoded, and must be by this point, so we undo
36 // the encoding step
37 $querystring = urldecode(http_build_query($arr, '', '&'));
38
39 $url = "https://$host$path";
40
41 // mash everything together for the text to hash
42 $base_string = $method."&".rawurlencode($url)."&".rawurlencode($querystring);
43
44 // same with the key
45 $key = rawurlencode($consumer_secret)."&".rawurlencode($token_secret);
46
47 // generate the hash
48 $signature = rawurlencode(base64_encode(hash_hmac('sha1', $base_string, $key, true)));
49
50 // this time we're using a normal GET query, and we're only encoding the query params
51 // (without the oauth params)
52 $url .= "?".http_build_query($query);
53 $url=str_replace("&","&",$url); //Patch by @Frewuill
54
55 $oauth['oauth_signature'] = $signature; // don't want to abandon all that work!
56 ksort($oauth); // probably not necessary, but twitter's demo does it
57
58 // also not necessary, but twitter's demo does this too
59 function add_quotes($str) { return '"'.$str.'"'; }
60 $oauth = array_map("add_quotes", $oauth);
61
62 // this is the full value of the Authorization line
63 $auth = "OAuth " . urldecode(http_build_query($oauth, '', ', '));
64
65 // if you're doing post, you need to skip the GET building above
66 // and instead supply query parameters to CURLOPT_POSTFIELDS
67 $options = array( CURLOPT_HTTPHEADER => array("Authorization: $auth"),
68 //CURLOPT_POSTFIELDS => $postfields,
69 CURLOPT_HEADER => false,
70 CURLOPT_URL => $url,
71 CURLOPT_RETURNTRANSFER => true,
72 CURLOPT_SSL_VERIFYPEER => false);
73
74 // do our business
75 $feed = curl_init();
76 curl_setopt_array($feed, $options);
77 $json = curl_exec($feed);
78 curl_close($feed);
79
80 //$twitter_data = $json;
81 print_r($json);
82?>