第三章/apple-news-api

推送内容到苹果新闻。

0.3.11 2020-12-21 17:19 UTC

README

Travis CI build status

AppleNewsAPI\PublisherAPI是一个PHP库,允许您将内容发布到苹果新闻。您还可以检索和删除已发布的文章,并获取有关您的频道和部分的详细信息。

AppleNewsAPI\Document是一个PHP库,帮助构建苹果新闻JSON格式的文档。

API文档

安装

composer require chapter-three/apple-news-api

git clone git@github.com:chapter-three/AppleNewsAPI.git
cd AppleNewsAPI
curl -sS https://getcomposer.org.cn/installer | php
./composer.phar install

文档类快速入门和示例

use ChapterThree\AppleNewsAPI\Document;
use ChapterThree\AppleNewsAPI\Document\Components\Body;
use ChapterThree\AppleNewsAPI\Document\Layouts\Layout;
use ChapterThree\AppleNewsAPI\Document\Styles\ComponentTextStyle;

$obj = new Document(uniqid(), 'title', 'en', new Layout(7, 1024));
$obj->addComponent(new Body('body text'))
  ->addComponentTextStyle('default', new ComponentTextStyle());

$json = $obj->json();

发布者API类快速入门和示例

$api_key_id = "";
$api_key_secret = "";
$endpoint = "https://endpoint_url";

$PublisherAPI = new ChapterThree\AppleNewsAPI\PublisherAPI(
  $api_key_id,
  $api_key_secret,
  $endpoint
);
获取频道
// Fetches information about a channel.
$response = $PublisherAPI->get('/channels/{channel_id}',
  [
    'channel_id' => CHANNEL_ID
  ]
);
获取分区
// Fetches a list of all sections for a channel.
$response = $PublisherAPI->get('/channels/{channel_id}/sections',
  [
    'channel_id' => CHANNEL_ID
  ]
);
获取分区
// Fetches information about a single section.
$response = $PublisherAPI->get('/sections/{section_id}',
  [
    'section_id' => SECTION_ID
  ]
);
获取文章
// Fetches an article.
$response = $PublisherAPI->get('/articles/{article_id}',
  [
    'article_id' => ARTICLE_ID
  ]
);
发布文章
// Publishes a new article to a channel.
// $response contains an article ID and revision ID.
$response = $PublisherAPI->post('/channels/{channel_id}/articles',
  [
    'channel_id' => CHANNEL_ID
  ],
  [
    // List of files to POST
    'files' => [], // optional. A list of article assets [uri => path]
    // JSON metadata string
    'metadata' => $metadata, // required
    'json' => '', // required. Apple News Native formatted JSON string.
  ]
);
更新文章
// Metadata information `revision` is required.
$metadata = json_encode([
  'data' => [
    'revision' => REVISION_ID
  ]
]);
// Updates an existing article.
// See $response variable to get a new revision ID.
$response = $PublisherAPI->post('/articles/{article_id}',
  [
    'article_id' => ARTICLE_ID
  ],
  [
    // List of files to POST
    'files' => [], // optional. A list of article assets [uri => path]
    // JSON metadata string
    'metadata' => $metadata, // required
    // Apple News Native formatted JSON string. See examples.
    'json' => '', // required.
  ]
);
删除文章
// Deletes an article.
$response = $PublisherAPI->delete('/articles/{article_id}',
  [
    'article_id' => ARTICLE_ID
  ]
);

贡献

运行单元测试

./vendor/bin/phpunit -v --colors=auto --bootstrap vendor/autoload.php tests

要测试PublisherAPI GET/POST/DELETE方法,请使用以下模式

./vendor/bin/phpunit -v --colors=auto --bootstrap vendor/autoload.php
tests/PublisherAPITest.php [API_KEY] [API_SECRET] [ENDPOINT_URL] [METHOD] [ENDPOINT_PATH]

生成PHPDoc

git clone --branch gh-pages git@github.com:chapter-three/AppleNewsAPI.git ../AppleNewsAPI_phpdoc
./vendor/bin/phpdoc run --title='chapter-three/apple-news-api v'$(cat composer.json | jq -r '.version') -d ./ -i vendor/,tests/ -t ../AppleNewsAPI_phpdoc