I am trying to convert curl request to Guzzle:
curl --location --request POST 'https://shopify.s3.amazonaws.com' \
--form 'key="tmp/436699194/bulk/4fed3d0c/bulk-customer-insert-file.jsonl"' \
--form 'x-amz-credential="AKIAJYM55WGJDKQ/20210625/us-east-1/s3/aws4_request"' \
--form 'x-amz-algorithm="AWS4-HMAC-SHA256"' \
--form 'x-amz-date="20210625T105058Z"' \
--form 'x-amz-signature="6c02b9f5dff8dd57d04bcdef3e3e602cad09fa719ffd84d"' \
--form 'policy="policy"' \
--form 'acl="private"' \
--form 'Content-Type="text/jsonl"' \
--form 'success_action_status="201"' \
--form 'file="https://files.com/uploads/imports/12841.jsonl"'
The curl request works properly, but when I try to transfer It to Guzzle, It stops working and returns an error:
$response = (new Client([
'headers' => ['Content-Type' => 'multipart/form-data']
]))->post('https://shopify.s3.amazonaws.com', [
'form_params' => [
'key' => 'tmp/436699194/bulk/492f28bf-d0c/bulk-customer-insert-file.jsonl',
'x-amz-credential' => 'AKIAJYKQ/20210625/us-east-1/s3/aws4_request',
'x-amz-algorithm' => 'AWS4-HMAC-SHA256',
'x-amz-date' => '20210625T05058Z',
'x-amz-signature' => '6c02b9f5dcde2cafa719ffd84d',
'policy' => 'policy',
'acl' => 'private',
'Content-Type' => 'text/jsonl',
'success_action_status' => '201',
'file' => 'https://files.com/uploads/imports/12841.jsonl',
],
]);
Error:
<Error><Code>InvalidArgument</Code><Message>Conflicting query string parameters: acl, policy</Message><ArgumentName>ResourceType</ArgumentName><ArgumentValue>acl</ArgumentValue><RequestId>P6V9RFVQNMW249XD</RequestId><HostId>GhCPTKO2P/VysP90bvFI5lXiyzF0IlSX//rotCB/hTtxy8tQMcwqKh8j397VdMKYvD1UL+aEgMo=</HostId></Error>
The documentation states: "You must use a multipart form, and include all parameters as form inputs in the request body."
Using multipart instead of form_params worked perfectly!
Related
I wanted to convert following Curl command line to php/curl but for many reasons my customer wants guzzle in PHP.
curl -X POST "https://uploadexample.net/api/upload" -H "accept: application/json" -H "Authorization: bearer: 928292992qwg" -H "Content-Type: multipart/form-data" -F "front_photo=#photo-1-214x300.jpg;type=image/jpeg" -F "back_photo=#photo-2-214x300.jpg;type=image/jpeg"
I have never used guzzle but I tried to manually convert above Curl to this guzzle routine. I get 500 server error. Am I converting correctly?
I was trying to stay as close to original curl as possible.
$headers = [
'Content-type' => 'application/json',
'Content-type' => 'multipart/form-data',
'Accept' => 'application/json',
"Authorization" => "Bearer 928292992qwg"
];
$client = new Client([
Base URI is used with relative requests
'base_uri' => 'https://exampleuploadrx.net',
]);
$response = $client->request('POST', '/api/upload', [
'json' => [
'front_photo' => new CURLFile('photo-1-214x300.jpg;type=image/jpeg'),
'back_photo' => new CURLFile('photo-2-214x300.jpg;type=image/jpeg'),
],
'headers' => $headers,
]
);
I have collection on postman and it is working fine. But now, I am trying to convert this PHP cURL
curl --location --request POST 'https://test.url.com' \
--header 'Content-Type: application/x-www-form-urlencode' \
--header 'Authorization: Basic 1234567890' \
--data-urlencode 'abc_id=123456' \
--data-urlencode 'grant_type=client_credentials' \
--data-urlencode 'scope=req-auth/all'
Here is my code:
$headers = [
"Authorization: Basic 1234567890",
"Content-Type: application/x-www-form-urlencode"
];
$data = [
"abc_id" => '123456',
"grant_type" => 'client_credentials',
"scope" => 'req-auth/all',
];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 0,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($data)
));
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$response = curl_exec($curl);
$errno = curl_errno($curl);
curl_close($curl);
print_r($response);
I tried this code but it is not working on my end How to include 'data-urlencode' using php curl library
I am trying to post to an external API, that accepts a XML file. The content type is application/octet-stream.
This is my code:
return Http::withToken(config('sproom.auth_token'))
->withHeaders([
'Content-Type' => 'application/octet-stream'
])
->attach('xml', file_get_contents('myfile.xml'), 'myfile')
->post('https://example.org/api/documents')->json();
When posting the above, I get an API error back: Cannot find format for document. No further documentation exists.
I am guessing that the xml file is not being sent correctly as application/octet-stream.
The external API is using Swagger as "documentation", and if I upload the XML file using the Swagger UI, I get a success response. Here in cURL:
curl -X POST "https://example.org/api/documents" -H "accept: */*" -H "Authorization: Bearer vmFrxk2+7......." -H "Content-Type: application/octet-stream" -d {}
I am not sure what I am doing wrong?
I don't think that the content type of the request is application/octet-stream, Also in the curl request you have written above has a -d
curl -X POST "https://example.org/api/documents" -H "accept: */*" -H "Authorization: Bearer vmFrxk2+7......." -H "Content-Type: application/octet-stream" -d {}
This is what is written in man curl
-d, --data
(HTTP) Sends the specified data in a POST request to the HTTP
server, in the same way that a browser does when a user has
filled in an HTML form and presses the submit button. This will
cause curl to pass the data to the server using the content-type
application/x-www-form-urlencoded. Compare to -F, --form.
If you are using ->attach, I guess the request should be multipart/form-data.
So please do see which one you need I guess you can't post files in application/x-www-form-urlencoded.
I don't think there is an option currently to attach content type to a file in attach(), so you can try using guzzle directly. It comes with laravel by default and httpclient is a wrapper over it to reduce the code.
Here is the direct guzzle code
$client = new \GuzzleHttp\Client(['headers' => ['Authorization' => 'Bearer ' . config('sproom.auth_token')]]);
$client->request('POST', 'https://example.org/api/documents', [
'headers' => [
'Accept' => '*/*'
],
'multipart' => [
[
'name' => 'myfile',
'contents' => file_get_contents('myfile.xml'),
'headers' => ['Content-Type' => 'application/octet-stream'],
'filename' => 'myfile.xml'
]
]
]);
Ultimately, I ended up using Guzzle directly:
$client = new \GuzzleHttp\Client(['headers' => ['Authorization' => 'Bearer ' . config('sproom.auth_token')]]);
try {
return $client->request('POST', config('sproom.api_url') . '/api/documents', [
'body' => file_get_contents('myfile.xml'),
'headers' => [
'Content-Type' => 'application/json',
]
])->getBody();
} catch (RequestException $exception) {
return json_decode($exception->getResponse()->getBody()->getContents());
}
I need to replace curl with file_get_content ( I have removed sensitive informations ):
curl -F 'client_id=aaa' \
-F 'client_secret=bbb' \
-F 'grant_type=authorization_code' \
-F 'redirect_uri=http://testtest.altervista.org/instagram/' \
-F 'code=ccc' \
https://api.instagram.com/oauth/access_token
I tryed this but doens't work ( it return nothing ):
$url = 'https://api.instagram.com/oauth/access_token?client_id=aaa&client_secret=bbb&grant_type=authorization_code&redirect_uri=http://testtest.altervista.org/instagram/&code=ccc';
print json_decode(file_get_contents($url));
Why ?
Not sure why you want to change cURL to a file_get_contents, but I would at least make sure to encode the querystring you're trying to use. You could use http_build_query to get proper results
$query = array(
'client_id' => 'aaa',
'client_secret' => 'bbb',
'grant_type' => 'authorization_code',
'redirect_uri' => 'http://testtest.altervista.org/instagram/',
'code' => 'ccc
);
$domain = 'https://api.instagram.com/oauth/access_token?'
$url = $domain . http_build_query($data);
print json_decode( file_get_contents($url) );
This is a curl request from Stripe API curl method:
curl https://api.stripe.com/v1/accounts \
-u sk_test_**********: \
-d managed=false \
-d country=US \
-d email="bob#example.com"
Right now I have this unirest code:
<?php Unirest\Request::auth(Config::get("stripe.secret.api_key"), '');
$headers = array(
"Content-Type" => "application/json"
);
$body = array(
"managed" => $_managed,
"country" => $_country,
"email" => $_email,
);
$response = Unirest\Request::post("https://api.stripe.com/v1/accounts", $headers, $body);
return array(
'status' => 'success',
'message' => $response
); ?>
Stripe returns method is wrong. I think its the -u param in curl.
I don't have much idea about Unirest. As you are facing issue with -u header, you can use the authorization parameter inside the url as below.
https://sk_test_RxRTXF1CDHuRw3ZUdynxnG6P:#api.stripe.com/v1/accounts
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^