vojant/twitter-php

Twitter REST API v1.1 的简单 PHP 封装。支持仅应用程序认证

dev-master 2014-03-16 02:04 UTC

This package is not auto-updated.

Last update: 2024-09-23 16:16:40 UTC


README

MIT 许可证 (MIT)
版权所有 (c) 2013 Albert Kozłowski

  1. 关于

Twitter-PHP 是 Twitter 1.1 REST API 的简单 PHP 封装。
它支持 仅应用程序认证单个用户 OAuth

要开始,只需在 Twitter 开发者网站 上创建应用程序并遵循以下说明。

这非常 简单,看看吧

$twitter = new \TwitterPhp\RestApi($consumerKey,$consumerSecret,$accessToken,$accessTokenSecret);
$connectionUser = $twitter->connectAsUser();
$connectionUser->post('statuses/update',array('status' => 'Hello World!'));
  1. 安装
  • 将 RestApi.php 包含到您的应用程序中

      require_once 'Twitter-php/RestApi.php';
    
  • 或者通过 composer 安装

       {
        "require": {
          "vojant/twitter-php": "dev-master"
        }
       }
    
  1. 使用方法
<?php

require_once 'Twitter-php/RestApi.php';

$consumerKey = 'YOUR CONSUMER KEY';
$consumerSecret = 'YOUR CONSUMER SECRET';
$accessToken = 'YOUR ACCESS TOKEN';
$accessTokenSecret = 'YOUR ACCESS TOKEN SECRET';

$twitter = new \TwitterPhp\RestApi($consumerKey,$consumerSecret,$accessToken,$accessTokenSecret);

以应用程序身份登录

$connection = $twitter->connectAsApplication();

调用 API

$data = $connection->get(RESOURCE,PARAMETERS)

以用户身份登录

$connectionUser = $twitter->connectAsUser();

调用 API / GET 和 POST

$data = $connection->get(RESOURCE,PARAMETERS);
$data = $connection->post(RESOURCE,PARAMETERS)
  1. 示例
<?php

include 'RestApi.php';

/*
 * Config
 */
$consumerKey = 'YOUR CONSUMER KEY';
$consumerSecret = 'YOUR CONSUMER SECRET';
$accessToken = 'YOUR ACCESS TOKEN';
$accessTokenSecret = 'YOUR ACCESS TOKEN SECRET';


/*
 * Create new RestApi instance
 * Consumer key and Consumer secret are required
 * Access Token and Access Token secret are required to use api as a user
 */
$twitter = new \TwitterPhp\RestApi($consumerKey,$consumerSecret,$accessToken,$accessTokenSecret);

/*
 * Connect as application
 * https://dev.twitter.com/docs/auth/application-only-auth
 */
$connection = $twitter->connectAsApplication();

/*
 * Collection of the most recent Tweets posted by the user indicated by the screen_name, without replies
 * https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline
 */
$data = $connection->get('/statuses/user_timeline',array('screen_name' => 'TechCrunch', 'exclude_replies' => 'true'));

/*
 * Connect as User
 * Access Token and Access Token Secret are required!
 * https://dev.twitter.com/docs/auth/oauth/single-user-with-examples
 */
$connectionUser = $twitter->connectAsUser();

/*
 * Tweet hello world to your timeline
 * https://dev.twitter.com/docs/api/1.1/post/statuses/update
 */
$connectionUser->post('statuses/update',array('status' => 'Hello World!'));

/*
 * Collection of the 5 most recent Tweets and retweets posted by the authenticating user and the users they follow
 * https://dev.twitter.com/docs/api/1.1/get/statuses/home_timeline
 */
$tweets = $connectionUser->get('statuses/home_timeline',array('limit' => 5));
  1. 文档
  1. 许可

MIT 许可证 (MIT)

版权所有 (c) 2013 Albert Kozłowski

在此条件下,任何人未经费用获得此软件及其相关文档(“软件”)的副本,均可自由处理该软件,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本,并允许向软件提供者提供软件的人这样做,前提是以下条件

上述版权声明和本许可声明应包含在所有副本或实质性部分中。

软件按“原样”提供,不提供任何明示或暗示的保证,包括但不限于适销性、适用于特定用途和非侵权性保证。在任何情况下,作者或版权所有者均不对任何索赔、损害或其他责任(无论基于合同、侵权或其他原因)负责,无论该索赔、损害或其他责任是否源于、因或与软件或其使用或其他方式有关。