简体   繁体   English

使用 PHP 在 Twitter 应用程序中请求用户的电子邮件地址

[英]Request a User’s Email Address in Twitter App with 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.我正在使用 PHP Twitter 登录系统,并且我已请求将我的 Twitter 应用程序列入 Twitter 白名单,以便在用户在我的网站上注册 Twitter 时收到来自用户的电子邮件。

My application was successfully whitelisted, but I didn't find any tutorial how to get email in PHP code.我的应用程序已成功列入白名单,但我没有找到任何如何在 PHP 代码中获取电子邮件的教程。

There is the code of the twitter-login.php page:有 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;
?>

I have tried like id is called: $user->id;我试过像 id 一样被称为: $user->id; => $user->email; => $user->email; , but it didn't work. ,但它没有用。 Does anybody have a solution for this?有人对此有解决方案吗?

如果应用程序已被列入白名单,则电子邮件地址可用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将用户的电子邮件地址添加到Woocommerce电子邮件模板#php? - How to add the user's email address to a Woocommerce email template #php? Twitter api 没有返回带有用户信息数组的电子邮件地址 - Twitter api is not returning email address with user info array PHP,验证用户是否输入了电子邮件域,而不是地址 - PHP, Validate if user entered an email domain, NOT address PHP如何将URL重定向到电子邮件地址并启动用户的邮件客户端 - PHP how to redirect url to email address and initiate user's mail client 发送PHP邮件时,如何在用户的电子邮件地址前显示其名称? - How do I display the user's name before the their email address when sending PHP mail? php的mail()覆盖了“发件人”电子邮件地址 - php's mail() is overriding the 'from' email address 将PHP上的电子邮件结果发送到用户输入的电子邮件地址 - Email results on a PHP to user-input email address 使用PHP中的用户IP地址的cURL请求 - cURL request with user IP address in PHP 在PHP电子邮件表单的标题中使用发件人的姓名和电子邮件地址 - Using a sender's name and email address in the header of a PHP email form 您如何获取Google reCAPTCHA请求的用户IP地址? PHP Laravel 5 - How do you obtain the user's ip address for the google reCAPTCHA request? PHP Laravel 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM