|
1 | 1 | ## Discogs Api |
2 | 2 |
|
3 | 3 | [](http://travis-ci.org/ricbra/php-discogs-api) |
| 4 | +[](https://packagist.org/packages/ricbra/php-discogs-api) |
| 5 | +[](https://packagist.org/packages/ricbra/php-discogs-api) |
| 6 | +[](https://packagist.org/packages/ricbra/php-discogs-api) |
| 7 | +[](https://scrutinizer-ci.com/g/ricbra/php-discogs-api/) |
4 | 8 |
|
5 | | -This library is an PHP 5.3 implementation of the [Discogs API v2.0.](http://www.discogs.com/developers/index.html) |
| 9 | +This library is a PHP 5.4 implementation of the [Discogs API v2.0.](http://www.discogs.com/developers/index.html) |
6 | 10 | The Discogs API is a REST-based interface. By using this library you don't have to worry about communicating with the |
7 | 11 | API: all the hard work has already be done. |
8 | 12 |
|
| 13 | +This API is build upon the shoulders of a giant: [Guzzle 4.0](http://guzzle.readthedocs.org/en/latest/). This is an absolutely awesome library. |
| 14 | + |
9 | 15 | ## License |
10 | 16 | This library is released under the MIT license. See the complete license in the LICENSE file. |
11 | 17 |
|
12 | 18 | ## Installation |
13 | | -Start by [installing composer](http://getcomposer.org/doc/01-basic-usage.md#installation) and finally |
14 | | -[install the dependencies](http://getcomposer.org/doc/01-basic-usage.md#installing-dependencies). |
| 19 | +Start by [installing composer](http://getcomposer.org/doc/01-basic-usage.md#installation). |
| 20 | +Next do: |
| 21 | + |
| 22 | + $ composer require ricbra/php-discogs-api ~1.0.0 |
15 | 23 |
|
16 | 24 | ## Requirements |
17 | | -PHP >=5.3.0 |
| 25 | +PHP >=5.4.0 |
18 | 26 |
|
19 | 27 | ## Usage |
20 | 28 | Creating a new instance is as simple as: |
21 | 29 |
|
22 | 30 | ```php |
23 | 31 | <?php |
24 | | -$service = new \Discogs\Service(); |
| 32 | +$client = Discogs\ClientFactory::factory([]); |
25 | 33 | ``` |
26 | 34 |
|
27 | | -### Perform a search: |
| 35 | +### OAuth |
| 36 | +There a lot of endpoints which require OAuth. Lucky for you using Guzzle this is peanuts. |
28 | 37 |
|
29 | 38 | ```php |
30 | 39 | <?php |
31 | 40 |
|
32 | | -$resultset = $service->search(array( |
33 | | - 'q' => 'Meagashira', |
34 | | - 'label' => 'Enzyme' |
35 | | -)); |
36 | | - |
37 | | -// Total results |
38 | | -echo count($resultset)."\n"; |
39 | | -// Total pages |
40 | | -$pagination = $resultset->getPagination(); |
41 | | -echo count($pagination)."\n"; |
42 | | - |
43 | | -// Fetch all results (use on your own risk, only one request per second allowed) |
44 | | -do { |
45 | | - $pagination = $resultset->getPagination(); |
46 | | - echo $pagination->getPage().'<br />'; |
47 | | - foreach ($resultset as $result) { |
48 | | - echo get_class($result).'<br />'; |
49 | | - } |
50 | | -} while($resultset = $service->next($resultset)); |
| 41 | +$client = Discogs\ClientFactory::factory([]); |
| 42 | +$oauth = new GuzzleHttp\Subscriber\Oauth\Oauth1([ |
| 43 | + 'consumer_key' => $consumerKey, // from Discogs developer page |
| 44 | + 'consumer_secret' => $consumerSecret, // from Discogs developer page |
| 45 | + 'token' => $token['oauth_token'], // get this using a OAuth library |
| 46 | + 'token_secret' => $token['oauth_token_secret'] // get this using a OAuth library |
| 47 | +]); |
| 48 | +$client->getHttpClient()->getEmitter()->attach($oauth); |
| 49 | + |
| 50 | +$response = $client->search([ |
| 51 | + 'q' => 'searchstring' |
| 52 | +]); |
51 | 53 | ``` |
52 | 54 |
|
53 | | -### Get information about a label: |
| 55 | +### History |
| 56 | +Another cool plugin is the History plugin: |
54 | 57 |
|
55 | 58 | ```php |
56 | 59 | <?php |
57 | 60 |
|
58 | | -$label = $service->getLabel(1); |
| 61 | +$client = Discogs\ClientFactory::factory([]); |
| 62 | +$history = new GuzzleHttp\Subscriber\History(); |
| 63 | +$client->getHttpClient()->getEmitter()->attach($history); |
| 64 | + |
| 65 | +$response = $client->search([ |
| 66 | + 'q' => 'searchstring' |
| 67 | +]); |
| 68 | + |
| 69 | +foreach ($history as $row) { |
| 70 | + print (string) $row['request']; |
| 71 | + print (string) $row['response']; |
| 72 | +} |
59 | 73 |
|
60 | | -echo $label->getName()."\n"; |
61 | 74 | ``` |
62 | 75 |
|
63 | | -### Get information about an artist: |
| 76 | +### More info and plugins |
| 77 | +For more information about Guzzle and its plugins checkout [the docs.](http://guzzle.readthedocs.org/en/latest/) |
| 78 | + |
| 79 | +### Perform a search: |
| 80 | +Per august 2014 an signed OAuth request is required for this endpoint. |
64 | 81 |
|
65 | 82 | ```php |
66 | 83 | <?php |
67 | 84 |
|
68 | | -$artist = $service->getArtist(1); |
| 85 | +$response = $client->search([ |
| 86 | + 'q' => 'Meagashira' |
| 87 | +]); |
| 88 | +// Loop through results |
| 89 | +foreach ($response['results'] as $result) { |
| 90 | + var_dump($result['title']); |
| 91 | +} |
| 92 | +// Pagination data |
| 93 | +var_dump($response['pagination']); |
| 94 | + |
| 95 | +// Dump all data |
| 96 | +var_dump($response->toArray()); |
69 | 97 |
|
70 | | -echo $artist->getName()."\n"; |
71 | 98 | ``` |
72 | 99 |
|
73 | | -### Get information about a release: |
| 100 | +### Get information about a label: |
74 | 101 |
|
75 | 102 | ```php |
76 | 103 | <?php |
77 | 104 |
|
78 | | -$release = $service->getRelease(1); |
| 105 | +$label = $service->getLabel([ |
| 106 | + 'id' => 1 |
| 107 | +]); |
79 | 108 |
|
80 | | -echo $release->getTitle()."\n"; |
81 | 109 | ``` |
82 | 110 |
|
83 | | -### Get information about a master release: |
| 111 | +### Get information about an artist: |
84 | 112 |
|
85 | 113 | ```php |
86 | 114 | <?php |
87 | 115 |
|
88 | | -$master = $service->getMaster(1); |
| 116 | +$artist = $service->getArtist([ |
| 117 | + 'id' => 1 |
| 118 | +]); |
89 | 119 |
|
90 | | -echo $master->getTitle()."\n"; |
91 | 120 | ``` |
92 | 121 |
|
93 | | -### Response transformation |
| 122 | +### Get information about a release: |
94 | 123 |
|
95 | | -You have two options in which form to receive formatted response: as object using supplied models, or as plain array. |
96 | | -By default (if nothing has been set via setter) Model response transformer is chosen. You can manipulate it via |
97 | | -the `setResponseTransfomer` setter: |
| 124 | +```php |
| 125 | +<?php |
98 | 126 |
|
99 | | -``` php |
100 | | -$discogs->setResponseTransformer(new \Discogs\ResponseTransformer\Model()); |
101 | | -// or |
102 | | -$discogs->setResponseTransformer(new \Discogs\ResponseTransformer\Hash()); |
| 127 | +$release = $service->getRelease([ |
| 128 | + 'id' => 1 |
| 129 | +]); |
| 130 | + |
| 131 | +echo $release['title']."\n"; |
103 | 132 | ``` |
104 | 133 |
|
105 | | -You can also set your own response transformer which need to implement the `ResponseTransformerInterface` |
| 134 | +### Get information about a master release: |
106 | 135 |
|
107 | | -**NOTE** At this moment only the "Database" resource has been implemented. The "Marketplace" and "User" are missing. |
| 136 | +```php |
| 137 | +<?php |
| 138 | + |
| 139 | +$master = $service->getMaster([ |
| 140 | + 'id' => 1 |
| 141 | +]); |
| 142 | + |
| 143 | +echo $master['title']."\n"; |
| 144 | +``` |
108 | 145 |
|
109 | 146 | ## Documentation |
110 | 147 | Further documentation can be found at the [Discogs API v2.0 Documentation](http://www.discogs.com/developers/index.html). |
111 | 148 |
|
| 149 | +## Contributing |
| 150 | +Implemented a missing call? PR's are welcome! |
| 151 | + |
| 152 | + |
112 | 153 | [](https://bitdeli.com/free "Bitdeli Badge") |
113 | 154 |
|
0 commit comments