通过PHP白名单获取用户电子邮件


Get user email afer whitelist PHP

我使用的是PHP Twitter登录系统&我已经请求将我的推特应用程序列入推特白名单,这样当用户在我的网站上注册推特时,我就可以收到他的电子邮件。

我的应用程序成功地被列入了白名单,但我没有找到任何如何用PHP代码获取电子邮件的教程。

index.php代码有一部分://常用变量

        $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;

我可以看到姓名、日期等,但我无法查看用户电子邮件,帮助

转储$my_email的内容是什么?这可能是一个错误,也可能是推特文档中描述的行为:

"当设置为true时,电子邮件将作为一串如果用户的帐户上没有电子邮件地址,或者,如果电子邮件地址未经验证,则返回null。"

可能是这样吗?

如果没有,你可以尝试这样做:

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

或者简单地说:

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

使用纯布尔值而不是字符串,可在此处找到:https://github.com/abraham/twitteroauth/issues/364阅读官方文件:https://dev.twitter.com/rest/reference/get/account/verify_credentials这是有道理的。

如果这不起作用(应该),在其他一些地方,他们只使用:

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

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

希望它能有所帮助!