【问题标题】:Request a User’s Email Address in Twitter App with PHP使用 PHP 在 Twitter 应用程序中请求用户的电子邮件地址
【发布时间】:2015-12-28 03:06:06
【问题描述】:

我正在使用 PHP Twitter 登录系统,并且我已请求将我的 Twitter 应用程序列入 Twitter 白名单,以便当用户在我的网站上注册 Twitter 时,我可以收到来自用户的电子邮件。

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

有twitter-login.php页面的代码:

<?php

require('http.php');
require('oauth_client.php');
require('config.php');

//require('config.php');

$client = new oauth_client_class;
$client->debug = 1;
$client->debug_http = 1;
$client->redirect_uri = REDIRECT_URL;
//$client->redirect_uri = 'oob';

$client->client_id = CLIENT_ID;
$application_line = __LINE__;
$client->client_secret = SECRET_KEY;

if (strlen($client->client_id) == 0 || strlen($client->client_secret) == 0)
  die('Please go to Twitter Apps page https://dev.twitter.com/apps/new , ' .
          'create an application, and in the line ' . $application_line .
          ' set the client_id to Consumer key and client_secret with Consumer secret. ' .
          'The Callback URL must be ' . $client->redirect_uri . ' If you want to post to ' .
          'the user timeline, make sure the application you create has write permissions');

if (($success = $client->Initialize())) {
  if (($success = $client->Process())) {
    if (strlen($client->access_token)) {
      $success = $client->CallAPI(
              'https://api.twitter.com/1.1/account/verify_credentials.json', 'GET', array(), array('FailOnAccessError' => true), $user);
    }
  }
  $success = $client->Finalize($success);
}
if ($client->exit)
  exit;
if ($success) {
  // Now check if user exist with same email ID
  $sql = "SELECT COUNT(*) AS count from users_twitter where twitter_id = :id";
  try {
    $stmt = $DB->prepare($sql);
    $stmt->bindValue(":id", $user->id);
    $stmt->execute();
    $result = $stmt->fetchAll();

    if ($result[0]["count"] > 0) {
      // User Exist 

      $_SESSION["name"] = $user->name;
      $_SESSION["id"] = $user->id;
      $_SESSION["new_user"] = "no";
    } else {
      // New user, Insert in database
      $sql = "INSERT INTO `users_twitter` (`name`, `twitter_id`) VALUES " . "( :name, :id)";
      $stmt = $DB->prepare($sql);
      $stmt->bindValue(":name", $user->name);
      $stmt->bindValue(":id", $user->id);
      $stmt->execute();
      $result = $stmt->rowCount();
      if ($result > 0) {
        $_SESSION["name"] = $user->name;
        $_SESSION["id"] = $user->id;
        $_SESSION["new_user"] = "yes";
        $_SESSION["e_msg"] = "";
      }
    }
  } catch (Exception $ex) {
    $_SESSION["e_msg"] = $ex->getMessage();
  }

  $_SESSION["user_id"] = $user->id;
} else {
  $_SESSION["e_msg"] = $client->error;
}
header("location:home.php");
exit;
?>

我尝试过 id 被称为:$user-&gt;id; => $user-&gt;email;,但没有成功。有人对此有解决方案吗?

【问题讨论】:

  • 你有没有罚款解决方案。我已将 Twitter 开发者控制台中的电子邮件地址列入白名单,但未收到用户电子邮件。

标签: php email twitter login


【解决方案1】:

如果应用程序是whitelisted,则可以使用电子邮件地址

【讨论】:

  • 如果应用程序是whitelisted,则电子邮件地址可用。
  • 谢谢,我忘了。
  • 你知道是否有一种方法可以同时表示获取的电子邮件地址已确认? (比如点击 Twitter 发送到用户电子邮件地址的确认链接)
猜你喜欢
  • 1970-01-01
  • 2016-02-20
  • 2016-05-24
  • 2012-05-10
  • 2012-12-09
  • 1970-01-01
  • 1970-01-01
  • 2021-01-05
  • 1970-01-01
相关资源
最近更新 更多