grzgajda / comicvine-api
PHP类包装器,提供简单灵活的方法来使用ComicVine API。
Requires
- guzzlehttp/guzzle: ^6.1
Requires (Dev)
- mockery/mockery: ^0.9.4
- phpunit/phpunit: 4.8.*
This package is not auto-updated.
Last update: 2023-04-01 10:21:41 UTC
README
作者: Grzegorz Gajda grz.gajda@outlook.com
更新 v1.1.1
当字符串包含空格字符时,过滤等参数的参数没有工作。在版本1.1.1中,这个问题已修复。此外,98%的代码已覆盖(目录 tests
)。
更新 v1.1.0
如果您不想使用CURLConnection
类,我添加了与guzzlehttp/guzzle包的内置集成。从版本1.1.0开始,HTTP连接的默认提供者是GuzzleConnection
类。
基本用法
首先,我们需要创建一个RegisterKey
对象的实例以验证我们的密钥。我们可以使用ComicVine
静态类来创建该对象。
$apiKey = ComicVine::makeApiKey('YOUR_KEY');
接下来,我们需要将密钥和连接注册到ComicVine
类。默认情况下,方法register
使用内置的CURLConnection
类来建立连接。然后,您不需要第二个参数。
ComicVine::register($instanceOfRegisterKey, $instanceOfConnection = null)
注册后,我们可以创建查询以访问ComicVine API。但默认情况下,响应将以XML格式返回。我们可以通过创建ResponseFormat
类的实例来更改响应格式。
$responseFormat = ComicVine::createFormat('json');
然后使用查询(我们想要找到蝙蝠侠的url)
$response = ComicVine::getCharacters() ->setFormat($responseFormat) ->setFilters(['name' => 'Batman'] ->setLimit(1) ->setFieldList(['api_detail_url']) ->getResponse();
现在,变量$response
保存了从ComicVine API获得的JSON响应。
请求
实现资源列表
URL | 方法 | 实现? |
---|---|---|
/character | none | false |
/characters | getCharacters | true |
/chat | none | false |
/chats | getChats | true |
/concept | none | false |
/concepts | getConcepts | true |
/episode | none | false |
/episodes | getEpisodes | true |
/issue | none | false |
/issues | getIssues | true |
/location | none | false |
/locations | getLocations | true |
/movie | none | false |
/movies | getMovies | true |
/object | none | false |
/objects | getObjects | true |
/origin | none | false |
/origins | getOrigins | true |
/person | none | false |
/people | getPeople | true |
/power | none | false |
/powers | getPowers | true |
/promo | none | false |
/promos | getPromos | true |
/publisher | none | false |
/publishers | getPublishers | true |
/series | none | false |
/series_list | getSeriesList | true |
/search | getSearch | false |
/story_arc | none | false |
/story_arcs | getStoryArcs | true |
/team | none | false |
/teams | getTeams | true |
/video | none | false |
/videos | getVideos | false |
/video_type | none | false |
/video_types | getVideoTypes | true |
/volume | none | false |
/volumes | getVolumes | true |
通过调用每个get
方法创建一个ControllerQuery
类的新实例。 ControllerQuery
类允许我们
/* * Argument for setFieldList is array containing * only names of fields which we want. */ $response->setFieldList(['api_detail_url', 'aliases']) /* * Set filters to find resource you want. It needs key => value * schema and allows for many filters. */ ->setFilters(['name' => 'Batman', 'aliases' => 'Fatherless']) /* * Allows to sort response. Key is name of field, value is asc or desc * only. */ ->setSort(['date_added' => 'asc']) /* * Set how much elements do you want to get. Maximum limit is 100 * (API restrictions). */ ->setLimit(100) /* * From which position we would want to start getting elements. */ ->setOffset(50) /* * After setting request query (set methods), times to get * response. Get response return XML object or JSON string. */ ->getResponse()
扩展
查看当前包的状态,您可以编写自己的Connection
类(必须实现interface Connection
)和ResponseFormat
(必须实现interface ResponseFormat
)。我们可以在ComicVine::register()
方法中使用新的Connection
类,在setFormat
方法中使用新的ResponseFormat
类。
许可证
该包是开源软件,遵循MIT许可证。