定制 codezero/twitter 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

codezero/twitter

Composer 安装命令:

composer require codezero/twitter

包简介

Simple Twitter API wrapper

README 文档

README

Build Status Latest Stable Version Total Downloads License

This package hides away the complexity of "talking" to the Twitter API, but instead offers a few simple functions to execute some basic queries.

Features

  • All queries require an API key and API secret to generate App Credentials
  • Authorization will be triggered automatically behind the scenes
  • Fetch a user's tweets with a single method call
  • Search Twitter for a hash tag or keyword
  • Get the results in standard JSON format, or use our simplified Tweet objects
  • Optional Caching (only Laravel implementation included, see codezero-be/courier)
  • Optional Laravel ServiceProvider included

Installation

Install this package through Composer:

"require": {
    "codezero/twitter": "1.*"
}

Setup

Laravel Setup

After installing, update your Laravel config/app.php file to include a reference to this package's service provider in the providers array:

'providers' => [
    'CodeZero\Twitter\TwitterServiceProvider'
]

Next, publish the package config file and enter your API credentials.

With Laravel 4.*:

php artisan config:publish codezero/twitter

With Laravel 5.*:

php artisan vendor:publish --provider="CodeZero\Twitter\TwitterServiceProvider"

Manual Setup

Specify the location of your config file. An example configuration file is included in the src/config folder. You can put this anywhere you want.

$config = '/path/to/configFile.php';

Create Twitter instance:

use CodeZero\Twitter\Twitter;
$twitter = new Twitter($config);

Edit Configuration

Your configuration file should look like the following:

<?php
return [
    'base_url' => 'https://api.twitter.com/',
    'api_version' => '1.1',
    'api_key' => '',
    'api_secret' => ''
];

Be sure to enter your API key and API secret. Twitter requires this for all requests. Also, do not include the API version in the base_url as this would break the authorization request.

Usage

Set options

The number of results to return. Each Twitter request has a maximum limit. If you specify a $count greater than this limit, the maximum results will be returned. (Default: 10)

$count = 10;

The number of minutes the results of the query should be cached. Twitter sets a request limit per hour, so caching is a good idea. Setting this to 0 (zero) will disable caching. (Default: 30)

$cacheMinutes = 30;

This package includes a Tweet object which greatly simplifies the returned results. If you want the full JSON response to be returned, set this to false. (Default: true)

$returnEntities = true;

Get tweets

try
{
    $username = 'laravelphp'; //=> Example...
    $tweets = $twitter->getTweetsFromUser($username, $count, $cacheMinutes, $returnEntities);
}
catch (\CodeZero\Twitter\TwitterException $e)
{
    $error = $e->getMessage(); //=> user not found etc.
}

Response formats

JSON

If you $returnEntities is false, you get a CodeZero\Courier\Response object, which contains the actual JSON response.

$tweets = $twitter->getTweetsFromUser($username);

echo $tweets; //=> Print the JSON
$json = $tweets->getBody(); //=> Returns the JSON
$array = $tweets->toArray(); //=> Convert JSON to an array

For more information on this Response object, refer to codezero-be/courier.

Tweets

If you $returnEntities is true, you get an array of CodeZero\Twitter\Entities\Tweet objects. This is a very simplified Tweet object, which only contains the most useful info about the tweet and its user.

$tweets = $twitter->getTweetsFromUser($username);
foreach ($tweets as $tweet)
{
    $user = $tweet->user();
    $tweetOwner = $user->getName();
    $tweetUsername = $user->getUsername();
    $tweetText = $tweet->getText();
    $tweetDate = $tweet->getCreatedAt();
}

For an overview of all available Tweet and User information, take a look at the source.

Available Requests

Get tweets from a user

$username = 'laravelphp';
$tweets = $twitter->getTweetsFromUser($username, $count, $cacheMinutes, $returnEntities);

Search for tweets with a hash tag or keyword

$query = '#laravel';
$tweets = $twitter->searchTweets($query, $count, $cacheMinutes, $returnEntities);

That's all for now...

Analytics

codezero/twitter 适用场景与选型建议

codezero/twitter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 40.02k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2014 年 08 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「api」 「twitter」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 codezero/twitter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 codezero/twitter 我们能提供哪些服务?
定制开发 / 二次开发

基于 codezero/twitter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 40.02k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 4
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 2
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-08-29