Skip to content

Commit 6817df4

Browse files
Merge pull request #20 from stevenmaguire/v3-fusion-support
Add V3 (Fusion) support - Testing and Feedback Wanted
2 parents 3bf3f8e + a061943 commit 6817df4

31 files changed

+2069
-612
lines changed

.travis.yml

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,35 @@
11
language: php
22

3-
php:
4-
- 5.5
5-
- 5.6
6-
- 7.0
7-
- hhvm
3+
matrix:
4+
include:
5+
- php: 5.6
6+
- php: 7.0
7+
- php: 7.1
8+
- php: nightly
9+
- php: hhvm-3.6
10+
sudo: required
11+
dist: trusty
12+
group: edge
13+
- php: hhvm-3.9
14+
sudo: required
15+
dist: trusty
16+
group: edge
17+
- php: hhvm-3.12
18+
sudo: required
19+
dist: trusty
20+
group: edge
21+
- php: hhvm-3.15
22+
sudo: required
23+
dist: trusty
24+
group: edge
25+
- php: hhvm-nightly
26+
sudo: required
27+
dist: trusty
28+
group: edge
29+
fast_finish: true
30+
allow_failures:
31+
- php: nightly
32+
- php: hhvm-nightly
833

934
before_script:
1035
- travis_retry composer self-update

API-GUIDE-v2.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Yelp PHP Client - v2
2+
3+
## Create client explicitly
4+
5+
```php
6+
$client = new \Stevenmaguire\Yelp\v2\Client(array(
7+
'consumerKey' => 'YOUR COSUMER KEY',
8+
'consumerSecret' => 'YOUR CONSUMER SECRET',
9+
'token' => 'YOUR TOKEN',
10+
'tokenSecret' => 'YOUR TOKEN SECRET',
11+
'apiHost' => 'api.yelp.com' // Optional, default 'api.yelp.com'
12+
));
13+
```
14+
15+
## Search by keyword and location
16+
17+
```php
18+
$results = $client->search(array('term' => 'Sushi', 'location' => 'Chicago, IL'));
19+
```
20+
21+
## Search by phone number
22+
23+
```php
24+
$results = $client->searchByPhone(array('phone' => '867-5309'));
25+
```
26+
27+
## Locate details for a specific business by Yelp business id
28+
29+
```php
30+
$business = $client->getBusiness('union-chicago-3');
31+
```
32+
33+
You may include [action links](http://engineeringblog.yelp.com/2015/07/yelp-api-now-returns-action-links.html) in your results by passing additional parameters with your request.
34+
35+
```php
36+
$resultsWithActionLinks = $client->getBusiness('union-chicago-3', [
37+
'actionLinks' => true
38+
]);
39+
```
40+
41+
## Configure defaults
42+
43+
```php
44+
$client->setDefaultLocation('Chicago, IL') // default location for all searches if location not provided
45+
->setDefaultTerm('Sushi') // default keyword for all searches if term not provided
46+
->setSearchLimit(20); // number of records to return
47+
```
48+
49+
## Exceptions
50+
51+
If the API request results in an Http error, the client will throw a `\Stevenmaguire\Yelp\Exception\HttpException` that includes the response body, as a string, from the Yelp API.
52+
53+
```php
54+
try {
55+
$business = $client->getBusiness('union-chicago-3');
56+
} catch (\Stevenmaguire\Yelp\Exception\HttpException $e) {
57+
$responseBody = $e->getResponseBody(); // string from Http request
58+
$responseBodyObject = json_decode($responseBody);
59+
}
60+
```

API-GUIDE-v3.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# Yelp PHP Client - v3 (Fusion)
2+
3+
## Create client explicitly
4+
5+
Yelp API version 3 (Fusion) [requires an OAuth2 access token to authenticate each request](https://www.yelp.com/developers/documentation/v3/authentication). The [oauth2-yelp](https://github.com/stevenmaguire/oauth2-yelp) is available to help obtain an access token.
6+
7+
```php
8+
// Get access token via oauth2-yelp library
9+
$provider = new \Stevenmaguire\OAuth2\Client\Provider\Yelp([
10+
'clientId' => '{yelp-client-id}',
11+
'clientSecret' => '{yelp-client-secret}'
12+
]);
13+
$accessToken = (string) $provider->getAccessToken('client_credentials');
14+
15+
// Provide the access token to the yelp-php client
16+
$client = new \Stevenmaguire\Yelp\v3\Client(array(
17+
'accessToken' => $accessToken,
18+
'apiHost' => 'api.yelp.com' // Optional, default 'api.yelp.com'
19+
));
20+
```
21+
22+
## Search for businesses
23+
24+
See also [https://www.yelp.com/developers/documentation/v3/business_search](https://www.yelp.com/developers/documentation/v3/business_search)
25+
26+
```php
27+
$parameters = [
28+
'term' => 'bars',
29+
'location' => 'Chicago, IL',
30+
'latitude' => 41.8781,
31+
'longitude' => 87.6298,
32+
'radius' => 10,
33+
'categories' => ['bars', 'french'],
34+
'locale' => 'en_US',
35+
'limit' => 10,
36+
'offset' => 2,
37+
'sort_by' => 'best_match',
38+
'price' => '1,2,3',
39+
'open_now' => true,
40+
'open_at' => 1234566,
41+
'attributes' => ['hot_and_new','deals']
42+
];
43+
44+
$results = $client->getBusinessesSearchResults($parameters);
45+
```
46+
47+
## Search for businesses by phone number
48+
49+
See also [https://www.yelp.com/developers/documentation/v3/business_search_phone](https://www.yelp.com/developers/documentation/v3/business_search_phone)
50+
51+
```php
52+
$results = $client->getBusinessesSearchResultsByPhone('312-867-5309');
53+
```
54+
55+
## Retrieve details for a specific business by Yelp business id
56+
57+
See also [https://www.yelp.com/developers/documentation/v3/business](https://www.yelp.com/developers/documentation/v3/business)
58+
59+
```php
60+
$parameters = [
61+
'locale' => 'en_US',
62+
];
63+
64+
$business = $client->getBusiness('the-motel-bar-chicago', $parameters);
65+
```
66+
67+
## Retrieve reviews for a specific business by Yelp business id
68+
69+
See also [https://www.yelp.com/developers/documentation/v3/business_reviews](https://www.yelp.com/developers/documentation/v3/business_reviews)
70+
71+
```php
72+
$parameters = [
73+
'locale' => 'en_US',
74+
];
75+
76+
$reviews = $client->getBusinessReviews('the-motel-bar-chicago', $parameters);
77+
```
78+
79+
## Retrieve autocomplete suggestions
80+
81+
See also [https://www.yelp.com/developers/documentation/v3/autocomplete](https://www.yelp.com/developers/documentation/v3/autocomplete)
82+
83+
```php
84+
$parameters = [
85+
'text' => 'Mot',
86+
'latitude' => 41.8781,
87+
'longitude' => 87.6298,
88+
'locale' => 'en_US'
89+
];
90+
91+
$results = $client->getAutocompleteResults($parameters);
92+
```
93+
94+
## Search for transactions by type
95+
96+
See also [https://www.yelp.com/developers/documentation/v3/transactions_search](https://www.yelp.com/developers/documentation/v3/transactions_search)
97+
98+
```php
99+
$parameters = [
100+
'latitude' => 41.8781,
101+
'longitude' => 87.6298,
102+
'location' => 'Chicago, IL'
103+
];
104+
105+
$results = $client->getTransactionsSearchResultsByType('delivery', $parameters);
106+
```
107+
108+
## Exceptions
109+
110+
If the API request results in an Http error, the client will throw a `Stevenmaguire\Yelp\Exception\HttpException` that includes the response body, as a string, from the Yelp API.
111+
112+
```php
113+
try {
114+
$business = $client->getBusiness('the-motel-bar-chicago');
115+
} catch (\Stevenmaguire\Yelp\Exception\HttpException $e) {
116+
$responseBody = $e->getResponseBody(); // string from Http request
117+
$responseBodyObject = json_decode($responseBody);
118+
}
119+
```

CONTRIBUTING.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,14 @@ We accept contributions via Pull Requests on [Github](https://github.com/stevenm
2525
## Running Tests
2626

2727
``` bash
28-
$ phpunit
28+
$ ./vendor/bin/phpunit
29+
```
30+
31+
32+
## Running PHP Code Sniffer
33+
34+
``` bash
35+
$ ./vendor/bin/phpcs src --standard=psr2 -sp
2936
```
3037

3138

README.md

Lines changed: 82 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
[![Quality Score](https://img.shields.io/scrutinizer/g/stevenmaguire/yelp-php.svg?style=flat-square)](https://scrutinizer-ci.com/g/stevenmaguire/yelp-php)
88
[![Total Downloads](https://img.shields.io/packagist/dt/stevenmaguire/yelp-php.svg?style=flat-square)](https://packagist.org/packages/stevenmaguire/yelp-php)
99

10-
A PHP client for authenticating with Yelp using OAuth 1 and consuming the search API.
10+
A PHP client for authenticating with Yelp using OAuth and consuming the API.
1111

1212
## Install
1313

@@ -19,65 +19,116 @@ $ composer require stevenmaguire/yelp-php
1919

2020
## Usage
2121

22+
This package currently supports `v2` and `v3` (Fusion) of the Yelp API. Each version of the Yelp API maps to a different client, as the APIs are very different. Each client has separate documentation; links provided below.
23+
2224
### Create client
2325

24-
```php
25-
$client = new Stevenmaguire\Yelp\Client(array(
26-
'consumerKey' => 'YOUR COSUMER KEY',
27-
'consumerSecret' => 'YOUR CONSUMER SECRET',
28-
'token' => 'YOUR TOKEN',
29-
'tokenSecret' => 'YOUR TOKEN SECRET',
30-
'apiHost' => 'api.yelp.com' // Optional, default 'api.yelp.com'
31-
));
32-
```
26+
Each version of the Yelp API maps to a different client, as the APIs are very different. A client factory is available to create appropriate clients.
3327

34-
### Search by keyword and location
28+
#### v2 Client Example
3529

3630
```php
37-
$results = $client->search(array('term' => 'Sushi', 'location' => 'Chicago, IL'));
31+
$options = array(
32+
'consumerKey' => 'YOUR COSUMER KEY',
33+
'consumerSecret' => 'YOUR CONSUMER SECRET',
34+
'token' => 'YOUR TOKEN',
35+
'tokenSecret' => 'YOUR TOKEN SECRET',
36+
'apiHost' => 'api.yelp.com' // Optional, default 'api.yelp.com'
37+
);
38+
39+
$client = \Stevenmaguire\Yelp\ClientFactory::makeWith(
40+
$options,
41+
\Stevenmaguire\Yelp\Version::TWO
42+
);
3843
```
3944

40-
### Search by phone number
45+
#### v3 Client Example
4146

4247
```php
43-
$results = $client->searchByPhone(array('phone' => '867-5309'));
48+
$options = array(
49+
'accessToken' => 'YOUR ACCESS TOKEN',
50+
'apiHost' => 'api.yelp.com' // Optional, default 'api.yelp.com'
51+
);
52+
53+
$client = \Stevenmaguire\Yelp\ClientFactory::makeWith(
54+
$options,
55+
\Stevenmaguire\Yelp\Version::THREE
56+
);
4457
```
4558

46-
### Locate details for a specific business by Yelp business id
59+
Version | Constant | Documentation
60+
--------|----------|--------------
61+
v2 | `Stevenmaguire\Yelp\Version::TWO` | [API-GUIDE-v2.md](API-GUIDE-v2.md)
62+
v3 | `Stevenmaguire\Yelp\Version::THREE` | [API-GUIDE-v3.md](API-GUIDE-v3.md)
63+
64+
### Exceptions
65+
66+
If the API request results in an Http error, the client will throw a `Stevenmaguire\Yelp\Exception\HttpException` that includes the response body, as a string, from the Yelp API.
4767

4868
```php
49-
$results = $client->getBusiness('union-chicago-3');
69+
$responseBody = $e->getResponseBody(); // string from Http request
70+
$responseBodyObject = json_decode($responseBody);
5071
```
5172

52-
You may include [action links](http://engineeringblog.yelp.com/2015/07/yelp-api-now-returns-action-links.html) in your results by passing additional parameters with your request.
73+
### Advanced usage
74+
75+
Both the [v3 client](API-GUIDE-v3.md) and the [v2 client](API-GUIDE-v2.md) expose some public methods that allow overiding default behavior by providing alternative HTTP clients and requests.
5376

5477
```php
55-
$resultsWithActionLinks = $client->getBusiness('union-chicago-3', [
56-
'actionLinks' => true
78+
$client = new \Stevenmaguire\Yelp\v3\Client(array(
79+
'accessToken' => $accessToken,
80+
));
81+
82+
// Create a new guzzle http client
83+
$specialHttpClient = new \GuzzleHttp\Client([
84+
// ... some special configuration
5785
]);
58-
```
5986

60-
### Configure defaults
87+
// Update the yelp client with the new guzzle http client
88+
// then get business data
89+
$business = $client->setHttpClient($specialHttpClient)
90+
->getBusiness('the-motel-bar-chicago');
6191

62-
```php
63-
$client->setDefaultLocation('Chicago, IL') // default location for all searches if location not provided
64-
->setDefaultTerm('Sushi') // default keyword for all searches if term not provided
65-
->setSearchLimit(20); // number of records to return
66-
```
92+
// Create request for other yelp API resource not supported by yelp-php
93+
$request = $client->getRequest('GET', '/v3/some-future-endpoint');
6794

68-
### Exceptions
95+
// Send that request
96+
$response = $client->getResponse($request);
6997

70-
If the API request results in an Http error, the client will throw a `Stevenmaguire\Yelp\Exception` that includes the response body, as a string, from the Yelp API.
98+
// See the contents
99+
echo $response->getBody();
100+
```
101+
102+
## Upgrading with Yelp API v2 support from `yelp-php 1.x` to `yelp-php 2.x`
71103

72104
```php
73-
$responseBody = $e->getResponseBody(); // string from Http request
74-
$responseBodyObject = json_decode($responseBody);
105+
// same options for all
106+
$options = array(
107+
'consumerKey' => 'YOUR COSUMER KEY',
108+
'consumerSecret' => 'YOUR CONSUMER SECRET',
109+
'token' => 'YOUR TOKEN',
110+
'tokenSecret' => 'YOUR TOKEN SECRET',
111+
'apiHost' => 'api.yelp.com' // Optional, default 'api.yelp.com'
112+
);
113+
114+
115+
// yelp-php 1.x
116+
$client = new Stevenmaguire\Yelp\Client($options);
117+
118+
// yelp-php 2.x - option 1
119+
$client = \Stevenmaguire\Yelp\ClientFactory::makeWith(
120+
$options,
121+
\Stevenmaguire\Yelp\Version::TWO
122+
);
123+
124+
// yelp-php 2.x - option 2
125+
$client = new \Stevenmaguire\Yelp\v2\Client($options);
75126
```
76127

77128
## Testing
78129

79130
``` bash
80-
$ phpunit
131+
$ ./vendor/bin/phpunit
81132
```
82133

83134
## Contributing

0 commit comments

Comments
 (0)