Struggling with s3 multipart upload guzzle for Eventbrite - php

I'm trying to upload to s3 based on the documentation in Eventbrite but I'm getting nowhere. I think it's down to the structure of my request but I've tried multiple different things and nothing is really working and I get a 'Bucket POST must contain a field named 'key' error.
The instructions are here, so it provides you with a presigned POST object, but I can't figure out how to then provide those details in Guzzle
https://www.eventbrite.com/developer/v3/resources/uploads/
I would use the s3client but I don't think it's suitable, as I do not have the region name.
So this is the array back from EB, as given in the documentation
$postFields = [
'key' => $post_args['key'],
'AWSAccessKeyId' => $post_args['AWSAccessKeyId'],
'bucket' => $post_args['bucket'],
'acl' => $post_args['acl'],
'signature' => $post_args['signature'],
'policy' => $post_args['policy'],
];
and I've tried various structures:
$args = [
'query' => $postFields,
'multipart' => [
[
'name' => $instructions_response['body']['file_parameter_name'],
'Content-type' => 'multipart/form-data',
'contents' => $image,
]
],
]
Or
$args = [
'multipart' => [
[
'key' => $post_args['key'],
'AWSAccessKeyId' => $post_args['AWSAccessKeyId'],
'bucket' => $post_args['bucket'],
'acl' => $post_args['acl'],
'signature' => $post_args['signature'],
'policy' => $post_args['policy'],
'name' => $instructions_response['body']['file_parameter_name'],
'Content-type' => 'multipart/form-data',
'contents' => $image,
]
],
];
Or
$args = [
'key' => $post_args['key'],
'AWSAccessKeyId' => $post_args['AWSAccessKeyId'],
'bucket' => $post_args['bucket'],
'acl' => $post_args['acl'],
'signature' => $post_args['signature'],
'policy' => $post_args['policy'],
'multipart' => [
[
'name' => $instructions_response['body']['file_parameter_name'],
'Content-type' => 'multipart/form-data',
'contents' => $image,
]
],
];
(Plus a bunch of more spurious ones that I can't remember anymore)
Anyway, I'm stuck. I just can't think how this POST data is meant to be structured for it to post to S3. I've gone down the rabbit hole with Xdebug and it's still not clear what is even going on when it makes the request tbh
Request called like so
$upload_response = $this->client->request('POST', $upload_url, $args);
UPDATE: It was pointed out to me that whilst 'key' is lowercase in the error message and what comes back from aws, it's capitalised in the documentation. I changed it to 'Key' and now I get 'Conflicting query string parameters: acl, policy'

Related

etsy - upload image to product using guzzle

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.

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 ?

Php, Guzzle, Schema validation failed but working in curl

I'm in the process of convert from cURL to Guzzle, and got most of it working.
GET requests working great etc.
My problem is the POST request, getting Schema validation errors.
It works in curl, so I'm a bit confused... well, a lot.
Client error: `POST https://restapi.e-conomic.com/customers` resulted in a `400 Bad Request` response:
{"message":"Schema validation failed.","errorCode":"E00500"
I hope one of you can tell me, if I did something wrong in the convert? Maybe my arrays needs to be formatted in another way.
This is my old working "cURL code":
$data = array(
'name' => 'Test User',
'address' => 'Road 123',
'email' => 'morten#domain.com',
'zip' => '9000',
'city' => 'City',
'country' => 'Danmark',
'corporateIdentificationNumber' => '12345678',
'customerGroup' => array(
'customerGroupNumber' => 1
),
'currency' => 'DKK',
'paymentTerms' => array(
'paymentTermsNumber' => 1
),
'vatZone' => array(
'vatZoneNumber' => 1
)
);
$options = array(
CURLOPT_URL => 'https://restapi.e-conomic.com/customers',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => array(
'X-AppSecretToken:[removed]',
'X-AgreementGrantToken:[removed]',
'Content-Type:application/json; charset=utf-8'
),
CURLOPT_POSTFIELDS => json_encode($data)
);
curl_setopt_array($ch, $options);
This is my new "guzzle code", that is causing me problems:
$client = new GuzzleHttp\Client();
$headers = [
'X-AppSecretToken' => '[removed]',
'X-AgreementGrantToken' => '[removed]',
'Content-Type' => 'application/json;charset=utf-8',
'debug' => false
];
$form_params = [
'name' => 'Test User',
'address' => 'Road 123',
'email' => 'test#email.dk',
'zip' => '9000',
'city' => 'City',
'country' => 'Danmark',
'corporateIdentificationNumber' => '12345678',
'customerGroup' => [
'customerGroupNumber' => 1
],
'currency' => 'DKK',
'paymentTerms' => [
'paymentTermsNumber' => 1
],
'vatZone' => [
'vatZoneNumber' => 1
]
];
$response = $client->post('https://restapi.e-conomic.com/customers', [
'headers' => $headers,
'form_params' => $form_params
]);
I tried to use the "body" parameter, as stated in the Guzzle documentation, but received this error:
Passing in the "body" request option as an array to send a POST request has been deprecated. Please use the "form_params" request option to send a application/x-www-form-urlencoded request, or the "multipart" request option to send a multipart/form-data request.
I'm not sure what to do and really hope, that one of you guys will tell me what i'm doing wrong.
https://restapi.e-conomic.com/schema/customers.post.schema.json#_ga=2.167601086.1488491524.1500877149-796726383.1499933074
https://restdocs.e-conomic.com/#post-customers
I had to post the quest as json:
$response = $client->post('https://restapi.e-conomic.com/customers', [
'headers' => $headers,
'json' => $form_params
]);

Upload image file using slack API files.upload method

The following code seems to work, but the Slack API saves the file a plain text.
protected function upload($file)
{
$client = $this->guzzle;
if (!$token) {
$token = env('SLACK_TOKEN');
}
$response = $client->request('POST', env('SLACK_API') . "/files.upload?token=$token", [
'form_params' => [
'name' => $file->getFilename(),
'content' => File::get($file->getRealPath()),
'filename' => $file->getFilename(),
'filetype' => 'image',
'channels' => "#_test",
]
]);
return json_decode((string)$response->getBody());
}
When I use the guzzle multipart post, I get the error: 'no_file_data' I feel like I am missing something.
Is there a way to upload images or non-text files using the files.upload method in the Slack API?
Almost as soon as I posted this question, I realized that I was formatting the array incorrectly on the multipart post in guzzle.
This seems to work:
$response = $this->guzzle->post(env('SLACK_API') . "/files.upload?token=$token",
['multipart' =>
[
[
'name' => 'filename',
'contents' => $file->getClientOriginalName()
],
[
'name' => 'file',
'contents' => fopen($file,'r')
],
[
'name' => 'channels',
'contents' => '#_test'
]
]
]);

convert curl to guzzle

i have this perfectly working curl command:
curl -i --data "site=walletgroove.com&placement=above&device=desktop&source=*&campaign=*&url=*&country=*&active=1" http://10.0.0.38/adserver/src/public/api/rule
which i tried to execute with guzzle but for some reason i keep getting error from my code, an exception to be more accurate. this exception is thrown when params are not passed properly.
this is one of few tries i had:
public function testApi_postRule()
{
$client = new Client();
$client->post('http://10.0.0.38/adserver/src/public/api/rule',[ 'query' => [
'site' => 'walletgroove.com',
'placement' => 'guzzle_unique_placement',
'device' => 'desktop',
'source' => 'guzzource',
'campaign' => '*',
'country' => '*',
'url' => '*',
'active' => '1'
]]);
}
any idea what am i doing wrong here??
You are passing the query parameter, which appends a query string instead of sending in the request body. For a POST request, you probably want to use form_params as shown in the documentation.
$client->post('http://10.0.0.38/adserver/src/public/api/rule', [
'form_params' => [
'site' => 'walletgroove.com',
'placement' => 'guzzle_unique_placement',
'device' => 'desktop',
'source' => 'guzzource',
'campaign' => '*',
'country' => '*',
'url' => '*',
'active' => '1'
]
]);

Categories