etsy - upload image to product using guzzle - php

i am trying to upload image to listing in etsy using guzzle
here is my code
$image_url = PATH_TO_IMAGE;
$response = $this->_guzzle->request("POST", "https://openapi.etsy.com/v3/application/shops/{$shopId}/listings/{$productId}/images", [
'headers' => [
'x-api-key' => $keyString,
'Authorization' => "Bearer {$accessToken}",
'Content-Type' => "multipart/form-data"
],
'multipart' => [
[
'name' => 'image',
'filename' => 'a.png',
'contents' => fopen($image_url, "rb")
]
],
'verify' => false
]);
i am still getting error
[error] => Either a valid image file or a listing_image_id must be provided.

Related

How to send Fast Excel to Guzzle post request

i use Fast Excel library from https://github.com/rap2hpoutre/fast-excel, can i put return from ->download('file.xlsx') to Guzzle post request? with this code i'm not get anything
$httpClient = new \GuzzleHttp\Client([
'headers' => [
'Authorization' => mdmTokenGenerate($token),
'Accept' => 'application/json'
],
'verify' => false
]);
$response = $httpClient->post(
"{$client->domain}/api/push", [
'multipart' => [
[
'name' => 'file',
'contents' => (new FastExcel(MasterNumbers::limit(1000)->get()))->download('file.xlsx')
]
]
]
);

download pdf with guzzle

I want to download a pdf file with guzzle. Is this possible?
I tried this code:
$response = $this->client->post(self::API_BASE_URL.self::API_LABEL_URL,
[
'future' => true,
'json' => [$this->json],
'headers' => ['Content-Type' => 'application/json', 'Accept' => 'application/pdf'],
'query' => [
'return-type' => 'pdf',
'x-api-key' => $this->apiKey
],
]);
and I get
[body] => GuzzleHttp\Stream\Stream Object(
[stream:GuzzleHttp\Stream\Stream:private] => Resource id #120
[size:GuzzleHttp\Stream\Stream:private] => 649
[seekable:GuzzleHttp\Stream\Stream:private] => 1
[readable:GuzzleHttp\Stream\Stream:private] => 1
[writable:GuzzleHttp\Stream\Stream:private] => 1
[uri:GuzzleHttp\Stream\Stream:private] => php://temp
[customMetadata:GuzzleHttp\Stream\Stream:private] => Array
But I don't have an idea how I can handle this Resource id to save the pdf file.
I use guzzle 5.3 and PHP 5.4 with no options to update the php version.
You can use Guzzle sink option to download file - http://docs.guzzlephp.org/en/stable/request-options.html#sink
$pdfFilePath = __DIR__ . '/resource/file.pdf'; // specify your path
$pdfFileResource = fopen($pdfFilePath, 'w+');
$this->client->post(
self::API_BASE_URL . self::API_LABEL_URL,
[
'future' => true,
'json' => [$this->json],
'headers' => [
'Content-Type' => 'application/json',
'Accept' => 'application/pdf'
],
'query' => [
'return-type' => 'pdf',
'x-api-key' => $this->apiKey
],
'sink' => $pdfFileResource
]
);
More examples - https://github.com/andriichuk/php-curl-cookbook#download-file

Send Multipart Form from PHP with Guzzle

I'm trying to create a multipart form for upload file in my API.
According the docs of guzzle :
\\$data = picture of form
\\$uri = Endpoint in my API
$this->client->post($uri, [
'multipart' => [
[
'name' => 'picture',
'contents' => file_get_contents($data),
]
],
'headers' => $headers
]);
When I'm using Insomnia or Postman :
I'm not understand why it not working in Guzzle.
$file = new File($data->getPathname());
$tmp_picture_path = Storage::putFile("temp_dir", $file);
$tmp_picture = Storage::readStream($tmp_picture_path);
$response = $this->client->post(
$uri,
[
'multipart' => [
[
'name' => 'picture',
'contents' => $tmp_picture,
'filename' => "avatar." . $data->getClientOriginalExtension()
]
],
'headers' => $headers
]
);
Storage::delete($tmp_picture_path);
Just save locally the file and read the stream dor send it properly.

Post data and file using Guzzle - PHP & Laravel

I am using guzzle 6.3 to post a file along with some data to my api built on laravel 5.5. When i post the data, i am not able to get the data sent to the api except the file posted.
Client Side
$client = new Client([
'headers' => [ 'Content-Type' => 'application/json' ]
]);
$response = $client->request('POST',$url, [
'multipart' => [
[
'name' => 'body',
'contents' => json_encode(['documentation' => 'First Doc', 'recipient' => ['78011951231']]),
'headers' => ['Content-Type' => 'application/json']
],
[
'name' => 'file',
'contents' => fopen('/path/public/media/aaaah.wav', 'r'),
'headers' => ['Content-Type' => 'audio/wav']
],
],
]);
echo($response->getBody()->getContents());
API Controller
if (($Key)) {
return response()->json([
'status' => 'success',
'documenation' => $request->get('documentation'),
'recipient' => $request->get('recipient'),
'file' =>$request->get('file'),
'media'=>$request->hasFile('file')
]);
}
Response
{"status":"error","documentation":null,,"recipient":null,"file":null,"media":true}
Why am i getting NULL returned for the data that i am posting? Could it be because of the file that i am posting ?

guzzle, how to force content-type in a multipart/form-data

I'm new with Guzzle and I'm trying to make a REST request to sign PDF file. The provider says :
you need to use BASIC authentication
the request must be a POST request
the mimetype should be multipart/form-data
the file sent must be application/octet-stream and its name should be "file"
the data sent must be application/json and its name should be "data"
The system returns a response which contains the signed PDF file and type is application/octet-stream
This is the code I tested with Guzzle, but the provider says that the type mime sent in application/pdf. How can I "force" the mimetype for the PDF file ?
$client = new Client([
'auth' => ['login', 'password'],
'debug' => true,
'curl' => [
CURLOPT_PROXY => '192.168.1.232',
CURLOPT_PROXYPORT => '8080',
CURLOPT_PROXYUSERPWD => 'username:password',
],
]);
$boundary = 'my_custom_boundary';
$multipart = [
[
'name' => 'data',
'contents' => "{'nomDocument':'documentTest.pdf','externalid':'123456'}",
'Content-Type' => 'application/json'
],
[
'name' => 'file',
'contents' => fopen('documentTest.pdf', 'r'),
'Content-Type' => 'application/octet-stream'
],
];
$params = [
'headers' => [
'Connection' => 'close',
'Content-Type' => 'multipart/form-data; boundary='.$boundary,
],
'body' => new GuzzleHttp\Psr7\MultipartStream($multipart, $boundary),
];
try{
$response = $client->request('POST', 'https://server.com/api/sendDocument', $params);
} catch (RequestException $e) {
echo Psr7\str($e->getRequest());
if ($e->hasResponse()) {
echo Psr7\str($e->getResponse());
}
}
Thank you for your help.
You have to pass the Content-Type in headers
$multipart = [
[
'name' => 'data',
'contents' => "{'nomDocument':'documentTest.pdf','externalid':'123456'}",
'headers' => [ 'Content-Type' => 'application/json']
],
[
'name' => 'file',
'contents' => fopen('documentTest.pdf', 'r'),
'headers' => [ 'Content-Type' => 'application/octet-stream']
],
];
in Guzzle Documentation say that you can specify headers for every multipart data.
If you not set header Guzzle put a Content-Type for you based on file.

Categories