Through the Twitter API
It’s been a while since Twitter closed access to information and even limited requests to the developer community. You can check that in REST Apis of the version 1.1 (which is the current) authentication is required to perform any type of request, including for this one you tried to do, the GET users/show
.
I have a post detailed on my website about it. But basically the steps to reach your goal through the method Application-only Authentication are these:
- Create an application to obtain API key and API secret;
- Through the keys, get the bearer token;
- With the token, carry out the desired request;
Getting the bearer token:
$encoded_consumer_key = urlencode(CONSUMER_KEY); // API key
$encoded_consumer_secret = urlencode(CONSUMER_SECRET); // API secret
$bearer_token = $encoded_consumer_key . ':' . $encoded_consumer_secret;
$base64_consumer_key = base64_encode($bearer_token);
$url = "https://api.twitter.com/oauth2/token";
$headers = array(
"POST /oauth2/token HTTP/1.1",
"Host: api.twitter.com",
"User-Agent: Twitter Application-only OAuth App",
"Authorization: Basic " . $base64_consumer_key,
"Content-Type: application/x-www-form-urlencoded;charset=UTF-8",
"Content-Length: 29"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
$header = curl_setopt($ch, CURLOPT_HEADER, 1);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$response = curl_exec ($ch);
curl_close($ch);
$output = explode("\n", $response);
$bearer_token = '';
foreach($output as $line) {
if ($line !== false) {
$bearer_token = $line;
}
}
$bearer_token = json_decode($bearer_token);
$bearer_token = $bearer_token->{'access_token'};
When then you have the token at hand, you can make your request to get user information:
$url = "https://api.twitter.com/1.1/users/show.json";
$formed_url = '?screen_name=cissamagazine';
$headers = array(
"GET /1.1/users/show.json" . $formed_url . " HTTP/1.1",
"Host: api.twitter.com",
"User-Agent: Twitter Application-only OAuth App",
"Authorization: Bearer " . $bearer_token,
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url . $formed_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
In the variable $response
you will have a JSON
, that you can manipulate it the way you want to get user information.
post a piece of code, some test result Voce did...
– Renaro Santos
this was my last attempt $xml = file_get_contents('http://twitter.com/users/show.xml?screen_name=cissamagazine'); if (preg_match('/followers_count>(.*)</', $xml, $array) != 0) { $followers['Count'] = $array[1]; echo $followers['Count'];
– Gabriel Schmidt Cordeiro
@Gabrielschmidtcordeiro add this code in the question and not just in the comments ;)
– GWER
Don’t use the Twitter API instead of scraping the page?
– Luis Cipriani
@Gabrielschmidtcorderly doesn’t that help you? http://www.pinceladasdaweb.com.br/blog/2009/10/19/show-numero-numberof followers/
– GWER
@GWER
– Gabriel Schmidt Cordeiro
@Luiscipriani, I’ll see what I can do. But I think there must be something easier if compared to what I did to get the number of facebook likes
– Gabriel Schmidt Cordeiro