Apple News API PHP Client

MultipartBuilder
in package

FinalYes

Builds multipart/form-data request bodies for the Apple News API.

Apple News requires articles to be submitted as MIME multipart messages. The message MUST contain an 'article.json' part and can optionally include a 'metadata' part and binary parts for bundle resources (images, fonts, etc.).

Tags
see
https://developer.apple.com/documentation/applenewsapi/post-channels-_channelid_-articles

Table of Contents

Constants

CRLF  = "\r\n"

Properties

$boundary  : string
$parts  : array<int, array{name: string, filename: string|null, contentType: string, content: string}>

Methods

__construct()  : mixed
addArticle()  : self
Convenience method to add the 'article.json' document part.
addFont()  : self
Add a font file part.
addImage()  : self
Add an image part from raw binary content.
addImageFile()  : self
Add an image part from a local file path.
addJson()  : self
Add a JSON part to the multipart body.
addMetadata()  : self
Convenience method to add article metadata.
addPart()  : self
Core method to add a generic part to the multipart body.
build()  : string
Build the complete multipart raw body string.
getBoundary()  : string
Get the random boundary string used.
getContentType()  : string
Get the full Content-Type header value required for this request.
guessImageContentType()  : string
Guess the MIME content type based on the file extension.

Constants

Properties

$parts

private array<int, array{name: string, filename: string|null, contentType: string, content: string}> $parts = []

Methods

__construct()

public __construct([string|null $boundary = null ]) : mixed
Parameters
$boundary : string|null = null

Optional custom boundary string.

addArticle()

Convenience method to add the 'article.json' document part.

public addArticle(string $json) : self
Parameters
$json : string

The ANF JSON string.

Return values
self

addFont()

Add a font file part.

public addFont(string $name, string $content, string $filename) : self
Parameters
$name : string

The part name.

$content : string

Raw binary font data.

$filename : string

The font filename.

Return values
self

addImage()

Add an image part from raw binary content.

public addImage(string $name, string $content, string $filename[, string|null $contentType = null ]) : self
Parameters
$name : string

The part name.

$content : string

Raw binary data.

$filename : string

The filename to present in the part header.

$contentType : string|null = null

Optional MIME type. Guessed from filename if null.

Return values
self

addImageFile()

Add an image part from a local file path.

public addImageFile(string $name, string $filePath) : self
Parameters
$name : string

The part name/bundle identifier.

$filePath : string

Absolute path to the local file.

Tags
throws
InvalidArgumentException

if file doesn't exist.

throws
RuntimeException

if file cannot be read.

Return values
self

addJson()

Add a JSON part to the multipart body.

public addJson(string $name, string $json[, string|null $filename = null ]) : self
Parameters
$name : string

The part name (e.g., 'article.json', 'metadata').

$json : string

The JSON string.

$filename : string|null = null

Optional filename for the part.

Return values
self

addMetadata()

Convenience method to add article metadata.

public addMetadata(array<string, mixed> $metadata) : self
Parameters
$metadata : array<string, mixed>

Metadata array (wrapped in 'data' key automatically).

Tags
throws
JsonException
Return values
self

addPart()

Core method to add a generic part to the multipart body.

public addPart(string $name, string $content, string $contentType[, string|null $filename = null ]) : self
Parameters
$name : string

Part name.

$content : string

Content data.

$contentType : string

MIME content type.

$filename : string|null = null

Optional filename.

Return values
self

build()

Build the complete multipart raw body string.

public build() : string
Return values
string

getBoundary()

Get the random boundary string used.

public getBoundary() : string
Return values
string

getContentType()

Get the full Content-Type header value required for this request.

public getContentType() : string
Return values
string

guessImageContentType()

Guess the MIME content type based on the file extension.

private guessImageContentType(string $filename) : string
Parameters
$filename : string
Return values
string

        
On this page

Search results