kaankilic / pinbot
Laravel 5.x 的 Pinterest 包装器
Requires
- php: >=5.5.9
Requires (Dev)
- mockery/mockery: ^0.9.4
- orchestra/testbench: ^3.0
- phpunit/phpunit: ^4.8 || ^5.0
This package is auto-updated.
Last update: 2024-09-13 00:32:37 UTC
README
易于使用的 Pinterest API 包装器,与 Laravel 框架配合良好。此 Laravel 包可以帮助您使用任何账户凭据使用 Pinterest 账户。此库是从 seregazhuk/php-pinterest-bot 分支出来的。
如果您对该包有任何问题或发现任何错误,请随时与我联系。
安装
依赖项
库需要 CURL 扩展和 PHP 5.5.9 或更高版本。
通过 Composer 安装
composer require kaankilic/pinbot
当包通过 Composer 成功下载后,您需要将 Pinbot 包注册到您的应用程序中。您需要将以下语句添加到您的 config/app.php 文件中。
'providers' => array( ... Kaankilic\Pinbot\Providers\PinbotServiceProvider::class, )
之后,您需要注册外观。
'aliases' => array( ... 'Pinbot'=> Kaankilic\Pinbot\Facades\Pinbot::class, )
快速入门
一旦成功安装 Pinbot,它就可以在任何地方使用了。
use Pinbot; $Pinterest = Pinbot::create(); $bot->auth->login('mypinterestlogin', 'mypinterestpassword'); $boards = $bot->boards->forMe(); //returns the boards of logged user.
注意:某些方法通过 Pinterest 导航结果(带有书签)使用 Pinterest,例如获取用户关注者/被关注者、图钉喜欢/不喜欢、搜索以及其他信息查询。这意味着对于每一批结果,都会有对 Pinterest 的请求。这些方法返回一个带有 Pinterest API 结果的 分页 对象。
如何避免被 Pinterest 禁止:不要过于激进地制作图钉或写评论。尝试使用 $bot->wait($seconds) 调用添加一些超时,这样您就会表现得像一个真实的人,而不是一个机器人,一分钟内创建数百个图钉。
账户
登录
$result = $bot->auth->login('mypinterestlogin', 'mypinterestpassword');
登录方法在成功时返回 true,在失败时返回 false
$result = $bot->auth->login('mypinterestlogin', 'mypinterestpassword'); if (!$result) { echo $bot->getLastError(); die(); }
默认情况下,机器人使用自动登录。它使用从上次会话中保存的 cookies。如果自动登录失败,那么机器人将发送登录请求。
要跳过自动登录并强制发送登录请求,可以将 false 作为第三个参数传递
$result = $bot->auth->login('mypinterestlogin', 'mypinterestpassword', false);
或者您可以选择跳过登录。这仅适用于 likes、follows 和制作图钉等操作。您可以通过 isLoggedIn 方法获取当前登录状态
if ($bot->auth->isLoggedIn()) { // ... }
要注销,请使用 logout 方法
$bot->auth->logout();
注册
注册新用户
$bot->auth->register('youremail@gmail.com', 'password', 'Name');
使用具有流畅接口的 Registration 表单对象来指定额外的参数
use seregazhuk\PinterestBot\Api\Forms\Registration; $registration = new Registration('youremail@gmail.com', 'password', 'name'); $registration ->setAge(30) ->setCountry('DE') ->setMaleGender(); // ->setFemaleGender() $bot->auth->register($registration);
注册商业账户。最后一个参数(网站 URL)是 可选的
$bot->auth->registerBusiness('youremail@gmail.com', 'password', 'BusinessName'); $bot->auth->registerBusiness('youremail@gmail.com', 'password', 'BusinessName', 'http://yoursite.com');
带有注册表单的变体
use seregazhuk\PinterestBot\Api\Forms\Registration; $registration = new Registration('youremail@gmail.com', 'password', 'name'); $registration ->setAge(30) ->setCountry('DE') ->setMaleGender() ->setSite('http://yoursite.com'); $bot->auth->registerBusiness($registration);
注册后,您将收到一封确认电子邮件。您可以将此电子邮件中的链接传递给 confirmEmail 方法
$bot->auth->confirmEmail($linkFromEmail);
将您的账户转换为商业账户。需要登录。最后一个参数(网站 URL)是 可选的
$bot->auth->convertToBusiness('businessName'); $bot->auth->convertToBusiness('businessName', 'http://yoursite.com');
重置密码
您可以将重置密码的链接发送到您的电子邮件
$bot->password->sendResetLink('youremail@gmail.com');
然后您可以从电子邮件中获取链接并传递给重置密码
$bot->password->reset( 'https://post.pinterest.com/f/a/your-password-reset-params', 'newPassword' );
个人资料
更改个人资料。要更新个人资料,您需要设置Profile表单对象。它有以下方法
setLastName($lastName),setFirstName($firstName),setUserName($username),setAbout($bio),setLocation($location),setWebsiteUrl($url),setCountry($code)(ISO2代码)。可以通过$bot->user->getCountries()方法检索国家列表。excludeFromSearch($bool)以排除您的帐户从搜索结果中setLocale($locale),可以通过$bot->user->getLocales()方法检索地区列表。setAccountType($type)(仅限企业帐户)可以通过$bot->user->getAccountTypes()方法检索可用的类型列表。setImage($pathToImage):
use seregazhuk\PinterestBot\Api\Forms\Profile $profileForm = (new Profile()) ->setFirstName('John') ->setLastName('Doe') ->setAbout('My bio') ->setCountry('UK'); $bot->user->profile($profileForm);
您可以通过使用setImage()方法和图片路径来更改您的个人资料头像
use seregazhuk\PinterestBot\Api\Forms\Profile $profileForm = (new Profile())->setImage($pathToFile); $bot->user->profile($profileForm);
通过不带任何参数调用profile方法,您可以获取当前的个人资料设置
$profile = $bot->user->profile(); echo $profile['username']; // Prints your username
结果中您可以找到您的用户名以及所有帐户设置。
获取当前用户名
$username = $bot->user->username();
获取当前用户ID
$userId = $bot->user->id();
检查您的帐户是否被禁止
if ($bot->user->isBanned() { // You have ban }
更改您的密码
$bot->password->change('oldPassword', 'newPassword');
从搜索建议中删除您最近搜索的内容
$bot->user->clearSearchHistory();
停用当前帐户
$bot->user->deactivate();
获取会话历史记录
$history = $bot->user->sessionsHistory();
邀请
通过电子邮件邀请某人
$bot->user->invite($email);
版面
获取所有用户板
$boards = $bot->boards->forUser($username);
获取所有当前登录用户的板。
$boards = $bot->boards->forMe();
通过板名和用户名获取完整的板信息。在这里您可以获取板ID,用于进一步的功能(例如,创建图钉或关注板)
$info = $bot->boards->info($username, $board);
创建一个新的板
// Create a public board $bot->boards->create('Name', 'Description'); // Create a private board $bot->boards->createPrivate('Name', 'Description');
通过ID更新一个板
$bot->boards->update($boardId, ['name' => 'New title', 'description' => 'New description']);
在更新中可以传递更多选项:'隐私'默认为公共,'类别'默认为其他
$bot->boards->update($boardId, [ 'name' => 'New title', 'description' => 'New description', 'privacy' => 'secret', 'category' => 'sports', ]);
通过ID删除一个板
$bot->boards->delete($boardId);
通过ID关注/取消关注板
$bot->boards->follow($boardId); $bot->boards->unfollow($boardId);
通过ID获取板的全部图钉(返回分页对象)
foreach ($bot->boards->pins($boardId) as $pin) { // ... }
获取板的关注者。使用Pinterest API分页(返回分页对象)
foreach($bot->boards->followers($boardId) as $follower) { // ... }
当您重新图钉时,Pinterest会建议一些板标题。您可以通过图钉的ID获取这些建议
$suggestions = $bot->boards->titleSuggestionsFor($pinId);
通过消息或电子邮件发送板
// Send board with message $bot->boards->sendWithMessage($boardId, 'Message', $userId); // To a user $bot->boards->sendWithMessage($boardId, 'Message', [$userId1, $userId2]); // To many yusers // Send board by email $bot->boards->sendWithEmail($boardId, 'Message', 'friend@example.com'); // One email $bot->boards->sendWithEmail($boardId, 'Message', ['friend1@example.com', 'friend2@example.com']); // many
邀请
获取您的板邀请
$invites = $bot->boards->invites();
邀请某人加入您的板
// to a user by email $bot->boards->sendInvite($boardId, 'someone@example.com'); // to a user by user id $bot->boards->sendInvite($boardId, $userId); // to users by email $bot->boards->sendInvite($boardId, ['someone@example.com', 'somefriend@example.com']); // to users by user id $bot->boards->sendInvite($boardId, [$user1Id, $user2Id]);
接受板邀请
$bot->boards->acceptInvite($boardId);
忽略板邀请
$bot->boards->ignoreInvite($boardId);
删除邀请。从板协作者中删除,需要您想要从板中删除的用户ID
$bot->boards->deleteInvite($boardId, $userId); // also you can ban a user specifying third argument as true $bot->boards->deleteInvite($boardId, $userId, true);
图钉
注意!在图钉或评论图钉时不要过于激进,否则Pinterest可能会禁止您。
通过ID获取图钉信息
$info = $bot->pins->info(1234567890);
创建新的图钉。接受图片URL,板ID,图片发布位置,描述和预览URL
$pinInfo = $bot->pins->create('http://exmaple.com/image.jpg', $boardId, 'Pin description'); print_r($pinfInfo['id']);
您可以通过本地图片的路径传递。它将被上传到Pinterest
$pinInfo = $bot->pins->create('image.jpg', $boardId, 'Pin description');
您可以将图钉的链接(来源)指定为第四个参数。如果没有设置,链接等于图片URL
$pinInfo = $bot->pins->create( 'http://exmaple.com/image.jpg', $boardId, 'Pin description', 'http://site.com', );
通过ID重新图钉图钉。您需要一个图钉ID和一个您想要放置此图钉的板ID。第三个参数是图钉描述,它是可选的。
$pinInfo = $bot->pins->repin($pinId, $boardId, 'my repin');
通过ID编辑图钉。您可以更改图钉的描述、链接或板
// Change description and link $bot->pins->edit($pinId, 'new description', 'new link'); // Change board $bot->pins->edit($pinId, 'new description', 'new link', $newBoardId);
将图钉移动到新的板
// Change board $bot->pins->moveToBoard($pinId, $newBoardId);
通过ID删除图钉
$bot->pins->delete($pinId);
通过ID点赞/不喜欢图钉
$bot->pins->like($pinId); $bot->pins->unLike($pinId);
复制/移动图钉到板。要复制/移动一个图钉,传递它的ID作为第一个参数。传递ID数组以复制/移动多个图钉
$bot->pins->copy($pinId, $boardId); $bot->pins->move($pinId, $boardId);
从图钉保存图片到磁盘。将图钉的原始图片保存到指定的路径
$imagePath = $bot->pins->saveOriginalImage($pinId, $pathForPics);
从板中删除图钉。要删除一个图钉,传递它的ID作为第一个参数。传递ID数组以删除多个图钉
$bot->pins->deleteFromBoard($pinId, $boardId);
写评论
$result = $bot->comments->create($pinId, 'your comment'); // Result contains info about written comment. For example, // comment_id if you want to delete it.
删除评论
$bot->comments->delete($pinId, $commentId);
从特定URL获取图钉。例如:https://pinterest.com/source/flickr.com/将返回flickr.com的最新图钉(返回分页对象)
foreach ($bot->pins->fromSource('flickr.com') as $pin) { // ... }
获取用户图钉源(返回分页对象)
foreach ($bot->pins->feed() as $pin) { // ... } // Only first 20 pins from feed foreach ($bot->pins->feed(20) as $pin) { // ... }
获取引脚的活动(返回分页对象)
foreach ($bot->pins->activity($pinId) as $data) { // ... }
如果您不想获取所有活动记录,可以将限制作为第二个参数传递。获取最后5条活动记录
$activities = $bot->pins->activity($pinId, 5); // print_r($activities->toArray()); foreach ($activities as $activity) { // ... }
获取当前引脚的相关引脚(返回分页对象)
foreach ($bot->pins->related($pinId) as $pin) { // ... }
获取当前引脚的最后10个相关引脚
$relatedPins = $bot->pins->related($pinId, 10); // print_r($relatedPins->toArray()); foreach ($relatedPins as $pin) { // ... }
从http://pinterest.com/discover页面获取特定主题的热门引脚。使用可以从中接收的topic id,即从$bot->topics->explore()方法(返回分页对象)
$trendingTopics = $bot->topics->explore(); $firstTopicId = $trendingTopics[0]['id']; $pins = $bot->pins->explore($firstTopicId)->toArray();
获取视觉上相似的引脚
$result = $bot->pins->visualSimilar($pinId);
通过消息或电子邮件发送引脚
// Send pin with message $bot->pins->sendWithMessage($pinId, 'message', $userId); // To a user $bot->pins->sendWithMessage($pinId, 'message', [$userId1, $userId2]); // To many users // Send pin by email $bot->pins->sendWithEmail($pinId, 'message', 'friend@example.com'); // One email $bot->pins->sendWithEmail($pinId, 'message', ['friend1@example.com', 'friend2@example.com']); // Many
获取您的引脚分析,如点击次数、查看次数和重新推送次数(仅限商业账户);
$analytics = $bot->pins->analytics($pinId);
试用
获取已绑定此引脚的引脚用户(返回分页对象)
$pinners = $bot->pins->tried($pinId); // print_r($pinners->toArray()); foreach ($pinners as $pinner) { // ... }
尝试一个引脚。第三个参数是图像文件的路径,是可选的。返回一个包含创建记录数据的数组
$tryRecord = $bot->pins->tryIt($pinId, 'comment', 'pathToImage');
删除您的尝试。您可以使用在创建tryIt记录时接收到的数据中的id字段
$tryRecord = $bot->pins->tryIt($pinId, 'comment', 'pathToImage'); // ... $bot->pins->deleteTryIt($tryRecord['id']);
编辑您的尝试。您可以使用在创建tryIt记录时接收到的数据中的id字段。您还需要为您的尝试提供引脚id
$tryRecord = $bot->pins->tryIt($pinId, 'comment', 'pathToImage'); // ... $bot->pins->editTryIt($tryRecord['pin']['id'], $tryRecord['id'], 'new comment', 'optionalPathToImage');
钉钉者
关注/取消关注用户。您可以使用id或用户名。**注意**:当使用用户名时,机器人将进行一个额外的请求来解析用户名对应的id
$bot->pinners->follow($userId); $bot->pinners->unfollow($userId); $bot->pinners->follow($username); $bot->pinners->unfollow($username);
通过用户名获取用户信息
$userData = $bot->pinners->info($username);
获取用户关注的用户信息。默认返回关注的用户。返回分页对象
foreach ($bot->pinners->following('username') as $following) { // ... }
您可以指定要返回的实体类型:人、兴趣或板。例如
foreach ($bot->pinners->following('username', 'people') as $user) { // Loop through people } foreach($bot->pinners->following('username', 'boards') as $board) { // Loop through boards } foreach($bot->pinners->following('username', 'interests') as $interest) { // Loop through interests }
您还可以使用特殊的方法辅助器来实现相同的结果
foreach ($bot->pinners->followingPeople('username') as $user) { // Loop through people } foreach ($bot->pinners->followingBoards('username') as $board) { // Loop through boards } foreach($bot->pinners->followingInterests('username') as $interest) { // Loop through interests }
获取用户关注者(返回分页对象)。接受可选参数username,其订阅者需要接收。
foreach ($bot->pinners->followers('username') as $follower) { // ... }
不带参数返回当前用户的关注者
// returns my followers foreach($bot->pinners->followers() as $follower) { // ... }
获取一个引脚的最新引脚(返回分页对象)
foreach ($bot->pinners->pins('username') as $pin) { // ... }
获取一个引脚的最后20个引脚
foreach ($bot->pinners->pins('username', 20) as $pin) { // ... }
获取用户喜欢的引脚(返回分页对象)
foreach ($bot->pinners->likes('username') as $like) { // ... }
阻止用户
// By name $bot->pinners->block('username'); // By id. For example, after calling info() method $pinnerInfo = $bot->pinners->info('username'); $bot->pinners->block($pinnerInfo['id']);
兴趣
获取主要类别的列表。需要机器人登录
$categories = $bot->interests->main();
通过名称获取类别信息(可以从main()中获取)
$info = $bot->interests->info("gifts");
获取兴趣的相关主题
$topics = $bot->interests->getRelatedTopics('videos');
获取特定兴趣的引脚(返回分页对象)
foreach ($bot->interests->pins('videos') as $pin) { // ... }
主题
每个兴趣都有一个相关主题的列表。
通过名称关注/取消关注主题
$bot->topics->follow('content-marketing'); $bot->topics->unFollow('content-marketing');
获取主题信息
$info = $bot->topics->info('content-marketing');
获取特定主题的引脚(返回分页对象)
foreach ($bot->topics->pins('content-marketing') as $pin) { // ... }
获取主题的相关主题(与兴趣的相关主题类似)
$topics = $bot->topics->getRelatedTopics('content-marketing');
从http://pinterest.com/discover页面获取热门主题。然后您可以使用每个主题的id,通过$bot->pins->explore()方法获取该主题的热门引脚
$trendingTopics = $bot->topics->explore(); $firstTopicId = $trendingTopics[0]['id']; $pins = $bot->pins->explore($firstTopicId)->toArray();
搜索
搜索函数使用Pinterest分页获取结果,并返回分页对象
$pins = $bot->pins->search('query')->toArray(); print_r($pins); // Or iterate with requests foreach ($bot->pins->search('query') as $pin) { // ... } // Search only in my pins $pins = $bot->pins->searchInMyPins('query')->toArray(); // Search in people foreach($bot->pinners->search('query') as $pinner) { // ... } // Search in boards foreach($bot->boards->search('query') as $board) { // ... }
收件箱
新闻
获取您当前用户的新闻(返回分页对象)
// Get result as array $news = $bot->inbox->news()->toArray(); // Iterate with requests foreach ($bot->inbox->news() as $new) { // ... }
通知
获取用户的通知(返回分页对象)
// Get result as array $notifications = $bot->inbox->notifications()->toArray(); // Iterate with requests foreach ($bot->inbox->notifications() as $notification) { // ... }
对话
获取最后对话的数组
$conversations = $bot->inbox->conversations(); print_r($conversations);
写消息
通过id向用户发送消息。您可以通过id指定一个用户,或者传递一个用户id数组
$bot->inbox->sendMessage($userId, 'message text');
将引脚通过id附加到消息
$pinId = 123456789; $bot->inbox->sendMessage($userId, 'message text', $pinId);
发送电子邮件
电子邮件参数可以是字符串或电子邮件数组
$bot->inbox->sendEmail('mail@domain.com', 'message text');
将引脚附加到电子邮件
$bot->inbox->sendEmail('mail@domain.com', 'message text', $pindId);
联系请求
当有人第一次邀请您加入一个版面时,您会收到一个联系请求。获取联系请求列表
$requests = $bot->inbox->contactRequests();
要接受或忽略一个请求,您需要指定请求ID。此ID可以从$bot->inbox->contactRequests()方法返回的数组中接收。
接受请求
$bot->inbox->acceptContactRequest($requestId);
忽略请求
$bot->inbox->ignoreContactRequest($requestId);
关键词
获取查询的推荐关键词
$keywords = $bot->keywords->recommendedFor('dress'); print_r($keywords); /* Array ( [0] => Array ( [term] => for teens [position] => 1 [display] => For Teens ) [1] => Array ( [term] => wedding [position] => 0 [display] => Wedding ) // ... ) */
"位置"决定创建完整单词的顺序。例如
- "for teens",位置 = 1 -> 完整关键词是:"dress for teens"
- "wedding",位置 = 0 -> 完整关键词是:"wedding dress"
因此,位置 = 0 表示在拼接时,附加关键词应放在搜索关键词之前,而位置 = 1 用于反向情况。
错误处理
您可以使用方法getLastError()检查请求后发生的错误。它返回包含您最后一次请求到API的错误字符串
$error = $bot->getLastError(); echo $error;
使用代理
要设置代理设置,请使用useProxy方法
$bot->getHttpClient()->useProxy('192.168.1.1', '12345');
默认情况下,它使用未经身份验证的http代理。如果您的代理需要身份验证,请将身份验证字符串作为第三个参数传递
$bot->getHttpClient()->useProxy('192.168.1.1', '12345', 'username:password');
使用socks代理
$bot->getHttpClient()->useSocksProxy('192.168.1.1', '12345'); // With authentication $bot->getHttpClient()->useSocksProxy('192.168.1.1', '12345', 'username:password');
如果您需要停止通过代理发送请求
$bot->getHttpClient()->dontUseProxy();
检查机器人是否使用代理
if($bot->getHttpClient()->usesProxy()) { // ... }
自定义请求设置
可以为机器人请求添加一些额外的Curl选项。例如,您可以设置代理和用户代理如下
$bot->getHttpClient()->setOptions([ CURLOPT_PROXY => 'xx.xx.xxx.xx:xxxx', CURLOPT_PROXYTYPE => CURLPROXY_HTTP // Or CURLPROXY_SOCKS5, CURLOPT_USERAGENT => 'Your_User_Agent', ]);
每次请求Pinterest都会返回一个包含您当前客户端信息的数组,如OS、浏览器、IP等
$info = $bot->getClientInfo();
默认情况下,它使用最后一次请求的客户端信息。要重新加载客户端上下文,请传递true参数
// Force to reload client info $info = $bot->getClientInfo(true);
您可以获取最后访问的页面的URL
$url = $bot->getHttpClient()->getCurrentUrl();
cookies
当前机器人Cookies可以通过getHttpClient和cookie/cookies方法获取。所有Cookies
$cookies = $bot->getHttpClient()->cookies();
按名称获取Cookies值
$someCookieValue = $bot->getHttpClient()->cookie('cookieName');
默认情况下,Cookies文件存储在您的系统临时目录中。您可以设置自定义路径来存储Cookies。注意!此路径必须具有写入权限
$bot->getHttpClient()->setCookiesPath($yourCustomPathForCookies); $currentPath = $bot->getHttpClient()->getCookiesPath();
删除您的Cookies
$bot->getHttpClient()->removeCookies();
访问(点击)链接。例如,当Pinterest发送包含某些链接的电子邮件时,您希望机器人访问它
$bot->user->visitPage($url);
分页
大多数方法使用Pinterest分页。例如,当您运行$bot->pins->search('query')时,Pinterest只为请求返回20个结果,您不能仅通过一个请求一次性获取所有图钉。因此,这些方法返回Pagination对象。您可以遍历它以获取结果
$pagination = $bot->pins->search('query'); foreach ($pagination as $pin) { // ... }
或者,您可以一次性获取所有结果作为数组,但这将需要一些时间,循环遍历所有Pinterest页面以获取这些结果
$pagination = $bot->pins->search('query'); $results = $pagination->toArray(); // Or $results = $bot->pins->search('query')->toArray();
默认情况下,方法返回前50个结果。例如,$bot->pins->search('query')将只返回前50个图钉。但是,您可以指定另一个限制数作为第二个参数。或者传递0以无限制。例如,
foreach ($bot->pins->search('query', 20) as $pin) { // ... }
将只返回搜索结果的20个图钉。
结果中的限制和偏移量
// Skip first 50 results $results = $bot->pins ->search('query') ->skip(50) ->get(); // Skip first 50 results, and then take 20 $results = $bot->pins ->search('query') ->take(20) ->skip(50) ->get();
要获取所有结果,请将0传递到take()方法中。
如何感谢您?
为什么不给GitHub仓库加星?我会很高兴得到关注!
谢谢!