From 74b0c2a250a1651d78997559fcab4742bd8c2e66 Mon Sep 17 00:00:00 2001 From: Dirk Groenen Date: Tue, 2 Feb 2016 17:28:27 +0100 Subject: [PATCH 01/15] Create board on callback --- demo/callback.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/demo/callback.php b/demo/callback.php index c52702b..e69c145 100644 --- a/demo/callback.php +++ b/demo/callback.php @@ -7,7 +7,9 @@

Hey you!

$pinterest->boards->create()

- users->getMeBoards(); ?> + boards->create(array( + "name" => "Test from Pinterest API" + )); ?> \ No newline at end of file From c4023ecb59c1aec7ead89f0b9b70e45e28e36981 Mon Sep 17 00:00:00 2001 From: Dirk Groenen Date: Fri, 5 Feb 2016 16:25:42 +0100 Subject: [PATCH 02/15] Change board ID to boardname --- README.md | 46 +++++++++++++++++++++++----------------------- demo/callback.php | 8 +++++--- 2 files changed, 28 insertions(+), 26 deletions(-) diff --git a/README.md b/README.md index 14f80b1..e946643 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ -## ![](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) -[![](https://img.shields.io/scrutinizer/coverage/g/dirkgroenen/Pinterest-API-PHP.svg)](https://scrutinizer-ci.com/g/dirkgroenen/Pinterest-API-PHP/?branch=master) -[![Packagist](https://img.shields.io/packagist/v/dirkgroenen/pinterest-api-php.svg)](https://packagist.org/packages/dirkgroenen/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) +[![](https://img.shields.io/scrutinizer/coverage/g/dirkgroenen/Pinterest-API-PHP.svg)](https://scrutinizer-ci.com/g/dirkgroenen/Pinterest-API-PHP/?branch=master) +[![Packagist](https://img.shields.io/packagist/v/dirkgroenen/pinterest-api-php.svg)](https://packagist.org/packages/dirkgroenen/pinterest-api-php) [![Support me with some coffee](https://img.shields.io/badge/donate-paypal-orange.svg)](https://www.paypal.me/dirkgroenen) ------------------- @@ -16,7 +16,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`. @@ -27,7 +27,7 @@ The Pinterest API wrapper is available on Composer. composer require dirkgroenen/Pinterest-API-PHP ``` -## Simple Example +## Simple Example ```php use DirkGroenen\Pinterest\Pinterest; @@ -41,9 +41,9 @@ $loginurl = $pinterest->auth->getLoginUrl(CALLBACK_URL, array('read_public')); echo 'Authorize Pinterest'; ``` -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"])){ @@ -54,7 +54,7 @@ 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();` method. +To get the profile of the current logged in user you can use the `Users::me();` method. ```php $me = $pinterest->users->me(); @@ -62,11 +62,11 @@ 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) @@ -100,7 +100,7 @@ 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( @@ -108,7 +108,7 @@ $pinterest->users->me(array( )); ``` -The response will now be: +The response will now be: ```json { @@ -176,9 +176,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`. @@ -189,7 +189,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.** @@ -219,7 +219,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); @@ -378,7 +378,7 @@ Returns: `Pin` `fromBoard( string $board_id, array $data );` ```php -$pinterest->pins->fromBoard("503066289565421201"); +$pinterest->pins->fromBoard("dirkgroenen/pinterest-api-test"); ``` Returns: `Collection` @@ -392,7 +392,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" )); ``` @@ -402,7 +402,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" )); ``` @@ -412,7 +412,7 @@ 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" )); ``` diff --git a/demo/callback.php b/demo/callback.php index e69c145..245159e 100644 --- a/demo/callback.php +++ b/demo/callback.php @@ -5,10 +5,12 @@

Hey you!

-

$pinterest->boards->create()

+

$pinterest->pins->create()

- boards->create(array( - "name" => "Test from Pinterest API" + pins->create(array( + "board" => "pinterest-api-test", + "note" => "Test from API", + "image_url" => "https://download.unsplash.com/photo-1438216983993-cdcd7dea84ce" )); ?> From db14bab6035769b44592bca1aef31826fea5c7a8 Mon Sep 17 00:00:00 2001 From: Dirk Groenen Date: Fri, 5 Feb 2016 16:29:04 +0100 Subject: [PATCH 03/15] Change in demo --- demo/callback.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/demo/callback.php b/demo/callback.php index 245159e..95b3d7d 100644 --- a/demo/callback.php +++ b/demo/callback.php @@ -7,8 +7,7 @@

Hey you!

$pinterest->pins->create()

- pins->create(array( - "board" => "pinterest-api-test", + pins->delete("503066220857361559", array( "note" => "Test from API", "image_url" => "https://download.unsplash.com/photo-1438216983993-cdcd7dea84ce" )); ?> From e1e2bdc45f3d29e6d358fc8d698d1061721daaab Mon Sep 17 00:00:00 2001 From: Dirk Groenen Date: Wed, 10 Feb 2016 20:04:04 +0100 Subject: [PATCH 04/15] Add autoload #37 --- autoload.php | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 autoload.php diff --git a/autoload.php b/autoload.php new file mode 100644 index 0000000..f108c50 --- /dev/null +++ b/autoload.php @@ -0,0 +1,39 @@ + Date: Wed, 10 Feb 2016 20:19:55 +0100 Subject: [PATCH 05/15] Add board edit endpoint --- README.md | 17 +++++++++++++++-- demo/boot.php | 2 +- src/Pinterest/Endpoints/Boards.php | 15 +++++++++++++++ tests/Pinterest/Endpoints/BoardsTest.php | 18 ++++++++++++++---- tests/Pinterest/env | 6 ++++++ .../responses/BoardsTest/testEdit.json | 7 +++++++ 6 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 tests/Pinterest/env create mode 100644 tests/Pinterest/responses/BoardsTest/testEdit.json diff --git a/README.md b/README.md index e946643..25c1a3f 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,8 @@ The Pinterest API wrapper is available on Composer. composer require dirkgroenen/Pinterest-API-PHP ``` +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; @@ -335,7 +337,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` @@ -352,11 +354,22 @@ $pinterest->boards->create(array( Returns: `Board` +### Edit board +`edit( string $board_id, array $data );` + +```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` diff --git a/demo/boot.php b/demo/boot.php index 2c02d9f..7992dae 100644 --- a/demo/boot.php +++ b/demo/boot.php @@ -3,7 +3,7 @@ ini_set('display_startup_errors', 1); error_reporting(E_ALL); - require "../vendor/autoload.php"; + require "../autoload.php"; $dotenv = new Dotenv\Dotenv(__DIR__); $dotenv->load(); diff --git a/src/Pinterest/Endpoints/Boards.php b/src/Pinterest/Endpoints/Boards.php index c55b5ec..694420e 100644 --- a/src/Pinterest/Endpoints/Boards.php +++ b/src/Pinterest/Endpoints/Boards.php @@ -43,6 +43,21 @@ public function create(array $data) return new Board($this->master, $response); } + /** + * Edit a board + * + * @access public + * @param string $board_id + * @param array $data + * @throws Exceptions/PinterestExceptions + * @return Board + */ + public function edit($board_id, array $data) + { + $response = $this->request->update(sprintf("boards/%s", $board_id), $data); + return new Board($this->master, $response); + } + /** * Delete a board * diff --git a/tests/Pinterest/Endpoints/BoardsTest.php b/tests/Pinterest/Endpoints/BoardsTest.php index b2d3eb6..63e1fff 100644 --- a/tests/Pinterest/Endpoints/BoardsTest.php +++ b/tests/Pinterest/Endpoints/BoardsTest.php @@ -1,9 +1,9 @@ - * + * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ @@ -17,7 +17,7 @@ class BoardsTest extends \PHPUnit_Framework_TestCase{ /** * The Pinterest instance - * + * * @var Pinterest */ private $pinterest; @@ -30,7 +30,7 @@ class BoardsTest extends \PHPUnit_Framework_TestCase{ public function setUp() { $curlbuilder = CurlBuilderMock::create( $this ); - + // Setup Pinterest $this->pinterest = new Pinterest("0", "0", $curlbuilder); $this->pinterest->auth->setOAuthToken( "0" ); @@ -65,6 +65,16 @@ public function testCreate() $this->assertEquals( $response->id , "503066289565421205" ); } + public function testEdit() + { + $response = $this->pinterest->boards->edit("503066289565421201", array( + "name" => "Test board from API" + )); + + $this->assertInstanceOf( "DirkGroenen\Pinterest\Models\Board", $response ); + $this->assertEquals( $response->id , "503066289565421205" ); + } + public function testDelete() { $response = $this->pinterest->boards->delete("503066289565421205"); diff --git a/tests/Pinterest/env b/tests/Pinterest/env new file mode 100644 index 0000000..5c8bfa8 --- /dev/null +++ b/tests/Pinterest/env @@ -0,0 +1,6 @@ +# Note: the API will perform live actions on the provided account + +export CLIENT_ID=4782712381411165558 +export CLIENT_SECRET=4489d440ae5d42f40170bf1f5073c5c9d1ddcf565ab4e120e1d1af180a663024 +export ACCESS_TOKEN=AbRHLxfhh1aBeK80gX98RSuvbCT2FBreHhmUMRpCW5Jw2MAJ6AAAAAA +export CALLBACK_URL=https://bitlabs.dev diff --git a/tests/Pinterest/responses/BoardsTest/testEdit.json b/tests/Pinterest/responses/BoardsTest/testEdit.json new file mode 100644 index 0000000..2221e4d --- /dev/null +++ b/tests/Pinterest/responses/BoardsTest/testEdit.json @@ -0,0 +1,7 @@ +{ + "data": { + "url": "https://www.pinterest.com/dirkgroenen/test-board-from-api/", + "id": "503066289565421205", + "name": "Test Board From API" + } +} \ No newline at end of file From 91d8e0464daaac86fb59d2e4fa623902e54e4812 Mon Sep 17 00:00:00 2001 From: Dirk Groenen Date: Wed, 10 Feb 2016 20:21:38 +0100 Subject: [PATCH 06/15] Add changes --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a2fdbea..a359cb6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +### 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) + ### 0.2.8 (02-02-2016) - Fix Curl execFollow and error handling [#31](https://github.com/dirkgroenen/Pinterest-API-PHP/issues/31) From f8fb1b037608ba19216b49e8023131b32763da56 Mon Sep 17 00:00:00 2001 From: Dirk Groenen Date: Wed, 10 Feb 2016 20:27:51 +0100 Subject: [PATCH 07/15] add formdata to patch request --- src/Pinterest/Transport/Request.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Pinterest/Transport/Request.php b/src/Pinterest/Transport/Request.php index a7f477a..6ce2c3e 100644 --- a/src/Pinterest/Transport/Request.php +++ b/src/Pinterest/Transport/Request.php @@ -186,7 +186,11 @@ public function execute($method, $apiCall, array $parameters = array(), $headers $ch->setOption(CURLOPT_CUSTOMREQUEST, "DELETE"); break; case 'PATCH': - $ch->setOption(CURLOPT_CUSTOMREQUEST, "PATCH"); + $ch->setOptions(array( + CURLOPT_CUSTOMREQUEST => "PATCH", + CURLOPT_POST => count($parameters), + CURLOPT_POSTFIELDS => $parameters + )); break; default: $ch->setOption(CURLOPT_CUSTOMREQUEST, "GET"); From 05fc6cbc5ff53c32854eda3124505a2bddbe4e4a Mon Sep 17 00:00:00 2001 From: Dirk Groenen Date: Wed, 10 Feb 2016 20:41:54 +0100 Subject: [PATCH 08/15] Add fields --- README.md | 8 +++++--- src/Pinterest/Endpoints/Boards.php | 5 +++-- src/Pinterest/Endpoints/Pins.php | 5 +++-- src/Pinterest/Transport/Request.php | 11 +++++++++-- 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 25c1a3f..df213b0 100644 --- a/README.md +++ b/README.md @@ -355,7 +355,7 @@ $pinterest->boards->create(array( Returns: `Board` ### Edit board -`edit( string $board_id, array $data );` +`edit( string $board_id, array $data, array $fields = array() );` ```php $pinterest->boards-edit("dirkgroenen/pinterest-api-test", array( @@ -435,10 +435,12 @@ 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. -`update( string $pin_id, array $data );` +`update( string $pin_id, array $data, array $fields = array() );` ```php -$pinterest->pins->update("181692166190246650"); +$pinterest->pins->update("181692166190246650", array( + "note" => "Updated name" +)); ``` Returns: `Pin` diff --git a/src/Pinterest/Endpoints/Boards.php b/src/Pinterest/Endpoints/Boards.php index 694420e..4eae558 100644 --- a/src/Pinterest/Endpoints/Boards.php +++ b/src/Pinterest/Endpoints/Boards.php @@ -49,12 +49,13 @@ public function create(array $data) * @access public * @param string $board_id * @param array $data + * @param array $fields * @throws Exceptions/PinterestExceptions * @return Board */ - public function edit($board_id, array $data) + public function edit($board_id, array $data, array $fields = array()) { - $response = $this->request->update(sprintf("boards/%s", $board_id), $data); + $response = $this->request->update(sprintf("boards/%s", $board_id), $data, $fields); return new Board($this->master, $response); } diff --git a/src/Pinterest/Endpoints/Pins.php b/src/Pinterest/Endpoints/Pins.php index 44b700c..6905eaf 100644 --- a/src/Pinterest/Endpoints/Pins.php +++ b/src/Pinterest/Endpoints/Pins.php @@ -73,12 +73,13 @@ public function create(array $data) * @access public * @param string $pin_id * @param array $data + * @param array $fields * @throws Exceptions/PinterestExceptions * @return Pin */ - public function update($pin_id, array $data) + public function update($pin_id, array $data, array $fields = array()) { - $response = $this->request->update(sprintf("pins/%s", $pin_id), $data); + $response = $this->request->update(sprintf("pins/%s", $pin_id), $data, $fields); return new Pin($this->master, $response); } diff --git a/src/Pinterest/Transport/Request.php b/src/Pinterest/Transport/Request.php index 6ce2c3e..aad7d16 100644 --- a/src/Pinterest/Transport/Request.php +++ b/src/Pinterest/Transport/Request.php @@ -116,11 +116,18 @@ public function delete($endpoint, array $parameters = array()) * @access public * @param string $endpoint * @param array $parameters + * @param array $queryparameters * @return Response */ - public function update($endpoint, array $parameters = array()) + public function update($endpoint, array $parameters = array(), array $queryparameters = array()) { - return $this->execute("PATCH", sprintf("%s%s", $this->host, $endpoint) . "/", $parameters); + if (!empty($queryparameters)) { + $path = sprintf("%s/?%s", $endpoint, http_build_query($parameters)); + } else { + $path = $endpoint; + } + + return $this->execute("PATCH", sprintf("%s%s", $this->host, $path) . "/", $parameters); } /** From 5a249842428a5c7d1f238cb228b23a8680e830ee Mon Sep 17 00:00:00 2001 From: Dirk Groenen Date: Wed, 10 Feb 2016 21:10:41 +0100 Subject: [PATCH 09/15] Fix src --- autoload.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoload.php b/autoload.php index f108c50..e08a02d 100644 --- a/autoload.php +++ b/autoload.php @@ -15,7 +15,7 @@ $prefix = 'DirkGroenen\\Pinterest\\'; // base directory for the namespace prefix - $base_dir = __DIR__ . '/src/'; + $base_dir = __DIR__ . '/src/Pinterest/'; // does the class use the namespace prefix? $len = strlen($prefix); From f826ceb0caddf291b5a18efe2ac3719ca6ab04c0 Mon Sep 17 00:00:00 2001 From: Dirk Groenen Date: Wed, 10 Feb 2016 21:59:38 +0100 Subject: [PATCH 10/15] try to get patch working, added fields to patch requests --- CHANGELOG.md | 2 ++ README.md | 11 +++++------ demo/boot.php | 2 +- demo/callback.php | 15 +++++++++------ demo/index.php | 2 +- src/Pinterest/Endpoints/Boards.php | 8 +++++--- src/Pinterest/Endpoints/Pins.php | 10 ++++++---- src/Pinterest/Transport/Request.php | 9 ++++----- 8 files changed, 33 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a359cb6..66a01d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ - 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) diff --git a/README.md b/README.md index df213b0..6cb1b1e 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,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(); @@ -355,7 +355,7 @@ $pinterest->boards->create(array( Returns: `Board` ### Edit board -`edit( string $board_id, array $data, array $fields = array() );` +`edit( string $board_id, array $data, string $fields = null );` ```php $pinterest->boards-edit("dirkgroenen/pinterest-api-test", array( @@ -432,13 +432,12 @@ $pinterest->pins->create(array( 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, array $fields = array() );` +`edit( string $pin_id, array $data, string $fields = null );` ```php -$pinterest->pins->update("181692166190246650", array( +$pinterest->pins->edit("181692166190246650", array( "note" => "Updated name" )); ``` diff --git a/demo/boot.php b/demo/boot.php index 7992dae..2c02d9f 100644 --- a/demo/boot.php +++ b/demo/boot.php @@ -3,7 +3,7 @@ ini_set('display_startup_errors', 1); error_reporting(E_ALL); - require "../autoload.php"; + require "../vendor/autoload.php"; $dotenv = new Dotenv\Dotenv(__DIR__); $dotenv->load(); diff --git a/demo/callback.php b/demo/callback.php index 95b3d7d..a05900f 100644 --- a/demo/callback.php +++ b/demo/callback.php @@ -4,13 +4,16 @@ -

Hey you!

-

$pinterest->pins->create()

+ boards->edit("dirkgroenen/test-from-api", array( + "name" => "Test from API - update", + "description" => "Test" + ));*/ - pins->delete("503066220857361559", array( - "note" => "Test from API", - "image_url" => "https://download.unsplash.com/photo-1438216983993-cdcd7dea84ce" - )); ?> + echo $pinterest->pins->edit("503066220857432361", array( + "note" => "Noted update" + ), "id,link,note,url,image"); + ?> \ No newline at end of file diff --git a/demo/index.php b/demo/index.php index 5cfa86e..cbf9e5b 100644 --- a/demo/index.php +++ b/demo/index.php @@ -5,6 +5,6 @@

Login with Pinterest

-

Use this link to login with your Pinterest account.

+

Use this link to login with your Pinterest account.

\ No newline at end of file diff --git a/src/Pinterest/Endpoints/Boards.php b/src/Pinterest/Endpoints/Boards.php index 4eae558..9219a98 100644 --- a/src/Pinterest/Endpoints/Boards.php +++ b/src/Pinterest/Endpoints/Boards.php @@ -49,13 +49,15 @@ public function create(array $data) * @access public * @param string $board_id * @param array $data - * @param array $fields + * @param string $fields * @throws Exceptions/PinterestExceptions * @return Board */ - public function edit($board_id, array $data, array $fields = array()) + public function edit($board_id, array $data, array $fields = null) { - $response = $this->request->update(sprintf("boards/%s", $board_id), $data, $fields); + $query = (!$fields) ? array() : array("fields" => $fields); + + $response = $this->request->update(sprintf("boards/%s", $board_id), $data, $query); return new Board($this->master, $response); } diff --git a/src/Pinterest/Endpoints/Pins.php b/src/Pinterest/Endpoints/Pins.php index 6905eaf..777e691 100644 --- a/src/Pinterest/Endpoints/Pins.php +++ b/src/Pinterest/Endpoints/Pins.php @@ -68,18 +68,20 @@ public function create(array $data) } /** - * Update a pin + * Edit a pin * * @access public * @param string $pin_id * @param array $data - * @param array $fields + * @param string $fields * @throws Exceptions/PinterestExceptions * @return Pin */ - public function update($pin_id, array $data, array $fields = array()) + public function edit($pin_id, array $data, $fields = null) { - $response = $this->request->update(sprintf("pins/%s", $pin_id), $data, $fields); + $query = (!$fields) ? array() : array("fields" => $fields); + + $response = $this->request->update(sprintf("pins/%s", $pin_id), $data, $query); return new Pin($this->master, $response); } diff --git a/src/Pinterest/Transport/Request.php b/src/Pinterest/Transport/Request.php index aad7d16..57ade60 100644 --- a/src/Pinterest/Transport/Request.php +++ b/src/Pinterest/Transport/Request.php @@ -122,12 +122,12 @@ public function delete($endpoint, array $parameters = array()) public function update($endpoint, array $parameters = array(), array $queryparameters = array()) { if (!empty($queryparameters)) { - $path = sprintf("%s/?%s", $endpoint, http_build_query($parameters)); + $path = sprintf("%s/?%s", $endpoint, http_build_query($queryparameters)); } else { $path = $endpoint; } - return $this->execute("PATCH", sprintf("%s%s", $this->host, $path) . "/", $parameters); + return $this->execute("PATCH", sprintf("%s%s", $this->host, $path), $parameters); } /** @@ -156,7 +156,7 @@ public function execute($method, $apiCall, array $parameters = array(), $headers if ($this->access_token != null) { $headers = array_merge($headers, array( "Authorization: Bearer " . $this->access_token, - "Content-type: multipart/form-data", + "Content-type: application/x-www-form-urlencoded; charset=UTF-8", )); } @@ -179,7 +179,7 @@ public function execute($method, $apiCall, array $parameters = array(), $headers switch ($method) { case 'POST': $ch->setOptions(array( - CURLOPT_CUSTOMREQUEST => 'POST', + CURLOPT_CUSTOMREQUEST => "POST", CURLOPT_POST => count($parameters), CURLOPT_POSTFIELDS => $parameters )); @@ -204,7 +204,6 @@ public function execute($method, $apiCall, array $parameters = array(), $headers break; } - // Execute request and catch response $response_data = $ch->execute(); From 764369cd733ace296dfb22c46c1b0b3e0bffe791 Mon Sep 17 00:00:00 2001 From: Dirk Groenen Date: Wed, 10 Feb 2016 22:03:51 +0100 Subject: [PATCH 11/15] fix patch requests --- demo/callback.php | 2 +- src/Pinterest/Transport/Request.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/demo/callback.php b/demo/callback.php index a05900f..4cb1e26 100644 --- a/demo/callback.php +++ b/demo/callback.php @@ -11,7 +11,7 @@ ));*/ echo $pinterest->pins->edit("503066220857432361", array( - "note" => "Noted update" + "note" => "Noted update 2" ), "id,link,note,url,image"); ?> diff --git a/src/Pinterest/Transport/Request.php b/src/Pinterest/Transport/Request.php index 57ade60..dee632e 100644 --- a/src/Pinterest/Transport/Request.php +++ b/src/Pinterest/Transport/Request.php @@ -196,7 +196,7 @@ public function execute($method, $apiCall, array $parameters = array(), $headers $ch->setOptions(array( CURLOPT_CUSTOMREQUEST => "PATCH", CURLOPT_POST => count($parameters), - CURLOPT_POSTFIELDS => $parameters + CURLOPT_POSTFIELDS => http_build_query($parameters) )); break; default: From 29960d399fdd4b4b0683ee561de1678e6d783960 Mon Sep 17 00:00:00 2001 From: Dirk Groenen Date: Wed, 10 Feb 2016 22:10:25 +0100 Subject: [PATCH 12/15] add fields to demo, fix typehint --- demo/callback.php | 6 +++--- src/Pinterest/Endpoints/Boards.php | 2 +- src/Pinterest/Models/Board.php | 10 +++++----- src/Pinterest/Models/Model.php | 1 - 4 files changed, 9 insertions(+), 10 deletions(-) diff --git a/demo/callback.php b/demo/callback.php index 4cb1e26..9df2a87 100644 --- a/demo/callback.php +++ b/demo/callback.php @@ -10,9 +10,9 @@ "description" => "Test" ));*/ - echo $pinterest->pins->edit("503066220857432361", array( - "note" => "Noted update 2" - ), "id,link,note,url,image"); + echo $pinterest->boards->edit("dirkgroenen/test-from-api", array( + "name" => "Noted update 2 - API" + ), "description,creator"); ?> diff --git a/src/Pinterest/Endpoints/Boards.php b/src/Pinterest/Endpoints/Boards.php index 9219a98..d4c1832 100644 --- a/src/Pinterest/Endpoints/Boards.php +++ b/src/Pinterest/Endpoints/Boards.php @@ -53,7 +53,7 @@ public function create(array $data) * @throws Exceptions/PinterestExceptions * @return Board */ - public function edit($board_id, array $data, array $fields = null) + public function edit($board_id, array $data, $fields = null) { $query = (!$fields) ? array() : array("fields" => $fields); diff --git a/src/Pinterest/Models/Board.php b/src/Pinterest/Models/Board.php index 23becb0..0763d54 100644 --- a/src/Pinterest/Models/Board.php +++ b/src/Pinterest/Models/Board.php @@ -1,9 +1,9 @@ - - * + * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ @@ -11,10 +11,10 @@ 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"]; diff --git a/src/Pinterest/Models/Model.php b/src/Pinterest/Models/Model.php index dddcf81..9a33e77 100644 --- a/src/Pinterest/Models/Model.php +++ b/src/Pinterest/Models/Model.php @@ -174,5 +174,4 @@ public function __toString() { return $this->toJson(); } - } \ No newline at end of file From cc60de283461ed21aea3c31ca3c00906570ff951 Mon Sep 17 00:00:00 2001 From: Dirk Groenen Date: Wed, 10 Feb 2016 22:11:13 +0100 Subject: [PATCH 13/15] Add extra demo fields --- demo/callback.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/demo/callback.php b/demo/callback.php index 9df2a87..c735a20 100644 --- a/demo/callback.php +++ b/demo/callback.php @@ -12,7 +12,7 @@ echo $pinterest->boards->edit("dirkgroenen/test-from-api", array( "name" => "Noted update 2 - API" - ), "description,creator"); + ), "id,name,url,description,creator,created_at,counts,image"); ?> From e7059ca8416a7eaa4661ae947be25aa306568a23 Mon Sep 17 00:00:00 2001 From: Dirk Groenen Date: Wed, 10 Feb 2016 22:13:06 +0100 Subject: [PATCH 14/15] add test --- tests/Pinterest/Endpoints/PinsTest.php | 18 ++++++++++++++---- .../Pinterest/responses/PinsTest/testEdit.json | 8 ++++++++ 2 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 tests/Pinterest/responses/PinsTest/testEdit.json diff --git a/tests/Pinterest/Endpoints/PinsTest.php b/tests/Pinterest/Endpoints/PinsTest.php index 91d58ae..6f18526 100644 --- a/tests/Pinterest/Endpoints/PinsTest.php +++ b/tests/Pinterest/Endpoints/PinsTest.php @@ -1,9 +1,9 @@ - * + * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ @@ -17,7 +17,7 @@ class PinsTest extends \PHPUnit_Framework_TestCase{ /** * The Pinterest instance - * + * * @var Pinterest */ private $pinterest; @@ -30,7 +30,7 @@ class PinsTest extends \PHPUnit_Framework_TestCase{ public function setUp() { $curlbuilder = CurlBuilderMock::create( $this ); - + // Setup Pinterest $this->pinterest = new Pinterest("0", "0", $curlbuilder); $this->pinterest->auth->setOAuthToken( "0" ); @@ -64,6 +64,16 @@ public function testCreate() $this->assertEquals( $response->id , "503066220854919983" ); } + public function testEdit() + { + $response = $this->pinterest->pins->edit("503066220854919983", array( + "note" => "Test pin from API wrapper - update" + )); + + $this->assertInstanceOf( "DirkGroenen\Pinterest\Models\Pin", $response ); + $this->assertEquals( $response->id , "503066220854919983" ); + } + public function testDelete() { $response = $this->pinterest->pins->delete("503066220854919983"); diff --git a/tests/Pinterest/responses/PinsTest/testEdit.json b/tests/Pinterest/responses/PinsTest/testEdit.json new file mode 100644 index 0000000..c501531 --- /dev/null +++ b/tests/Pinterest/responses/PinsTest/testEdit.json @@ -0,0 +1,8 @@ +{ + "data": { + "url": "http://pinterest.com/pin/503066220854919983/", + "note": "Test pin from API wrapper - update", + "link": "", + "id": "503066220854919983" + } +} \ No newline at end of file From ab0695915003a51d2adb879c59275dcfed5a4132 Mon Sep 17 00:00:00 2001 From: Scrutinizer Auto-Fixer Date: Wed, 10 Feb 2016 21:13:52 +0000 Subject: [PATCH 15/15] Scrutinizer Auto-Fixes This commit consists of patches automatically generated for this project on https://scrutinizer-ci.com --- autoload.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/autoload.php b/autoload.php index e08a02d..0489259 100644 --- a/autoload.php +++ b/autoload.php @@ -9,7 +9,7 @@ * 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) { +spl_autoload_register(function($class) { // project-specific namespace prefix $prefix = 'DirkGroenen\\Pinterest\\';