Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload with charset #431

Merged
merged 6 commits into from
Jan 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
17 changes: 15 additions & 2 deletions lib/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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}";
}
}