waleedahmad/pinterest-laravel
Composer 安装命令:
composer require waleedahmad/pinterest-laravel
包简介
Laravel Package for the official Pinterest API
关键字:
README 文档
README
A Package for using official Pinterest API with Laravel.
Requirements
- PHP 5.4 or higher
- Laravel 5.0 or Above
- cURL
- Registered Pinterest App
Get started
To use the Pinterest API you have to register yourself as a developer and create an application. After you've created your app you will receive a app_id and app_secret.
The terms
client_idandclient_secretare in this caseapp_idandapp_secret.
Installation
The Pinterest API wrapper is available on Composer.
composer require waleedahmad/pinterest-laravel
Configuration
Add service provider for $providers[] array in config/app.php array.
'providers' => [ [...] // other service providers \WaleedAhmad\Pinterest\ServiceProviders\PinterestServiceProvider::class, ]
Run vendor:publish command to copy pinterest configuration to app configs directory.
php artisan vendor:publish --provider="WaleedAhmad\Pinterest\ServiceProviders\PinterestServiceProvider"
Update .env file and fill in these env variables.
PINTEREST_KEY=YOUR_APP_KEY
PINTEREST_SECRET=YOUR_APP_SECRET
PINTEREST_REDIRECT_URI=YOUR_CALLBACK_URL
Getting access token in exchange for code
After you have initialized the class you can get a login URL:
$loginurl = Pinterest::auth()->getLoginUrl(CALLBACK_URL, array('read_public')); echo '<a href=' . $loginurl . '>Authorize Pinterest</a>';
Check the Pinterest documentation for the available scopes.
After your user has used the login link to authorize he will be send back to the given CALLBACK_URL. The URL will contain the code which can be exchanged into an access_token. To exchange the code for an access_token and set it you can use the following code:
if(isset($_GET["code"])){ $token = Pinterest::auth()->getOAuthToken($_GET["code"]); Pinterest::auth()->setOAuthToken($token->access_token); }
Get the user's profile
To get the profile of the current logged in user you can use the Users::me(<array>); method.
$me = Pinterest::user()->me(); echo $me;
Models
The API wrapper will parse all data through it's corresponding model. This results in the possibility to (for example) directly echo your model into a JSON string.
Models also show the available fields (which are also described in the Pinterest documentation). By default, not all fields are returned, so this can help you when providing extra fields to the request.
Available models
User
Pin
Board
Interest
- id
- name
Retrieving extra fields
If you want more fields you can specify these in the $data (GET requests) or $fields (PATCH requests) array. Example:
Pinterest::user()->me();
Response:
{
"id": "503066358284560467",
"username": null,
"first_name": "Waleed",
"last_name": "Ahmad",
"bio": null,
"created_at": null,
"counts": null,
"image": null
}
By default, not all fields are returned. The returned data from the API has been parsed into the User model. Every field in this model can be filled by parsing an extra $data array with the key fields. Say we want the user's username, first_name, last_name and image (small and large):
Pinterest::user()->me(array( 'fields' => 'username,first_name,last_name,image[small,large]' ));
The response will now be:
{
"id": "503066358284560467",
"username": "waleedahmad",
"first_name": "Waleed",
"last_name": "Ahmad",
"bio": null,
"created_at": null,
"counts": null,
"image": {
"small": {
"url": "http://media-cache-ak0.pinimg.com/avatars/waleedahmad_1438089829_30.jpg",
"width": 30,
"height": 30
},
"large": {
"url": "http://media-cache-ak0.pinimg.com/avatars/waleedahmad_1438089829_280.jpg",
"width": 280,
"height": 280
}
}
}
}
Collection
When the API returns multiple models (for instance when your requesting the pins from a board) the wrapper will put those into a Collection.
The output of a collection contains the data and page key. If you echo the collection you will see a json encoded output containing both of these. Using the collection as an array will only return the items from data.
Available methods for the collection class:
Get all items
all()
$pins = Pinterest::user()->getMePins(); $pins->all();
Returns: array<Model>
Get item at index
get( int $index )
$pins = Pinterest::user()->getMePins(); $pins->get(0);
Returns: Model
Check if collection has next page
hasNextPage()
$pins = Pinterest::user()->getMePins(); $pins->hasNextPage();
Returns: Boolean
Available methods
Every method containing a
dataarray can be filled with extra data. This can be for example extra fields or pagination.
Authentication
The methods below are available through Pinterest::auth.
Get login URL
getLoginUrl(string $redirect_uri, array $scopes, string $response_type = "code");
Pinterest::auth()->getLoginUrl("https://pinterest.dev/callback.php", array("read_public"));
Check the Pinterest documentation for the available scopes.
Note: since 0.2.0 the default authentication method has changed to code instead of token. This means you have to exchange the returned code for an access_token.
Get access_token
getOAuthToken( string $code );
Pinterest::auth()->getOAuthToken($code);
Set access_token
setOAuthToken( string $access_token );
Pinterest::auth()->setOAuthToken($access_token);
Get state
getState();
Pinterest::auth()->getState();
Returns: string
Set state
setState( string $state );
This method can be used to set a state manually, but this isn't required since the API will automatically generate a random state on initialize.
Pinterest::auth()->setState($state);
Rate limit
Get limit
getRateLimit();
This method can be used to get the maximum number of requests.
Pinterest::getRateLimit();
Returns: int
Get remaining
getRateLimitRemaining();
This method can be used to get the remaining number of calls.
Pinterest::getRateLimitRemaining();
Returns: int
Users
The methods below are available through Pinterest::users.
You also cannot access a user’s boards or Pins who has not authorized your app.
Get logged in user
me( array $data );
Pinterest::user()->me();
Returns: User
Find a user
find( string $username_or_id );
Pinterest::user()->find('waleedahmad');
Returns: User
Get user's pins
getMePins( array $data );
Pinterest::user()->getMePins();
Returns: Collection<Pin>
Search in user's pins
getMePins( string $query, array $data );
Pinterest::user()->searchMePins("cats");
Returns: Collection<Pin>
Search in user's boards
searchMeBoards( string $query, array $data );
Pinterest::user()->searchMeBoards("cats");
Returns: Collection<Board>
Get user's boards
getMeBoards( array $data );
Pinterest::user()->getMeBoards();
Returns: Collection<Board>
Get user's followers
getMeFollowers( array $data );
Pinterest::user()->getMeFollowers();
Returns: Collection<Pin>
Boards
The methods below are available through Pinterest::boards.
Get board
get( string $board_id, array $data );
Pinterest::boards()->get("waleedahmad/pinterest-laravel");
Returns: Board
Create board
create( array $data );
Pinterest::boards()->create(array( "name" => "Test board from API", "description" => "Test Board From API Test" ));
Returns: Board
Edit board
edit( string $board_id, array $data, string $fields = null );
Pinterest::boards-edit("waleedahmad/pinterest-laravel", array( "name" => "Test board after edit" ));
Returns: Board
Delete board
delete( string $board_id, array $data );
Pinterest::boards()->delete("waleedahmad/pinterest-laravel");
Returns: True|PinterestException
Pins
The methods below are available through Pinterest::pins.
Get pin
get( string $pin_id, array $data );
Pinterest::pins()->get("181692166190246650");
Returns: Pin
Get pins from board
fromBoard( string $board_id, array $data );
Pinterest::pins()->fromBoard("waleedahmad/pinterest-laravel");
Returns: Collection<Pin>
Create pin
create( array $data );
Creating a pin with an image hosted somewhere else:
Pinterest::pins()->create(array( "note" => "Test board from API", "image_url" => "https://download.unsplash.com/photo-1438216983993-cdcd7dea84ce", "board" => "waleedahmad/pinterest-laravel" ));
Creating a pin with an image located on the server:
Pinterest::pins()->create(array( "note" => "Test board from API", "image" => "/path/to/image.png", "board" => "waleedahmad/pinterest-laravel" ));
Creating a pin with a base64 encoded image:
Pinterest::pins()->create(array( "note" => "Test board from API", "image_base64" => "[base64 encoded image]", "board" => "waleedahmad/pinterest-laravel" ));
Returns: Pin
Edit pin
edit( string $pin_id, array $data, string $fields = null );
Pinterest::pins()->edit("181692166190246650", array( "note" => "Updated name" ));
Returns: Pin
Delete pin
delete( string $pin_id, array $data );
Pinterest::pins()->delete("181692166190246650");
Returns: True|PinterestException
Following
The methods below are available through Pinterest::following.
Following users
users( array $data );
Pinterest::following()->users();
Returns: Collection<User>
Following boards
boards( array $data );
Pinterest::following()->boards();
Returns: Collection<Board>
Following interests/categories
interests( array $data );
Pinterest::following()->interests();
Returns: Collection<Interest>
Follow an user
followUser( string $username_or_id );
Pinterest::following()->followUser("waleedahmad");
Returns: True|PinterestException
Unfollow an user
unfollowUser( string $username_or_id );
Pinterest::following()->unfollowUser("waleedahmad");
Returns: True|PinterestException
Follow a board
followBoard( string $board_id );
Pinterest::following()->followBoard("503066289565421201");
Returns: True|PinterestException
Unfollow a board
unfollowBoard( string $board_id );
Pinterest::following()->unfollowBoard("503066289565421201");
Returns: True|PinterestException
Follow an interest
According to the Pinterest documentation this endpoint exists, but for some reason their API is returning an error at the moment.
followInterest( string $interest );
Pinterest::following()->followInterest("architecten-911112299766");
Returns: True|PinterestException
Unfollow an interest
According to the Pinterest documentation this endpoint exists, but for some reason their API is returning an error at the moment.
unfollowInterest( string $interest );
Pinterest::following()->unfollowInterest("architecten-911112299766");
Returns: True|PinterestException
waleedahmad/pinterest-laravel 适用场景与选型建议
waleedahmad/pinterest-laravel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8.64k 次下载、GitHub Stars 达 10, 最近一次更新时间为 2018 年 05 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「api」 「laravel」 「pinterest」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 waleedahmad/pinterest-laravel 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 waleedahmad/pinterest-laravel 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 waleedahmad/pinterest-laravel 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Library to validate and parse social media profile URLs
This Plugin enables you to display RSS.App feeds (Instagram, Twitter, Facebook and more) in TYPO3 - more information on https://rss.app
A PSR-7 compatible library for making CRUD API endpoints
Laravel social media auto post package - Complete solution for posting to 8 major social media platforms
Pinterest extension for using via yii2-authclient
PHP Social Counter Plugin grabs the latest counts of your Fans/Followers etc. from your Favorite Social Networks and then displays them on your Blog. It is the perfect solution to encourage more users to join your network
统计信息
- 总下载量: 8.64k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 10
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2018-05-22