aislandener / pinterest-laravel
Laravel官方Pinterest API的包
Requires
- php: >=5.4
- ext-curl: *
Requires (Dev)
- phpunit/phpunit: 4.7.*
- vlucas/phpdotenv: ^2.2
This package is auto-updated.
Last update: 2022-05-17 03:35:32 UTC
README
一个用于Laravel中调用官方Pinterest API的包。
要求
- PHP 5.4或更高版本
- Laravel 5.0或更高版本
- cURL
- 注册的Pinterest应用
开始使用
要使用Pinterest API,您必须注册为开发者并创建一个应用。创建您的应用后,您将获得一个app_id
和一个app_secret
。
在这种情况下,术语
client_id
和client_secret
就是app_id
和app_secret
。
安装
Pinterest API包装器可在Composer上找到。
composer require waleedahmad/pinterest-laravel
配置
在config/app.php
数组中添加服务提供者到$providers[]
数组。
'providers' => [ [...] // other service providers \WaleedAhmad\Pinterest\ServiceProviders\PinterestServiceProvider::class, ]
运行vendor:publish
命令,将Pinterest配置复制到应用配置目录。
php artisan vendor:publish --provider="WaleedAhmad\Pinterest\ServiceProviders\PinterestServiceProvider"
更新.env
文件并填写以下env
变量。
PINTEREST_KEY=YOUR_APP_KEY
PINTEREST_SECRET=YOUR_APP_SECRET
PINTEREST_REDIRECT_URI=YOUR_CALLBACK_URL
用代码换取访问令牌
初始化类后,您可以使用以下代码获取登录URL
$loginurl = Pinterest::auth()->getLoginUrl(CALLBACK_URL, array('read_public')); echo '<a href=' . $loginurl . '>Authorize Pinterest</a>';
请参阅Pinterest文档了解可用的作用域。
您的用户使用登录链接授权后,将被重定向回指定的CALLBACK_URL
。URL将包含可以换取access_token
的code
。要使用以下代码将代码换取access_token
并设置它
if(isset($_GET["code"])){ $token = Pinterest::auth()->getOAuthToken($_GET["code"]); Pinterest::auth()->setOAuthToken($token->access_token); }
获取用户的个人资料
要获取当前登录用户的个人资料,您可以使用Users::me(<array>);
方法。
$me = Pinterest::user()->me(); echo $me;
模型
API包装器将通过其相应的模型解析所有数据。这导致可以直接将模型echo
到一个JSON字符串。
模型还显示了可用的字段(这些字段也在Pinterest文档中描述)。默认情况下,不是所有字段都会返回,这可以帮助您在请求中提供额外字段时使用。
可用的模型
用户
Pin
版块
兴趣
- id
- 名称
检索额外字段
如果您想获取更多字段,可以在 GET 请求中的 $data
或 PATCH 请求中的 $fields
数组中指定这些字段。示例
Pinterest::user()->me();
响应
{ "id": "503066358284560467", "username": null, "first_name": "Waleed", "last_name": "Ahmad", "bio": null, "created_at": null, "counts": null, "image": null }
默认情况下,不会返回所有字段。API 返回的数据已被解析到 User
模型中。此模型中的每个字段都可以通过解析具有键 fields
的额外 $data
数组来填充。假设我们想要用户的用户名、first_name、last_name 和图像(小图和大图)
Pinterest::user()->me(array( 'fields' => 'username,first_name,last_name,image[small,large]' ));
现在响应将是
{ "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 } } } }
集合
当 API 返回多个模型时(例如,当您请求版块的图钉时),包装器会将这些放入一个 Collection
中。
集合的输出包含 data
和页面 key
。如果您输出集合,您将看到包含这两个的 json 编码输出。将集合作为数组使用时,只会返回 data
中的项目。
集合类的可用方法
获取所有项目
all()
$pins = Pinterest::user()->getMePins(); $pins->all();
返回: array<Model>
在索引处获取项目
get( int $index )
$pins = Pinterest::user()->getMePins(); $pins->get(0);
返回: Model
检查集合是否有下一页
hasNextPage()
$pins = Pinterest::user()->getMePins(); $pins->hasNextPage();
返回: Boolean
可用方法
每个包含
data
数组的方法都可以用额外数据填充。这可以是例如额外字段或分页。
认证
以下方法通过 Pinterest::auth
提供。
获取登录 URL
getLoginUrl(string $redirect_uri, array $scopes, string $response_type = "code");
Pinterest::auth()->getLoginUrl("https://pinterest.dev/callback.php", array("read_public"));
请参阅Pinterest文档了解可用的作用域。
注意:从 0.2.0 版本开始,默认认证方法已从 token
更改为 code
。这意味着您必须用 access_token 交换返回的代码。
获取 access_token
getOAuthToken( string $code );
Pinterest::auth()->getOAuthToken($code);
设置 access_token
setOAuthToken( string $access_token );
Pinterest::auth()->setOAuthToken($access_token);
获取状态
getState();
Pinterest::auth()->getState();
返回: string
设置状态
setState( string $state );
此方法可以用于手动设置状态,但这是可选的,因为 API 在初始化时会自动生成一个随机的状态。
Pinterest::auth()->setState($state);
速率限制
获取限制
getRateLimit();
此方法可用于获取最大请求数。
Pinterest::getRateLimit();
返回: int
获取剩余次数
getRateLimitRemaining();
此方法可用于获取剩余调用次数。
Pinterest::getRateLimitRemaining();
返回: int
用户
以下方法可通过 Pinterest::users
获取。
您也无法访问未经您应用授权的用户的主板或Pins。
获取已登录用户
me( array $data );
Pinterest::user()->me();
返回: User
查找用户
find( string $username_or_id );
Pinterest::user()->find('waleedahmad');
返回: User
获取用户Pins
getMePins( array $data );
Pinterest::user()->getMePins();
返回: Collection<Pin>
在用户Pins中搜索
getMePins( string $query, array $data );
Pinterest::user()->searchMePins("cats");
返回: Collection<Pin>
在用户主板上搜索
searchMeBoards( string $query, array $data );
Pinterest::user()->searchMeBoards("cats");
返回: Collection<Board>
获取用户主板
getMeBoards( array $data );
Pinterest::user()->getMeBoards();
返回: Collection<Board>
获取用户关注者
getMeFollowers( array $data );
Pinterest::user()->getMeFollowers();
返回: Collection<Pin>
主板
以下方法可通过 Pinterest::boards
获取。
获取主板
get( string $board_id, array $data );
Pinterest::boards()->get("waleedahmad/pinterest-laravel");
返回: Board
创建主板
create( array $data );
Pinterest::boards()->create(array( "name" => "Test board from API", "description" => "Test Board From API Test" ));
返回: Board
编辑主板
edit( string $board_id, array $data, string $fields = null );
Pinterest::boards-edit("waleedahmad/pinterest-laravel", array( "name" => "Test board after edit" ));
返回: Board
删除主板
delete( string $board_id, array $data );
Pinterest::boards()->delete("waleedahmad/pinterest-laravel");
返回: True|PinterestException
Pin
以下方法可通过 Pinterest::pins
获取。
获取Pin
get( string $pin_id, array $data );
Pinterest::pins()->get("181692166190246650");
返回: Pin
从主板获取Pin
fromBoard( string $board_id, array $data );
Pinterest::pins()->fromBoard("waleedahmad/pinterest-laravel");
返回: Collection<Pin>
创建Pin
create( array $data );
使用在其他地方托管的照片创建Pin
Pinterest::pins()->create(array( "note" => "Test board from API", "image_url" => "https://download.unsplash.com/photo-1438216983993-cdcd7dea84ce", "board" => "waleedahmad/pinterest-laravel" ));
使用服务器上的照片创建Pin
Pinterest::pins()->create(array( "note" => "Test board from API", "image" => "/path/to/image.png", "board" => "waleedahmad/pinterest-laravel" ));
使用base64编码的图像创建Pin
Pinterest::pins()->create(array( "note" => "Test board from API", "image_base64" => "[base64 encoded image]", "board" => "waleedahmad/pinterest-laravel" ));
返回: Pin
编辑Pin
edit( string $pin_id, array $data, string $fields = null );
Pinterest::pins()->edit("181692166190246650", array( "note" => "Updated name" ));
返回: Pin
删除Pin
delete( string $pin_id, array $data );
Pinterest::pins()->delete("181692166190246650");
返回: True|PinterestException
关注
以下方法可通过 Pinterest::following
获取。
关注的用户
users( array $data );
Pinterest::following()->users();
返回: Collection<User>
关注的板块
boards( array $data );
Pinterest::following()->boards();
返回: Collection<Board>
关注的兴趣/分类
interests( array $data );
Pinterest::following()->interests();
返回: Collection<Interest>
关注一个用户
followUser( string $username_or_id );
Pinterest::following()->followUser("waleedahmad");
返回: True|PinterestException
取消关注一个用户
unfollowUser( string $username_or_id );
Pinterest::following()->unfollowUser("waleedahmad");
返回: True|PinterestException
关注一个板块
followBoard( string $board_id );
Pinterest::following()->followBoard("503066289565421201");
返回: True|PinterestException
取消关注一个板块
unfollowBoard( string $board_id );
Pinterest::following()->unfollowBoard("503066289565421201");
返回: True|PinterestException
关注一个兴趣
根据Pinterest文档,这个端点存在,但不知何故,他们的API目前返回了一个错误。
followInterest( string $interest );
Pinterest::following()->followInterest("architecten-911112299766");
返回: True|PinterestException
取消关注一个兴趣
根据Pinterest文档,这个端点存在,但不知何故,他们的API目前返回了一个错误。
unfollowInterest( string $interest );
Pinterest::following()->unfollowInterest("architecten-911112299766");
返回: True|PinterestException