From 79a37af8e5f5f4eef77e5e3ab75741995820a34c Mon Sep 17 00:00:00 2001 From: David Berlioz Date: Tue, 15 Nov 2022 17:44:51 +0100 Subject: [PATCH 1/4] #336 add charset to upload() --- lib/Document.php | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/Document.php b/lib/Document.php index b128e1253..6d2c94dc0 100644 --- a/lib/Document.php +++ b/lib/Document.php @@ -245,13 +245,14 @@ public function downloadStream($output = null): StreamInterface { * Uploads data to the document specified in the constructor. * * @param string|resource|StreamInterface|callable|\Iterator $feedData The contents of the feed to be uploaded + * @param ?string $charset An optional charset for the document to upload * * @return void */ - public function upload($feedData): void { + public function upload($feedData, ?string $charset = null): void { $response = $this->client->put($this->url, [ RequestOptions::HEADERS => [ - "content-type" => $this->contentType, + "content-type" => self::get_content_type_with_charset($this->contentType, $charset), "host" => parse_url($this->url, PHP_URL_HOST), ], RequestOptions::BODY => $feedData, @@ -275,4 +276,19 @@ public function __destruct() { unlink($this->tempFilename); } } + + /** + * Create a normalized content-type header. + * When uploading a document you must use the exact same content-type/charset in createFeedDocument() and upload(). + * + * @param string $contentType + * @param string|null $charset + * @return string + */ + public static function get_content_type_with_charset(string $contentType, string $charset = null): string { + if ($charset) { + return $contentType . "; charset=$charset"; + } + return $contentType; + } } From 07b7407f1161020b360b636187601c6bcf4d2937 Mon Sep 17 00:00:00 2001 From: David Berlioz Date: Tue, 15 Nov 2022 18:24:09 +0100 Subject: [PATCH 2/4] #336 doc for upload with charset --- README.md | 56 +++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 40 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 5664df474..2373259b3 100644 --- a/README.md +++ b/README.md @@ -38,22 +38,30 @@ If you've found this library useful, please consider [becoming a Sponsor](https: Check out the [Getting Started](#getting-started) section below for a quick overview. This README is divided into several sections: -* [Setup](#setup) - * [Configuration options](#configuration-options) -* [Examples](#examples) -* [Debug mode](#debug-mode) -* [Supported API segments](#supported-api-segments) - * [Seller APIs](#seller-apis) - * [Vendor APIs](#vendor-apis) -* [Restricted operations](#restricted-operations) -* [Uploading and downloading documents](#uploading-and-downloading-documents) - * [Downloading a report document](#downloading-a-report-document) - * [Uploading a feed document](#uploading-a-feed-document) - * [Downloading a feed result document](#downloading-a-feed-result-document) -* [Working with model classes](#working-with-model-classes) -* [Response headers](#response-headers) -* [Custom request authorization](#custom-authorization-signer) -* [Custom request signing](#custom-request-signer) +- [Selling Partner API for PHP](#selling-partner-api-for-php) + - [Features](#features) + - [Sponsors](#sponsors) + - [Installation](#installation) + - [Table of Contents](#table-of-contents) + - [Getting Started](#getting-started) + - [Prerequisites](#prerequisites) + - [Setup](#setup) + - [Configuration options](#configuration-options) + - [Examples](#examples) + - [Debug mode](#debug-mode) + - [Supported API segments](#supported-api-segments) + - [Seller APIs](#seller-apis) + - [Vendor APIs](#vendor-apis) + - [Restricted operations](#restricted-operations) + - [Uploading and downloading documents](#uploading-and-downloading-documents) + - [Downloading a report document](#downloading-a-report-document) + - [Uploading a feed document](#uploading-a-feed-document) + - [Uploading with a specific charset](#uploading-with-a-specific-charset) + - [Downloading a feed result document](#downloading-a-feed-result-document) + - [Working with model classes](#working-with-model-classes) + - [Response headers](#response-headers) + - [Custom Authorization Signer](#custom-authorization-signer) + - [Custom Request Signer](#custom-request-signer) ## Getting Started @@ -311,6 +319,22 @@ $feedId = $createFeedResult->getFeedId(); If you are manipulating huge feed documents you can pass to `upload()` anything that Guzzle can turn into a stream. +### Uploading with a specific charset + +```php +$charset = "Shift-JIS"; +... +// Create feed document +$createFeedDocSpec = new Feeds\CreateFeedDocumentSpecification([ + 'content_type' => SellingPartnerApi\Document::get_content_type_with_charset($feedType['contentType'], $charset)] +]); +... +// Upload feed contents to document +... +$docToUpload->upload($feedContents, $charset); +``` + + ## Downloading a feed result document This works very similarly to downloading a report document: From 6ed5e406e4f9c148408819d94c701dc0feca24cd Mon Sep 17 00:00:00 2001 From: David Berlioz Date: Tue, 15 Nov 2022 20:17:44 +0100 Subject: [PATCH 3/4] Auto TOC off --- README.md | 40 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 2373259b3..d2c855ffc 100644 --- a/README.md +++ b/README.md @@ -38,30 +38,22 @@ If you've found this library useful, please consider [becoming a Sponsor](https: Check out the [Getting Started](#getting-started) section below for a quick overview. This README is divided into several sections: -- [Selling Partner API for PHP](#selling-partner-api-for-php) - - [Features](#features) - - [Sponsors](#sponsors) - - [Installation](#installation) - - [Table of Contents](#table-of-contents) - - [Getting Started](#getting-started) - - [Prerequisites](#prerequisites) - - [Setup](#setup) - - [Configuration options](#configuration-options) - - [Examples](#examples) - - [Debug mode](#debug-mode) - - [Supported API segments](#supported-api-segments) - - [Seller APIs](#seller-apis) - - [Vendor APIs](#vendor-apis) - - [Restricted operations](#restricted-operations) - - [Uploading and downloading documents](#uploading-and-downloading-documents) - - [Downloading a report document](#downloading-a-report-document) - - [Uploading a feed document](#uploading-a-feed-document) - - [Uploading with a specific charset](#uploading-with-a-specific-charset) - - [Downloading a feed result document](#downloading-a-feed-result-document) - - [Working with model classes](#working-with-model-classes) - - [Response headers](#response-headers) - - [Custom Authorization Signer](#custom-authorization-signer) - - [Custom Request Signer](#custom-request-signer) +* [Setup](#setup) + * [Configuration options](#configuration-options) +* [Examples](#examples) +* [Debug mode](#debug-mode) +* [Supported API segments](#supported-api-segments) + * [Seller APIs](#seller-apis) + * [Vendor APIs](#vendor-apis) +* [Restricted operations](#restricted-operations) +* [Uploading and downloading documents](#uploading-and-downloading-documents) + * [Downloading a report document](#downloading-a-report-document) + * [Uploading a feed document](#uploading-a-feed-document) + * [Downloading a feed result document](#downloading-a-feed-result-document) +* [Working with model classes](#working-with-model-classes) +* [Response headers](#response-headers) +* [Custom request authorization](#custom-authorization-signer) +* [Custom request signing](#custom-request-signer) ## Getting Started From 33d0f2a51647d28b9ab637a68cb4fe3217b34f59 Mon Sep 17 00:00:00 2001 From: David Berlioz Date: Tue, 24 Jan 2023 09:25:22 +0100 Subject: [PATCH 4/4] #336 rename charset function and add default --- README.md | 2 +- lib/Document.php | 15 ++++++--------- 2 files changed, 7 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index f2fc2df18..5d276da8e 100644 --- a/README.md +++ b/README.md @@ -318,7 +318,7 @@ $charset = "Shift-JIS"; ... // Create feed document $createFeedDocSpec = new Feeds\CreateFeedDocumentSpecification([ - 'content_type' => SellingPartnerApi\Document::get_content_type_with_charset($feedType['contentType'], $charset)] + 'content_type' => SellingPartnerApi\Document::withContentType($feedType['contentType'], $charset)] ]); ... // Upload feed contents to document diff --git a/lib/Document.php b/lib/Document.php index 04fa993ac..be2defb77 100644 --- a/lib/Document.php +++ b/lib/Document.php @@ -245,14 +245,14 @@ public function downloadStream($output = null): StreamInterface { * Uploads data to the document specified in the constructor. * * @param string|resource|StreamInterface|callable|\Iterator $feedData The contents of the feed to be uploaded - * @param ?string $charset An optional charset for the document to upload + * @param string $charset An optional charset for the document to upload * * @return void */ - public function upload($feedData, ?string $charset = null): void { + public function upload($feedData, string $charset = 'utf-8'): void { $response = $this->client->put($this->url, [ RequestOptions::HEADERS => [ - "content-type" => self::get_content_type_with_charset($this->contentType, $charset), + "content-type" => self::withContentType($this->contentType, $charset), "host" => parse_url($this->url, PHP_URL_HOST), ], RequestOptions::BODY => $feedData, @@ -282,13 +282,10 @@ public function __destruct() { * When uploading a document you must use the exact same content-type/charset in createFeedDocument() and upload(). * * @param string $contentType - * @param string|null $charset + * @param string $charset * @return string */ - public static function get_content_type_with_charset(string $contentType, string $charset = null): string { - if ($charset) { - return $contentType . "; charset=$charset"; - } - return $contentType; + public static function withContentType(string $contentType, string $charset = 'utf-8'): string { + return "{$contentType}; charset={$charset}"; } }