By the Twitter API
Twitter has long closed down access to information and even limited requests to the developer community. You can verify that in the REST APIs of the 1.1 version (which is the current one) authentication is required to make any request, including the one you tried to do, to GET users/show
.
I have a post detailed on my website about it. But basically the steps to reach your goal through the Application-only authentication method are these:
-
Create an application to get API key and Secret API ;
- Through the keys, get the bearer token ;
- token , perform the requested 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 you then have token on hand, you can make your request for 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
, which you can manipulate it the way you want to get the user information.