diff --git a/CHANGELOG.md b/CHANGELOG.md index a2fdbea..66a01d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/README.md b/README.md index c397964..6e4205b 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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`. @@ -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; @@ -41,9 +42,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 +55,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 +63,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) @@ -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(); @@ -100,7 +101,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 +109,7 @@ $pinterest->users->me(array( )); ``` -The response will now be: +The response will now be: ```json { @@ -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`. @@ -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.** @@ -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); @@ -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` @@ -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` @@ -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` @@ -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" )); ``` @@ -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" )); ``` @@ -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` diff --git a/autoload.php b/autoload.php new file mode 100644 index 0000000..0489259 --- /dev/null +++ b/autoload.php @@ -0,0 +1,39 @@ + -

Hey you!

-

$pinterest->boards->create()

+ boards->edit("dirkgroenen/test-from-api", array( + "name" => "Test from API - update", + "description" => "Test" + ));*/ - 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"); + ?> \ 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 c55b5ec..d4c1832 100644 --- a/src/Pinterest/Endpoints/Boards.php +++ b/src/Pinterest/Endpoints/Boards.php @@ -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 * diff --git a/src/Pinterest/Endpoints/Pins.php b/src/Pinterest/Endpoints/Pins.php index 44b700c..777e691 100644 --- a/src/Pinterest/Endpoints/Pins.php +++ b/src/Pinterest/Endpoints/Pins.php @@ -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); } 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 diff --git a/src/Pinterest/Transport/Request.php b/src/Pinterest/Transport/Request.php index a7f477a..dee632e 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($queryparameters)); + } else { + $path = $endpoint; + } + + return $this->execute("PATCH", sprintf("%s%s", $this->host, $path), $parameters); } /** @@ -149,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", )); } @@ -172,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 )); @@ -186,14 +193,17 @@ 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 => http_build_query($parameters) + )); break; default: $ch->setOption(CURLOPT_CUSTOMREQUEST, "GET"); break; } - // Execute request and catch response $response_data = $ch->execute(); 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/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/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 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