MW  Jose Miguel Chacon MW Jose Miguel Chacon - 4 months ago 17
PHP Question

Get user email afer whitelist PHP

I am working with a PHP Twitter Login system & I have requested my Twitter application to be on a Twitter whitelist, so that I can get email from a user when he register with Twitter on my website.

My application was successfully whitelisted, but I didn't find any tutorial how to get email in PHP code.

There is a part of the index.php code:
//Variables del usuario

$tw_name = $_SESSION['request_vars']['screen_name'];
$tw_id = $_SESSION['request_vars']['user_id'];
$oauth_key = $_SESSION['request_vars']['oauth_token'];
$oauth_secret_key = $_SESSION['request_vars']['oauth_token_secret'];

$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $oauth_key, $oauth_secret_key);
$my_data = $connection->get('users/show', array('screen_name' => $tw_name, 'user_id'=> $tw_id));
$my_tweets = $connection->get('statuses/user_timeline', array('screen_name' => $screen_name, 'count' => 10));
$my_email = $connection->get('account/verify_credentials', array('screen_name' => $tw_name, 'user_id'=> $tw_id,'include_email'=>true));

echo '<strong>Name:</strong> '.$my_data->name.
'<br><strong>Date: </strong> ' .$my_data->created_at.
'<br><strong>Description:</strong> ' .$my_data->description.
'<br><strong>Location:</strong> ' .$my_data->location.
'</br><strong>Lenguage:</strong> '.$my_data->lang.'<br>';
echo $my_email->email;


I can see name, date, etc but i can't with the user email, help

Answer

What's the content of dumping $my_email? It could be a error or a described behaviour, in the twitter docs:

"When set to true email will be returned in the user objects as a string. If the user does not have an email address on their account, or if the email address is un-verified, null will be returned."

It could be the case?

If no, you could try doing this way:

$my_email = $connection->get("account/verify_credentials", ['include_entities' => true, 'skip_status' => true, 'include_email' => true]);

or simply:

get("account/verify_credentials", ['include_email' => true]);

With pure booleans instead of strings, found here: https://github.com/abraham/twitteroauth/issues/364 and reading the official documention: https://dev.twitter.com/rest/reference/get/account/verify_credentials it makes sense.

If this doesn't work (that it should), in some others places, they use only:

$data = $connection->get('account/verify_credentials');

(http://code.runnable.com/UnUm0Nx6Lu1MAAAY/how-to-use-twitter-oauth-in-php-web-application-for-twitteroauth)

Hope it helps!