Skip to content

Commit

Permalink
Merge pull request #431 from quazardous/upload-charset-336
Browse files Browse the repository at this point in the history
Upload with charset
  • Loading branch information
jlevers committed Jan 24, 2023
2 parents 547667a + 33d0f2a commit 7ca2ba2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
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}";
}
}

0 comments on commit 7ca2ba2

Please sign in to comment.