Apple News API PHP Client

AppleNewsClient
in package

FinalYes

Apple News Publisher API Client.

This class provides high-level methods to interact with the Apple News API, including channel information retrieval, section management, and article CRUD (Create, Read, Update, Delete) operations.

It uses PSR-18 for HTTP requests and PSR-17 for message factories to ensure compatibility with any modern PHP HTTP library.

Tags
see
https://developer.apple.com/documentation/applenewsapi

Table of Contents

Constants

API_ENDPOINT  = 'https://news-api.apple.com'

Properties

$authenticator  : Authenticator
$endpoint  : string
$httpClient  : ClientInterface
$requestFactory  : RequestFactoryInterface
$streamFactory  : StreamFactoryInterface

Methods

__construct()  : mixed
create()  : self
Factory method to create a client with the given credentials.
createArticle()  : array<string, mixed>
Create a new article.
createArticleFromJson()  : array<string, mixed>
Create an article from raw JSON.
deleteArticle()  : void
Delete an article.
getArticle()  : array<string, mixed>
Get article information.
getChannel()  : array<string, mixed>
Get channel information.
getChannelQuota()  : array<string, mixed>
Get channel quota information.
getSection()  : array<string, mixed>
Get section information.
listSections()  : array<string, mixed>
List all sections in a channel.
promoteArticles()  : array<string, mixed>
Promote articles in a section.
searchArticlesInChannel()  : array<string, mixed>
Search articles in a channel.
searchArticlesInSection()  : array<string, mixed>
Search articles in a section.
updateArticle()  : array<string, mixed>
Update an existing article.
delete()  : void
Perform a signed DELETE request.
get()  : array<string, mixed>
Perform a signed GET request.
handleErrorResponse()  : never
Handle error responses from the API by throwing appropriate exceptions.
postJson()  : array<string, mixed>
Perform a signed POST request with JSON body.
postMultipart()  : array<string, mixed>
Perform a signed POST request with a multipart body.
sendRequest()  : array<string, mixed>
Helper to send a PSR-7 request and decode the JSON response.

Constants

API_ENDPOINT

private mixed API_ENDPOINT = 'https://news-api.apple.com'

Properties

$requestFactory read-only

private RequestFactoryInterface $requestFactory

$streamFactory read-only

private StreamFactoryInterface $streamFactory

Methods

__construct()

public __construct(Authenticator $authenticator, ClientInterface $httpClient, RequestFactoryInterface $requestFactory, StreamFactoryInterface $streamFactory[, string $endpoint = self::API_ENDPOINT ]) : mixed
Parameters
$authenticator : Authenticator

Handles request signing.

$httpClient : ClientInterface

PSR-18 HTTP client.

$requestFactory : RequestFactoryInterface

PSR-17 request factory.

$streamFactory : StreamFactoryInterface

PSR-17 stream factory.

$endpoint : string = self::API_ENDPOINT

The base URL for the API.

create()

Factory method to create a client with the given credentials.

public static create(string $keyId, string $keySecret, ClientInterface $httpClient, RequestFactoryInterface $requestFactory, StreamFactoryInterface $streamFactory[, string $endpoint = self::API_ENDPOINT ]) : self
Parameters
$keyId : string

Your Apple News API Key ID.

$keySecret : string

Your Apple News API Key Secret (Base64 encoded).

$httpClient : ClientInterface

A PSR-18 compliant HTTP client.

$requestFactory : RequestFactoryInterface

A PSR-17 compliant request factory.

$streamFactory : StreamFactoryInterface

A PSR-17 compliant stream factory.

$endpoint : string = self::API_ENDPOINT

Optional override for the API endpoint.

Return values
self

createArticle()

Create a new article.

public createArticle(string $channelId, Article $article[, array<string, mixed>|null $metadata = null ][, array<string, string> $assets = [] ]) : array<string, mixed>

Handles the multipart submission of the article JSON, metadata, and any bundled assets (images, fonts).

Parameters
$channelId : string

The channel to publish to.

$article : Article

The article document object.

$metadata : array<string, mixed>|null = null

Optional metadata (sections, isSponsored, etc.)

$assets : array<string, string> = []

Map of bundle:// URLs to file paths or raw binary content.

Tags
throws
AppleNewsException

If the API returns an error.

see
https://developer.apple.com/documentation/applenewsapi/post-channels-_channelid_-articles
Return values
array<string, mixed>

The decoded API response (including article ID).

createArticleFromJson()

Create an article from raw JSON.

public createArticleFromJson(string $channelId, string $articleJson[, array<string, mixed>|null $metadata = null ][, array<string, string> $assets = [] ]) : array<string, mixed>

Useful if you already have the ANF JSON generated by another tool.

Parameters
$channelId : string

The channel to publish to.

$articleJson : string

The raw ANF JSON string.

$metadata : array<string, mixed>|null = null

Optional metadata.

$assets : array<string, string> = []

Map of bundle URLs to local paths/content.

Tags
throws
AppleNewsException
Return values
array<string, mixed>

getChannelQuota()

Get channel quota information.

public getChannelQuota(string $channelId) : array<string, mixed>
Parameters
$channelId : string

The unique identifier for the channel.

Tags
throws
AppleNewsException

If the API returns an error.

Return values
array<string, mixed>

The decoded API response.

promoteArticles()

Promote articles in a section.

public promoteArticles(string $sectionId, array<string|int, string> $articleIds) : array<string, mixed>
Parameters
$sectionId : string

The unique identifier for the section.

$articleIds : array<string|int, string>

List of article IDs to promote.

Tags
throws
AppleNewsException

If the API returns an error.

see
https://developer.apple.com/documentation/applenewsapi/post-sections-_sectionid_-promotedarticles
Return values
array<string, mixed>

The decoded API response.

searchArticlesInChannel()

Search articles in a channel.

public searchArticlesInChannel(string $channelId[, array<string, mixed> $params = [] ]) : array<string, mixed>
Parameters
$channelId : string

The channel ID to search within.

$params : array<string, mixed> = []

Search parameters like pageSize, pageToken, fromDate, toDate.

Tags
throws
AppleNewsException
see
https://developer.apple.com/documentation/applenewsapi/get-channels-_channelid_-articles
Return values
array<string, mixed>

searchArticlesInSection()

Search articles in a section.

public searchArticlesInSection(string $sectionId[, array<string, mixed> $params = [] ]) : array<string, mixed>
Parameters
$sectionId : string

The section ID to search within.

$params : array<string, mixed> = []

Search parameters.

Tags
throws
AppleNewsException
Return values
array<string, mixed>

updateArticle()

Update an existing article.

public updateArticle(string $articleId, string $revision, Article $article[, array<string, mixed>|null $metadata = null ][, array<string, string> $assets = [] ]) : array<string, mixed>

Updating an article requires the 'revision' string found in the information of the existing article.

Parameters
$articleId : string

The ID of the article to update.

$revision : string

The revision token from the previous get/create response.

$article : Article

The updated article object.

$metadata : array<string, mixed>|null = null

Optional metadata.

$assets : array<string, string> = []

Assets for the updated article.

Tags
throws
AppleNewsException
see
https://developer.apple.com/documentation/applenewsapi/post-articles-_articleid_
Return values
array<string, mixed>

get()

Perform a signed GET request.

private get(string $path) : array<string, mixed>
Parameters
$path : string

The API path (excluding domain).

Tags
throws
AppleNewsException
Return values
array<string, mixed>

handleErrorResponse()

Handle error responses from the API by throwing appropriate exceptions.

private handleErrorResponse(string $body, int $statusCode) : never
Parameters
$body : string

The raw response body.

$statusCode : int

The HTTP status code.

Tags
throws
AuthenticationException|AppleNewsException
Return values
never

postJson()

Perform a signed POST request with JSON body.

private postJson(string $path, string $body) : array<string, mixed>
Parameters
$path : string

The API path.

$body : string

The JSON encoded body.

Tags
throws
AppleNewsException
Return values
array<string, mixed>

postMultipart()

Perform a signed POST request with a multipart body.

private postMultipart(string $path, MultipartBuilder $builder) : array<string, mixed>
Parameters
$path : string

The API path.

$builder : MultipartBuilder

The builder containing the parts.

Tags
throws
AppleNewsException
Return values
array<string, mixed>

sendRequest()

Helper to send a PSR-7 request and decode the JSON response.

private sendRequest(RequestInterface $request) : array<string, mixed>
Parameters
$request : RequestInterface

The signed request.

Tags
throws
AppleNewsException
Return values
array<string, mixed>

        
On this page

Search results