問題描述
在白名單 PHP 之後獲取用戶電子郵件 (Get user email afer whitelist PHP)
我正在使用 PHP Twitter 登錄系統 & 我已請求將我的 Twitter 應用程序列入 Twitter 白名單,以便當用戶在我的網站上註冊 Twitter 時,我可以收到他的電子郵件。
我的應用程序已成功列入白名單,但我沒有找到任何教程如何在 PHP 代碼中獲取電子郵件。
index.php 代碼的一部分://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;
我可以看到名稱、日期等,但是我無法使用用戶電子郵件,幫助
參考解法
方法 1:
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');
Hope it helps!
(by MW Jose Miguel Chacon、JP. Aulet)