eugenecooper/pinterest-php

PHP用于消费Pinterest API的包装器。

1.1.3 2017-06-17 21:47 UTC

This package is auto-updated.

Last update: 2024-09-05 06:17:44 UTC


README

一个易于使用的Pinterest API包装器。

Pinterest PHP

Build Status Scrutinizer Code Quality Code Coverage Packagist Packagist

安装

通过Composer

$ composer require hansott/pinterest-php

使用方法

身份验证

要使用API,您需要从Pinterest获取一个访问令牌。如果您还没有创建,请创建一个新的Pinterest应用程序。您将获得一个特定的客户端ID和客户端密钥。

在您的PHP应用程序中,创建一个Pinterest\Http\ClientInterface实例(默认为Pinterest\Http\BuzzClient),并使用它来创建一个Pinterest\Authentication实例。

$client = new Pinterest\Http\BuzzClient();
$auth = new Pinterest\Authentication($client, $clientId, $clientSecret);

$clientId$clientSecret变量替换为您的Pinterest应用程序的数据。

现在,您可以允许用户通过将他们重定向到通过调用$auth->getAuthenticationUrl()获得的URL来进行身份验证,如下所示

use Pinterest\App\Scope;

$url = $auth->getAuthenticationUrl(
    'https://your/redirect/url/here',
    array(
        Scope::READ_PUBLIC,
        Scope::WRITE_PUBLIC,
        Scope::READ_RELATIONSHIPS,
        Scope::WRITE_RELATIONSHIPS,
    ),
    'random-string'
);

header('Location: ' . $url);
exit;
  • 重定向URL是Pinterest将用户注册时用于发送身份验证代码的页面URL。此URL需要通过https可访问,并且需要填写到Pinterest应用程序的表单中(在Pinterest后端)。
  • 第二个参数是您的应用程序在用户账户上需要的权限数组。这里至少需要有一个。
  • 验证状态是您为注册用户生成的随机代码,并持久化(例如在SESSION中)。Pinterest会将其发送回我们供进一步参考。

当您的应用程序用户同意通过API让您的应用程序控制他们的Pinterest账户时,Pinterest会将他们重定向到您提供的重定向URL,并附加一些GET参数。最重要的是“code”,我们将用它来换取OAuth访问令牌。他们还会将验证状态作为GET参数发送回我们,以便我们可以检查我们是否期望此调用。

此过程的最后一步是将该代码换取访问令牌

$code = $_GET['code'];
$token = $auth->requestAccessToken($code);

此时应安全地持久化该令牌。您现在可以使用它来从您的应用程序连接到Pinterest API,代表用户。

初始化Pinterest\Api

$auth = Pinterest\Authentication::onlyAccessToken($client, $token);
$api = new Pinterest\Api($auth);

使用$api中的Pinterest\Api实例,您现在可以代表用户向Pinterest的API发出经过身份验证的API请求。

获取经过身份验证的用户

$response = $api->getCurrentUser();
if ($response->ok()) {
    $user = $response->result(); // $user instanceof Objects\User
}

获取一个用户

// Get user by username
$response = $api->getUser('otthans');

// Get user by user id
$response = $api->getUser('314196648911734959');

if ($response->ok()) {
    $user = $response->result(); // $user instanceof Objects\User
}

获取一个版块

$response = $api->getBoard('314196580192594085');
if ($response->ok()) {
    $board = $response->result(); // $board instanceof Objects\Board
}

更新一个版块

$response = $api->getBoard('314196580192594085');
if (!$response->ok()) {
    die($response->getError());
}

$board = $response->result(); // $board instanceof Objects\Board
$board->name = 'New board name';
$board->description = 'New board description';
$response = $api->updateBoard($board);
if (!$response->ok()) {
    die($response->getError());
}

$updatedBoard = $response->result(); // $updatedBoard instanceof Objects\Board

获取经过身份验证用户的版块

$response = $api->getUserBoards();
if ($response->ok()) {
    $pagedList = $response->result(); // $pagedList instanceof Objects\PagedList
    $boards = $pagedList->items(); // array of Objects\Board objects
}

获取经过身份验证用户的推文

$response = $api->getUserLikes();
if ($response->ok()) {
    $pagedList = $response->result(); // $pagedList instanceof Objects\PagedList
    $pins = $pagedList->items(); // array of Objects\Pin objects
}

查看获取分页列表的下一项

获取版块的推文

$response = $api->getBoardPins($boardId);
if ($response->ok()) {
    $pagedList = $response->result(); // $pagedList instanceof Objects\PagedList
    $pins = $pagedList->items(); // array of Objects\Pin objects
}

查看获取分页列表的下一项

获取经过身份验证用户的关注者

$response = $api->getUserFollowers();
if ($response->ok()) {
    $pagedList = $response->result(); // $boards instanceof Objects\PagedList
    $users = $pagedList->items(); // array of Objects\User objects
}

查看获取分页列表的下一项

获取经过身份验证用户关注的版块

$response = $api->getUserFollowingBoards();
if ($response->ok()) {
    $pagedList = $response->result(); // $boards instanceof Objects\PagedList
    $boards = $pagedList->items(); // array of Objects\Board objects
}

查看获取分页列表的下一项

获取经过身份验证用户关注的用户

$response = $api->getUserFollowing();
if ($response->ok()) {
    $pagedList = $response->result(); // $boards instanceof Objects\PagedList
    $users = $pagedList->items(); // array of Objects\User objects
}

查看获取分页列表的下一项

获取经过身份验证用户关注的兴趣

示例:现代建筑

$response = $api->getUserInterests();
if ($response->ok()) {
    $pagedList = $response->result(); // $boards instanceof Objects\PagedList
    $boards = $pagedList->items(); // array of Objects\Board objects
}

查看获取分页列表的下一项

关注一个用户

$response = $api->followUser('otthans');
if ($response->ok()) {
    // Succeeded
}

创建一个版块

$name = 'My new board';
$optionalDescription = 'The description of the board';
$response = $api->createBoard($name, $optionalDescription);
if ($response->ok()) {
    $board = $response->result(); // $board instanceof Objects\Board
}

删除一个版块

$boardId = '314196580192594085';
$response = $api->createBoard($boardId);
if ($response->ok()) {
    // Succeeded
}

创建一个推文

$boardId = '314196580192594085';
$note = 'This is an amazing pin!';
$optionalLink = 'http://hansott.github.io/';

// Load an image from a url.
$image = Pinterest\Image::url('http://lorempixel.com/g/400/200/cats/');

// Load an image from a file.
$pathToFile = 'myfolder/myimage.png';
$image = Pinterest\Image::file($pathToFile);

// Load a base64 encoded image.
$pathToFile = 'myfolder/myimage.png';
$data = file_get_contents($pathToFile);
$base64 = base64_encode($data);
$image = Pinterest\Image::base64($base64);
 
$response = $api->createPin($boardId, $note, $image, $optionalLink);
if ($response->ok()) {
    $pin = $response->result(); // $pin instanceof Objects\Pin
}

删除一个推文

$pinId = 'the-pin-id';
$response = $api->deletePin($pinId);
if ($response->ok()) {
    // Succeeded
}

获取分页列表的下一项

$hasMoreItems = $pagedList->hasNext();
if (!$hasMoreItems) {
    return;
}
$response = $api->getNextItems($pagedList);
if (!$response->ok()) {
    die($response->getError());
}
$nextPagedList = $response->result();

贡献

有关详细信息,请参阅CONTRIBUTING

安全性

如果您发现任何与安全相关的问题,请通过电子邮件 hansott@hotmail.be 反馈,而不是使用问题跟踪器。

致谢

许可证

MIT许可证(MIT)。有关更多信息,请参阅 许可证文件