diff --git a/README.md b/README.md index b5a2e8e38..5d276da8e 100644 --- a/README.md +++ b/README.md @@ -311,6 +311,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::withContentType($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: diff --git a/lib/Document.php b/lib/Document.php index c2d48238e..be2defb77 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 = 'utf-8'): void { $response = $this->client->put($this->url, [ RequestOptions::HEADERS => [ - "content-type" => $this->contentType, + "content-type" => self::withContentType($this->contentType, $charset), "host" => parse_url($this->url, PHP_URL_HOST), ], RequestOptions::BODY => $feedData, @@ -275,4 +276,16 @@ 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 $charset + * @return string + */ + public static function withContentType(string $contentType, string $charset = 'utf-8'): string { + return "{$contentType}; charset={$charset}"; + } }