Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace guzzle with HTTPlug #65

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ composer.lock
/tests/instagram_test.log
/instagram_test.log
/tests/src/Instaphp/Instagram/instagram_test.log
.php_cs.cache
56 changes: 56 additions & 0 deletions .php_cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php
$header = <<<'EOF'
The MIT License (MIT)
Copyright © 2013 Randy Sesser <randy@instaphp.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
@author Randy Sesser <randy@instaphp.com>
@filesource
EOF;

return PhpCsFixer\Config::create()
->setRiskyAllowed(true)
->setRules(array(
'@Symfony' => true,
'@Symfony:risky' => true,
'array_syntax' => array('syntax' => 'short'),
'combine_consecutive_unsets' => true,
// one should use PHPUnit methods to set up expected exception instead of annotations
//'general_phpdoc_annotation_remove' => array('expectedException', 'expectedExceptionMessage', 'expectedExceptionMessageRegExp'),
'header_comment' => array('header' => $header),
'heredoc_to_nowdoc' => true,
'no_extra_consecutive_blank_lines' => array('break', 'continue', 'extra', 'return', 'throw', 'use', 'parenthesis_brace_block', 'square_brace_block', 'curly_brace_block'),
'no_unreachable_default_argument_value' => true,
'no_useless_else' => true,
'no_useless_return' => true,
'ordered_class_elements' => true,
'ordered_imports' => true,
'php_unit_strict' => true,
'php_unit_fqcn_annotation' => false,
'phpdoc_add_missing_param_annotation' => true,
'phpdoc_order' => true,
'psr4' => true,
//'strict_comparison' => true,
//'strict_param' => true,
'phpdoc_to_comment' => false,
'binary_operator_spaces' => array('align_double_arrow' => true, 'align_equals' => true),
))
->setFinder(
PhpCsFixer\Finder::create()
->exclude('tests/Fixtures')
->in(__DIR__)
)
;
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
git:
depth: 1
language: php
php:
- 5.3
Expand Down
87 changes: 76 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,52 @@
## Instaphp V2 ##

This is version 2 of Instaphp. It's a complete rewrite from version 1 and is not backwards compatible. If you're using v1 and want to update to v2, you'll have to make a few changes. Some of the method names have changed and configuration is no longer an XML file. There are unit tests, but given the less than stellar reliability of Instagram's API, they are fairly useless.
## Upgrade Notes ##

If you're using composer, you shouldn't need to worry about dependencies. If you're not, you will have to figure out the include chain. The new version relies on [GuzzleHttp][3] and [MonoLog][4].
The latest version uses HTTPlug so that you can supply your own HTTP client layer. If you want to use Guzzle 6 or whether you have to use Guzzle 5 because other libraries require it, the HTTPlug library provides an abstraction between Instaphp and your own HTTP libary.

[1]: https://github.com/sesser/Scurl
[2]: https://github.com/sesser/Scurl/blob/master/README.md
[3]: http://docs.guzzlephp.org/en/latest/
[4]: https://github.com/Seldaek/monolog
You will need to install one of the client libraries yourself http://docs.php-http.org/en/latest/clients.html

```
composer.phar require php-http/curl-client guzzlehttp/psr7
```

BC Breaks:

Unfortunately there are some BC breaks, specifically around the HTTP client options. `http_timeout`, `verify` and `http_connect_timeout` are now deprecated and should be set on the client yourself (see below).

The `event.before`, `event.error` and `event.after` callable functions now take the parameters `Psr\Http\Message\RequestInterface`, ``\Exception` and `Psr\Http\Message\ResponseInterface` as arguments instead of the Guzzle equivalents. They need to return the same objects for chaining.

It's not battle tested, so I can't speak to it's reliability/speed/ease of use, but the unit test (generally) all pass. I will keep this in the development branch for a while until I think it's ready to move into master.

The example below adds a custom log function to the `event.error` event

```php
$api = new Instaphp\Instaphp([
'client_id' => 'your client id',
'client_secret' => 'your client secret',
'redirect_uri' => 'http://somehost.foo/callback.php',
'scope' => 'comments+likes',
'event.error' => function(\Exception $e) {
error_log($e->getMessage());
return $e;
}
]);
```

## Usage ##

Installing should be done through [composer](https://getcomposer.org/). If you want to use [Guzzle 6](http://docs.guzzlephp.org/en/stable/), use the command below

```
composer.phar require instaphp/instaphp php-http/guzzle6-adapter guzzlehttp/psr7
```

And it should start using Guzzle 6

Here's a basic example showing how to get 10 popular posts...

``` php
<?php

$api = new Instaphp\Instaphp([
'client_id' => 'your client id',
'client_secret' => 'your client secret',
Expand All @@ -34,9 +63,12 @@ Here's a basic example showing how to get 10 popular posts...
}
?>
```

Important: This won't work out of the box. Instagram have strict policies about who can use their API and you need to read up about pre-populating the data using [a sandbox account](https://www.instagram.com/developer/sandbox/) before diving into your project.

### Configuration ###

Configuration is now a simple `array` of key/value pairs. The absolute minimum required setting is `client_id`, but if you plan to allow users to login via OAuth, you'll need `client_secret` & `redirect_uri`. All the other settings are optional and/or have sensible defaults.
Configuration is a simple `array` of key/value pairs. The absolute minimum required setting is `client_id`, but if you plan to allow users to login via OAuth, you'll need `client_secret` & `redirect_uri`. All the other settings are optional and/or have sensible defaults.

Key|Default Value|Description
:--|:-----------:|:----------------
Expand All @@ -48,9 +80,42 @@ log_enabled|FALSE|Enable logging
log_level|DEBUG|Log level. See [Monolog Logger](https://github.com/Seldaek/monolog#log-levels)
log_path|./instaphp.log|Where the log file lives
http_useragent|Instaphp/2.0; cURL/{curl_version}; (+http://instaphp.com)|The user-agent string sent with all requests
http_timeout|6|Timeout for requests to the API.
http_connect_timeout|2|Timeout for http connect
debug|FALSE|Debug mode?
event.before|Empty|Callback called prior to sending the request to the API. Method takes a single parameter [BeforeEvent](http://docs.guzzlephp.org/en/latest/events.html#before)
event.after|Empty|Callback called after a response is received from the API. Method takes a single parameter of [CompleteEvent](http://docs.guzzlephp.org/en/latest/events.html#complete)
event.error|Empty|Callback called when an error response is received from the API. Method takes a single parameter of [ErrorEvent](http://docs.guzzlephp.org/en/latest/events.html#error).

### Configuring Guzzle ###

Your choice of HTTP client is automatically discovered using HTTPlug's [Discovery classes](http://docs.php-http.org/en/latest/discovery.html). If you need to edit the settings of your client-specific configuration you can inject the adapter into the Instaphp class

First, install the adapter

`composer require php-http/guzzle6-adapter`

Then configure the client and wrap it in the adapter

``` php
<?php

use GuzzleHttp\Client as GuzzleClient;
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;

$guzzleConfig = [
'http_timeout' => 6,
'http_connect_timeout' => 2,
'verify' => false,
];
$guzzleClient = new GuzzleClient($guzzleConfig);

$adapter = new GuzzleAdapter($guzzleClient);

$api = new Instaphp\Instaphp([
'client_id' => 'your client id',
'client_secret' => 'your client secret',
'redirect_uri' => 'http://somehost.foo/callback.php',
'scope' => 'comments+likes'
], $adapter);

?>
```
24 changes: 19 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,30 @@
],
"require": {
"php": ">=5.4.0",
"guzzlehttp/guzzle": "~5.2",
"guzzlehttp/log-subscriber": "~1",
"monolog/monolog": "~1.8"
"monolog/monolog": "~1.8",
"psr/http-message": "^1.0",
"php-http/client-implementation": "^1.0",
"php-http/httplug": "^1.0",
"php-http/discovery": "^1.0",
"php-http/logger-plugin": "^1.0",
"php-http/client-common": "^1.5",
"php-http/message-factory": "^1.0",
"php-http/message": "^1.5"
},
"require-dev": {
"phpunit/phpunit": "~3.7"
"phpunit/phpunit": "~3.7",
"php-http/mock-client": "^1.0",
"php-http/curl-client": "^1.7",
"guzzlehttp/psr7": "^1.4",
"php-http/guzzle6-adapter": "^1.1",
"friendsofphp/php-cs-fixer": "^2.3"
},
"suggest": {
"php-http/guzzle6-adapter": "Allows you to configure the Guzzle connection options"
},
"autoload": {
"psr-0": {
"Instaphp": "src/"
}
}
}
}
20 changes: 8 additions & 12 deletions src/Instaphp/Exceptions/APIAgeGatedError.php
Original file line number Diff line number Diff line change
@@ -1,43 +1,39 @@
<?php

/**
/*
* The MIT License (MIT)
* Copyright © 2013 Randy Sesser <randy@instaphp.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the “Software”), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author Randy Sesser <randy@instaphp.com>
* @filesource
*/

namespace Instaphp\Exceptions;

/**
* APIAgeGatedError
*
* APIAgeGatedError.
*
* Exception thrown when user cannot view some resource due to unclear reasons
*
* @author Randy Sesser <randy@instaphp.com>
* @license http://instaphp.mit-license.org MIT License
* @package Instaphp
* @subpackage Exceptions
* @license http://instaphp.mit-license.org MIT License
*
* @version 2.0-dev
*/
class APIAgeGatedError extends InstagramException { }

class APIAgeGatedError extends InstagramException
{
}
20 changes: 8 additions & 12 deletions src/Instaphp/Exceptions/APIInvalidParametersError.php
Original file line number Diff line number Diff line change
@@ -1,43 +1,39 @@
<?php

/**
/*
* The MIT License (MIT)
* Copyright © 2013 Randy Sesser <randy@instaphp.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the “Software”), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author Randy Sesser <randy@instaphp.com>
* @filesource
*/

namespace Instaphp\Exceptions;

/**
* APIInvalidParametersError
*
* APIInvalidParametersError.
*
* Exception thrown when Instagram responds with this error_type
*
* @author Randy Sesser <randy@instaphp.com>
* @license http://instaphp.mit-license.org MIT License
* @package Instaphp
* @subpackage Exceptions
* @license http://instaphp.mit-license.org MIT License
*
* @version 2.0-dev
*/
class APIInvalidParametersError extends InstagramException { }

class APIInvalidParametersError extends InstagramException
{
}
20 changes: 8 additions & 12 deletions src/Instaphp/Exceptions/APINotAllowedError.php
Original file line number Diff line number Diff line change
@@ -1,43 +1,39 @@
<?php

/**
/*
* The MIT License (MIT)
* Copyright © 2013 Randy Sesser <randy@instaphp.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the “Software”), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author Randy Sesser <randy@instaphp.com>
* @filesource
*/

namespace Instaphp\Exceptions;

/**
* APINotAllowedError
*
* APINotAllowedError.
*
* Exception thrown when Instagram responds with this error_type
*
* @author Randy Sesser <randy@instaphp.com>
* @license http://instaphp.mit-license.org MIT License
* @package Instaphp
* @subpackage Exceptions
* @license http://instaphp.mit-license.org MIT License
*
* @version 2.0-dev
*/
class APINotAllowedError extends InstagramException { }

class APINotAllowedError extends InstagramException
{
}
Loading