php - Twitter OAuth (PHP) : Need good, 入门基本示例

标签 php html twitter twitter-oauth

使用 Facebook 的 PHP SDK,我能够在我的网站上快速登录 Facebook。他们只需设置一个 $user 变量,该变量可以很容易地访问。

我没有那么幸运尝试让 Twitter 的 OAuth 登录工作......坦率地说,他们的 github Material 对于 PHP 和网页设计相对较新的人来说是令人困惑和无用的,更不用说许多非官方的例子了我尝试过的工作同样令人困惑或已过时。

我真的需要一些帮助才能让 Twitter 登录正常工作——我的意思只是一个基本示例,我单击登录按钮,授权我的应用程序,然后它重定向到显示登录用户名称的页面。

非常感谢您的帮助。

编辑我知道abraham's twitter oauth的存在但它几乎没有提供任何指示来让他的东西正常工作。

最佳答案

这是获取授权 url 然后在你从 Twitter 返回时获取用户基本信息的基本示例

<?php
session_start();
//add autoload note:do check your file paths in autoload.php
require "ret/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;
//this code will run when returned from twiter after authentication
if(isset($_SESSION['oauth_token'])){
  $oauth_token=$_SESSION['oauth_token'];unset($_SESSION['oauth_token']);
  $consumer_key = 'your consumer key';
  $consumer_secret = 'your secret key';
  $connection = new TwitterOAuth($consumer_key, $consumer_secret);
 //necessary to get access token other wise u will not have permision to get user info
  $params=array("oauth_verifier" => $_GET['oauth_verifier'],"oauth_token"=>$_GET['oauth_token']);
  $access_token = $connection->oauth("oauth/access_token", $params);
  //now again create new instance using updated return oauth_token and oauth_token_secret because old one expired if u dont u this u will also get token expired error
  $connection = new TwitterOAuth($consumer_key, $consumer_secret,
  $access_token['oauth_token'],$access_token['oauth_token_secret']);
  $content = $connection->get("account/verify_credentials");
  print_r($content);
}
else{
  // main startup code
  $consumer_key = 'your consumer key';
  $consumer_secret = 'your secret key';
  //this code will return your valid url which u can use in iframe src to popup or can directly view the page as its happening in this example

  $connection = new TwitterOAuth($consumer_key, $consumer_secret);
  $temporary_credentials = $connection->oauth('oauth/request_token', array("oauth_callback" =>'http://dev.crm.alifca.com/twitter/index.php'));
  $_SESSION['oauth_token']=$temporary_credentials['oauth_token'];       $_SESSION['oauth_token_secret']=$temporary_credentials['oauth_token_secret'];$url = $connection->url("oauth/authorize", array("oauth_token" => $temporary_credentials['oauth_token']));
// REDIRECTING TO THE URL
  header('Location: ' . $url); 
}
?>

关于php - Twitter OAuth (PHP) : Need good, 入门基本示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6550337/

相关文章:

javascript - 更改下拉值时 DIV "hiding"

html - struts <s :form > tag used for? 的 id 属性是什么

php - mysql用户订阅查询失败

php - 覆盖 wordpress 登录重定向功能

php - PDO mysql : How to know if insert was successful

html - FontAwesome 5 - 多色图标

php - 使用 PHP 获取最后一条推文

python - 使用 wget 下载指定大小的 Twitter 图片

windows-phone-7 - Twitter、OAuth、Hammock、TweetSharp 和 Windows Phone 7

php - Magento : Fatal error: Class 'Mage_Giftcards_Helper_Data' not found in . ..../app/Mage.php 第 546 行