A Package for using official Pinterest API with Laravel., (*1)
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
., (*2)
The terms client_id
and client_secret
are in this case app_id
and app_secret
., (*3)
Installation
The Pinterest API wrapper is available on Composer., (*4)
composer require waleedahmad/pinterest-laravel
Configuration
Add service provider for $providers[]
array in config/app.php
array., (*5)
'providers' => [
[...] // other service providers
\WaleedAhmad\Pinterest\ServiceProviders\PinterestServiceProvider::class,
]
Run vendor:publish
command to copy pinterest configuration to app configs directory.
```$xslt
php artisan vendor:publish --provider="WaleedAhmad\Pinterest\ServiceProviders\PinterestServiceProvider", (*6)
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, (*7)
## Getting access token in exchange for code
After you have initialized the class you can get a login URL:
```php
$loginurl = Pinterest::auth()->getLoginUrl(CALLBACK_URL, array('read_public'));
echo '<a href=' . $loginurl . '>Authorize Pinterest</a>';
Check the Pinterest documentation for the available scopes., (*8)
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:, (*9)
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., (*10)
$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., (*11)
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., (*12)
Available models
Interest
If you want more fields you can specify these in the $data
(GET requests) or $fields
(PATCH requests) array. Example:, (*13)
Pinterest::user()->me();
Response:, (*14)
{
"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):, (*15)
Pinterest::user()->me(array(
'fields' => 'username,first_name,last_name,image[small,large]'
));
The response will now be:, (*16)
{
"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
., (*17)
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
., (*18)
Available methods for the collection class:, (*19)
Get all items
all()
, (*20)
$pins = Pinterest::user()->getMePins();
$pins->all();
Returns: array<Model>
, (*21)
Get item at index
get( int $index )
, (*22)
$pins = Pinterest::user()->getMePins();
$pins->get(0);
Returns: Model
, (*23)
Check if collection has next page
hasNextPage()
, (*24)
$pins = Pinterest::user()->getMePins();
$pins->hasNextPage();
Returns: Boolean
, (*25)
Available methods
Every method containing a data
array can be filled with extra data. This can be for example extra fields or pagination., (*26)
Authentication
The methods below are available through Pinterest::auth
., (*27)
Get login URL
getLoginUrl(string $redirect_uri, array $scopes, string $response_type = "code");
, (*28)
Pinterest::auth()->getLoginUrl("https://pinterest.dev/callback.php", array("read_public"));
Check the Pinterest documentation for the available scopes., (*29)
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., (*30)
Get access_token
getOAuthToken( string $code );
, (*31)
Pinterest::auth()->getOAuthToken($code);
Set access_token
setOAuthToken( string $access_token );
, (*32)
Pinterest::auth()->setOAuthToken($access_token);
Get state
getState();
, (*33)
Pinterest::auth()->getState();
Returns: string
, (*34)
Set state
setState( string $state );
, (*35)
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., (*36)
Pinterest::auth()->setState($state);
Rate limit
Get limit
getRateLimit();
, (*37)
This method can be used to get the maximum number of requests., (*38)
Pinterest::getRateLimit();
Returns: int
, (*39)
Get remaining
getRateLimitRemaining();
, (*40)
This method can be used to get the remaining number of calls., (*41)
Pinterest::getRateLimitRemaining();
Returns: int
, (*42)
Users
The methods below are available through Pinterest::users
., (*43)
You also cannot access a user’s boards or Pins who has not authorized your app., (*44)
Get logged in user
me( array $data );
, (*45)
Pinterest::user()->me();
Returns: User
, (*46)
Find a user
find( string $username_or_id );
, (*47)
Pinterest::user()->find('waleedahmad');
Returns: User
, (*48)
Get user's pins
getMePins( array $data );
, (*49)
Pinterest::user()->getMePins();
Returns: Collection<Pin>
, (*50)
Search in user's pins
getMePins( string $query, array $data );
, (*51)
Pinterest::user()->searchMePins("cats");
Returns: Collection<Pin>
, (*52)
Search in user's boards
searchMeBoards( string $query, array $data );
, (*53)
Pinterest::user()->searchMeBoards("cats");
Returns: Collection<Board>
, (*54)
Get user's boards
getMeBoards( array $data );
, (*55)
Pinterest::user()->getMeBoards();
Returns: Collection<Board>
, (*56)
Get user's followers
getMeFollowers( array $data );
, (*57)
Pinterest::user()->getMeFollowers();
Returns: Collection<Pin>
, (*58)
Boards
The methods below are available through Pinterest::boards
., (*59)
Get board
get( string $board_id, array $data );
, (*60)
Pinterest::boards()->get("waleedahmad/pinterest-laravel");
Returns: Board
, (*61)
Create board
create( array $data );
, (*62)
Pinterest::boards()->create(array(
"name" => "Test board from API",
"description" => "Test Board From API Test"
));
Returns: Board
, (*63)
Edit board
edit( string $board_id, array $data, string $fields = null );
, (*64)
Pinterest::boards-edit("waleedahmad/pinterest-laravel", array(
"name" => "Test board after edit"
));
Returns: Board
, (*65)
Delete board
delete( string $board_id, array $data );
, (*66)
Pinterest::boards()->delete("waleedahmad/pinterest-laravel");
Returns: True|PinterestException
, (*67)
Pins
The methods below are available through Pinterest::pins
., (*68)
Get pin
get( string $pin_id, array $data );
, (*69)
Pinterest::pins()->get("181692166190246650");
Returns: Pin
, (*70)
Get pins from board
fromBoard( string $board_id, array $data );
, (*71)
Pinterest::pins()->fromBoard("waleedahmad/pinterest-laravel");
Returns: Collection<Pin>
, (*72)
Create pin
create( array $data );
, (*73)
Creating a pin with an image hosted somewhere else:, (*74)
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:, (*75)
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:, (*76)
Pinterest::pins()->create(array(
"note" => "Test board from API",
"image_base64" => "[base64 encoded image]",
"board" => "waleedahmad/pinterest-laravel"
));
Returns: Pin
, (*77)
Edit pin
edit( string $pin_id, array $data, string $fields = null );
, (*78)
Pinterest::pins()->edit("181692166190246650", array(
"note" => "Updated name"
));
Returns: Pin
, (*79)
Delete pin
delete( string $pin_id, array $data );
, (*80)
Pinterest::pins()->delete("181692166190246650");
Returns: True|PinterestException
, (*81)
Following
The methods below are available through Pinterest::following
., (*82)
Following users
users( array $data );
, (*83)
Pinterest::following()->users();
Returns: Collection<User>
, (*84)
Following boards
boards( array $data );
, (*85)
Pinterest::following()->boards();
Returns: Collection<Board>
, (*86)
Following interests/categories
interests( array $data );
, (*87)
Pinterest::following()->interests();
Returns: Collection<Interest>
, (*88)
Follow an user
followUser( string $username_or_id );
, (*89)
Pinterest::following()->followUser("waleedahmad");
Returns: True|PinterestException
, (*90)
Unfollow an user
unfollowUser( string $username_or_id );
, (*91)
Pinterest::following()->unfollowUser("waleedahmad");
Returns: True|PinterestException
, (*92)
Follow a board
followBoard( string $board_id );
, (*93)
Pinterest::following()->followBoard("503066289565421201");
Returns: True|PinterestException
, (*94)
Unfollow a board
unfollowBoard( string $board_id );
, (*95)
Pinterest::following()->unfollowBoard("503066289565421201");
Returns: True|PinterestException
, (*96)
Follow an interest
According to the Pinterest documentation this endpoint exists, but for some reason their API is returning an error at the moment., (*97)
followInterest( string $interest );
, (*98)
Pinterest::following()->followInterest("architecten-911112299766");
Returns: True|PinterestException
, (*99)
Unfollow an interest
According to the Pinterest documentation this endpoint exists, but for some reason their API is returning an error at the moment., (*100)
unfollowInterest( string $interest );
, (*101)
Pinterest::following()->unfollowInterest("architecten-911112299766");
Returns: True|PinterestException
, (*102)