1. Go to this page and download the library: Download pinterest/laravel-sdk library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
pinterest / laravel-sdk example snippets
use Pinterest\Facades\Pinterest;
// List boards
$boards = Pinterest::boards()->list(['page_size' => 25]);
foreach ($boards->items() as $board) {
echo $board['name'];
}
// Get a specific board
$board = Pinterest::boards()->get('board-id');
// Create a board
$board = Pinterest::boards()->create([
'name' => 'My Recipes',
'description' => 'Favorite recipes collection',
'privacy' => 'PUBLIC',
]);
// Update a board
Pinterest::boards()->update('board-id', [
'name' => 'Updated Name',
]);
// Delete a board
Pinterest::boards()->delete('board-id');
// Board sections
Pinterest::boards()->createSection('board-id', ['name' => 'Desserts']);
Pinterest::boards()->listSections('board-id');
Pinterest::boards()->updateSection('board-id', 'section-id', ['name' => 'New Name']);
Pinterest::boards()->deleteSection('board-id', 'section-id');
// List pins on a board
$pins = Pinterest::boards()->listPins('board-id');
// List pins
$pins = Pinterest::pins()->list(['page_size' => 10]);
// Get a pin
$pin = Pinterest::pins()->get('pin-id');
// Create a pin
$pin = Pinterest::pins()->create([
'board_id' => 'board-id',
'title' => 'Amazing Sunset',
'description' => 'Beautiful sunset at the beach',
'link' => 'https://example.com/sunset',
'media_source' => [
'source_type' => 'image_url',
'url' => 'https://example.com/sunset.jpg',
],
]);
// Update a pin
Pinterest::pins()->update('pin-id', [
'title' => 'Updated Title',
]);
// Delete a pin
Pinterest::pins()->delete('pin-id');
// Save a pin to a different board
Pinterest::pins()->save('pin-id', [
'board_id' => 'other-board-id',
]);
// Get authenticated user info
$user = Pinterest::user()->getAccount();
echo $user->get('username');
// Get user analytics
$analytics = Pinterest::user()->getAnalytics([
'start_date' => '2024-01-01',
'end_date' => '2024-01-31',
'metric_types' => 'IMPRESSION,PIN_CLICK',
]);
// Top pins analytics
$topPins = Pinterest::user()->getTopPinsAnalytics([
'start_date' => '2024-01-01',
'end_date' => '2024-01-31',
'sort_by' => 'IMPRESSION',
'num_of_pins' => 10,
]);
$response = Pinterest::boards()->list(['page_size' => 25]);
// Iterate through all pages
while (true) {
foreach ($response->items() as $board) {
echo $board['name'] . "\n";
}
if (!$response->hasMorePages()) {
break;
}
$response = $response->getNextPage();
}
// 1. Generate authorization URL and redirect user
$state = bin2hex(random_bytes(16));
session(['pinterest_oauth_state' => $state]);
$url = Pinterest::oauth()->getAuthorizationUrl($state);
return redirect($url);
// 2. Handle the callback - exchange code for token
$code = request('code');
$token = Pinterest::oauth()->exchangeCodeForToken($code);
// Store tokens securely
$token->getAccessToken(); // Access token string
$token->getRefreshToken(); // Refresh token string
$token->getExpiresIn(); // Lifetime in seconds
// 3. Use the new token
Pinterest::setAccessToken($token->getAccessToken());
// 4. Refresh an expired token
$newToken = Pinterest::oauth()->refreshToken($token->getRefreshToken());