Skip to content

Commit

Permalink
Fix merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkgroenen committed Feb 10, 2016
2 parents a82931d + 975c69f commit 4a91703
Show file tree
Hide file tree
Showing 15 changed files with 190 additions and 54 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
### 0.2.9 (10-02-2016)

- Add autoload.php as Composer alternative [#37](https://github.com/dirkgroenen/Pinterest-API-PHP/issues/37)
- Add board update endpoint [#34](https://github.com/dirkgroenen/Pinterest-API-PHP/issues/34)
- Fix patch requests
- Add `$field` to update requests

### 0.2.8 (02-02-2016)

- Fix Curl execFollow and error handling [#31](https://github.com/dirkgroenen/Pinterest-API-PHP/issues/31)
Expand Down
67 changes: 40 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
## ![](http://i.imgur.com/cacgQlq.png) Pinterest API - PHP

## ![](http://i.imgur.com/cacgQlq.png) Pinterest API - PHP

[![](https://travis-ci.org/dirkgroenen/Pinterest-API-PHP.svg)](https://travis-ci.org/dirkgroenen/Pinterest-API-PHP)
[![](https://img.shields.io/scrutinizer/g/dirkgroenen/Pinterest-API-PHP.svg)](https://scrutinizer-ci.com/g/dirkgroenen/Pinterest-API-PHP/?branch=master)
Expand All @@ -16,7 +15,7 @@ A PHP wrapper for the official [Pinterest API](https://dev.pinterest.com).
- Registered Pinterest App

# Get started
To use the Pinterest API you have to register yourself as a developer and [create](https://dev.pinterest.com/apps/) an application. After you've created your app you will receive a `app_id` and `app_secret`.
To use the Pinterest API you have to register yourself as a developer and [create](https://dev.pinterest.com/apps/) an application. After you've created your app you will receive a `app_id` and `app_secret`.

> The terms `client_id` and `client_secret` are in this case `app_id` and `app_secret`.
Expand All @@ -27,7 +26,9 @@ The Pinterest API wrapper is available on Composer.
composer require dirkgroenen/Pinterest-API-PHP
```

## Simple Example
If you're not using Composer (which you should start using, unless you've got a good reason not to) you can include the `autoload.php` file in your project.

## Simple Example
```php
use DirkGroenen\Pinterest\Pinterest;

Expand All @@ -41,9 +42,9 @@ $loginurl = $pinterest->auth->getLoginUrl(CALLBACK_URL, array('read_public'));
echo '<a href=' . $loginurl . '>Authorize Pinterest</a>';
```

Check the [Pinterest documentation](https://dev.pinterest.com/docs/api/overview/#scopes) for the available scopes.
Check the [Pinterest documentation](https://dev.pinterest.com/docs/api/overview/#scopes) for the available scopes.

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:
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:

```php
if(isset($_GET["code"])){
Expand All @@ -54,19 +55,19 @@ if(isset($_GET["code"])){

## Get the user's profile

To get the profile of the current logged in user you can use the `Users::me(<array>);` method.
To get the profile of the current logged in user you can use the `Users::me(<array>);` method.

```php
$me = $pinterest->users->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.
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.

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.
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.

## Available models
## Available models

### [User](https://dev.pinterest.com/docs/api/users/#user-object)

Expand All @@ -79,7 +80,7 @@ Models also show the available fields (which are also described in the Pinterest
- name

## Retrieving extra fields
If you want more fields you can specify these in the `$data` array. Example:
If you want more fields you can specify these in the `$data` (GET requests) or `$fields` (PATCH requests) array. Example:

```php
$pinterest->users->me();
Expand All @@ -100,15 +101,15 @@ Response:
}
```

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):
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):

```php
$pinterest->users->me(array(
'fields' => 'username,first_name,last_name,image[small,large]'
));
```

The response will now be:
The response will now be:

```json
{
Expand Down Expand Up @@ -176,9 +177,9 @@ Returns: `Boolean`

# Available methods

> Every method containing a `data` array can be filled with extra data. This can be for example extra fields or pagination.
> Every method containing a `data` array can be filled with extra data. This can be for example extra fields or pagination.
## Authentication
## Authentication

The methods below are available through `$pinterest->auth`.

Expand All @@ -189,7 +190,7 @@ The methods below are available through `$pinterest->auth`.
$pinterest->auth->getLoginUrl("https://pinterest.dev/callback.php", array("read_public"));
```

Check the [Pinterest documentation](https://dev.pinterest.com/docs/api/overview/#scopes) for the available scopes.
Check the [Pinterest documentation](https://dev.pinterest.com/docs/api/overview/#scopes) for the available scopes.

**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.**

Expand Down Expand Up @@ -219,7 +220,7 @@ Returns: `string`
### Set state
`setState( string $state );`

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.
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.

```php
$pinterest->auth->setState($state);
Expand Down Expand Up @@ -335,7 +336,7 @@ The methods below are available through `$pinterest->boards`.
`get( string $board_id, array $data );`

```php
$pinterest->boards->get("503066289565421201");
$pinterest->boards->get("dirkgroenen/pinterest-api-test");
```

Returns: `Board`
Expand All @@ -352,11 +353,22 @@ $pinterest->boards->create(array(

Returns: `Board`

### Edit board
`edit( string $board_id, array $data, string $fields = null );`

```php
$pinterest->boards-edit("dirkgroenen/pinterest-api-test", array(
"name" => "Test board after edit"
));
```

Returns: `Board`

### Delete board
`delete( string $board_id, array $data );`

```php
$pinterest->boards->delete("503066289565421201");
$pinterest->boards->delete("dirkgroenen/pinterest-api-test");
```

Returns: `True|PinterestException`
Expand All @@ -378,7 +390,7 @@ Returns: `Pin`
`fromBoard( string $board_id, array $data );`

```php
$pinterest->pins->fromBoard("503066289565421201");
$pinterest->pins->fromBoard("dirkgroenen/pinterest-api-test");
```

Returns: `Collection<Pin>`
Expand All @@ -392,7 +404,7 @@ Creating a pin with an image hosted somewhere else:
$pinterest->pins->create(array(
"note" => "Test board from API",
"image_url" => "https://download.unsplash.com/photo-1438216983993-cdcd7dea84ce",
"board" => "503066289565421201"
"board" => "dirkgroenen/pinterest-api-test"
));
```

Expand All @@ -402,7 +414,7 @@ Creating a pin with an image located on the server:
$pinterest->pins->create(array(
"note" => "Test board from API",
"image" => "/path/to/image.png",
"board" => "503066289565421201"
"board" => "dirkgroenen/pinterest-api-test"
));
```

Expand All @@ -412,20 +424,21 @@ Creating a pin with a base64 encoded image:
$pinterest->pins->create(array(
"note" => "Test board from API",
"image_base64" => "[base64 encoded image]",
"board" => "503066289565421201"
"board" => "dirkgroenen/pinterest-api-test"
));
```


Returns: `Pin`

### Update pin
> According to the Pinterest documentation this endpoint exists, but for some reason their API is returning an error at the moment of writing.
### Edit pin

`update( string $pin_id, array $data );`
`edit( string $pin_id, array $data, string $fields = null );`

```php
$pinterest->pins->update("181692166190246650");
$pinterest->pins->edit("181692166190246650", array(
"note" => "Updated name"
));
```

Returns: `Pin`
Expand Down
39 changes: 39 additions & 0 deletions autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* Will autoload all required classes in the \DirkGroenen\Pinterest
* namespace.
*
* Only use this file if you're not using Composer (which you should
* start using unless you've a good reason not to)
*
* Based on the standard PSR-4 autoloader:
* https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4-autoloader-examples.md
*/
spl_autoload_register(function($class) {

// project-specific namespace prefix
$prefix = 'DirkGroenen\\Pinterest\\';

// base directory for the namespace prefix
$base_dir = __DIR__ . '/src/Pinterest/';

// does the class use the namespace prefix?
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
// no, move to the next registered autoloader
return;
}

// get the relative class name
$relative_class = substr($class, $len);

// replace the namespace prefix with the base directory, replace namespace
// separators with directory separators in the relative class name, append
// with .php
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';

// if the file exists, require it
if (file_exists($file)) {
require $file;
}
});
12 changes: 9 additions & 3 deletions demo/callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@
<html>
<head></head>
<body>
<h1>Hey you!</h1>
<h2>$pinterest->boards->create()</h2>
<?php
/*echo $pinterest->boards->edit("dirkgroenen/test-from-api", array(
"name" => "Test from API - update",
"description" => "Test"
));*/

<?php echo $pinterest->users->getMeBoards(); ?>
echo $pinterest->boards->edit("dirkgroenen/test-from-api", array(
"name" => "Noted update 2 - API"
), "id,name,url,description,creator,created_at,counts,image");
?>

</body>
</html>
2 changes: 1 addition & 1 deletion demo/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
<head></head>
<body>
<h1>Login with Pinterest</h1>
<p>Use this <a href="<?php echo $pinterest->auth->getLoginUrl('https://github.local/pinterest/demo/callback.php', array('read_public', 'write_public')); ?>">link to login</a> with your Pinterest account.</p>
<p>Use this <a href="<?php echo $pinterest->auth->getLoginUrl('https://pinterest.dev/demo/callback.php', array('read_public', 'write_public')); ?>">link to login</a> with your Pinterest account.</p>
</body>
</html>
18 changes: 18 additions & 0 deletions src/Pinterest/Endpoints/Boards.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,24 @@ public function create(array $data)
return new Board($this->master, $response);
}

/**
* Edit a board
*
* @access public
* @param string $board_id
* @param array $data
* @param string $fields
* @throws Exceptions/PinterestExceptions
* @return Board
*/
public function edit($board_id, array $data, $fields = null)
{
$query = (!$fields) ? array() : array("fields" => $fields);

$response = $this->request->update(sprintf("boards/%s", $board_id), $data, $query);
return new Board($this->master, $response);
}

/**
* Delete a board
*
Expand Down
9 changes: 6 additions & 3 deletions src/Pinterest/Endpoints/Pins.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,20 @@ public function create(array $data)
}

/**
* Update a pin
* Edit a pin
*
* @access public
* @param string $pin_id
* @param array $data
* @param string $fields
* @throws Exceptions/PinterestExceptions
* @return Pin
*/
public function update($pin_id, array $data)
public function edit($pin_id, array $data, $fields = null)
{
$response = $this->request->update(sprintf("pins/%s", $pin_id), $data);
$query = (!$fields) ? array() : array("fields" => $fields);

$response = $this->request->update(sprintf("pins/%s", $pin_id), $data, $query);
return new Pin($this->master, $response);
}

Expand Down
10 changes: 5 additions & 5 deletions src/Pinterest/Models/Board.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<?php
<?php
/**
* Copyright 2015 Dirk Groenen
* Copyright 2015 Dirk Groenen
*
* (c) Dirk Groenen <[email protected]>
*
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace DirkGroenen\Pinterest\Models;

class Board extends Model {

/**
* The available object keys
*
*
* @var array
*/
protected $fillable = ["id", "name", "url", "description", "creator", "created_at", "counts", "image"];
Expand Down
1 change: 0 additions & 1 deletion src/Pinterest/Models/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,4 @@ public function __toString()
{
return $this->toJson();
}

}
Loading

0 comments on commit 4a91703

Please sign in to comment.