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
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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 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 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!
I'm trying to figure out how to do this CURL command:
curl https://uploads.stripe.com/v1/files \
-u sk_test_OCHANGEDPcDr0: \
-F purpose=identity_document \
-F file="#/path/to/a/file.jpg"
In PHP, I'm using this PHP script:
https://github.com/php-mod/curl
And tried this:
$curl->setBasicAuthentication('-u', 'sk_live_CHANGED');
$curl->post('https://uploads.stripe.com/v1/files', array(
'purpose' => 'dispute_evidence',
'file' => $targetFile,
));
But I think the syntax is completely wrong, I'm really new to curl and don't understand what all the different -u and -F commands PHP equivalents are.
The -u username:password option translates to:
$curl->setBasicAuthentication('username', 'password');
To upload a file, you need to put # before the filename, just like in the curl command.
$curl->post('https://uploads.stripe.com/v1/files', array(
'purpose' => 'dispute_evidence',
'file' => '#' . $targetFile,
));
You'll probably want to try something like this:
$headers = array(
'Authorization: Bearer ' . $your_api_key,
'Content-Type: multipart/form-data'
);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://uploads.stripe.com/v1/files',
CURLOPT_POST => true,
CURLOPT_INFILESIZE => '10000',
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => array(
'purpose' => 'dispute_evidence',
'file' => '#' . realpath($path_to_file)
)
));
$resp = curl_exec($curl);
curl_close($curl);
EDIT: Alternatively, you can upload files directly from the browser like this.
If you have a functional curl command invocation, you can get what you are after by adding the --libcurl filename.c option.
This will generate a C program to do what your command line did. Though you are after PHP and not C, this is still useful because they both use the same names for various options.
I am using https://apptweak.io api,
they didn't mention PHP example in their code example...
please tell me, how to understand and write Curl API example into PHP curl
Here is simple "appteak.com" curl example
curl -G https://api.apptweak.com/ios/applications/284993459/informations.json \
-d country=us \
-d language=en \
-d device=iphone \
-H "X-Apptweak-Key: my_api_key"
Here is other example
$ curl -H "X-Apptweak-Key: my_api_key" \
https://api.apptweak.com/android/applications/com.facebook.katana.json?country=be&language=nl
How to write these curl in PHP?
i did this in php, after google, but its not work for me.
// Get cURL resource
$curl = curl_init();
// Set some options - we are passing in a useragent too here
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://api.apptweak.com/android/applications/com.facebook.katana.json?country=be&language=nl',
// CURLOPT_USERAGENT => 'Codular Sample cURL Request',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
'X-Apptweak-Key' => "my_api_key",
)
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
var_dump($resp ); //return false
I find correct answer
$token = "my_api_key";
$url = "http://URL.com";
$options = array('http' => array(
'header' => array("X-Apptweak-Key: $token"),
));
$context = stream_context_create($options);
$result = file_get_contents($url, 0, $context);