incarnate / curl-to-php Goto Github PK
View Code? Open in Web Editor NEWConvert curl commands to PHP code in your browser
Home Page: https://incarnate.github.io/curl-to-php/
License: MIT License
Convert curl commands to PHP code in your browser
Home Page: https://incarnate.github.io/curl-to-php/
License: MIT License
With Apple Push Notification Service now requiring cert and private key, please consider adding support for -cert and --key and --key-type
Sample curl for APNS:
curl -v --header "apns-topic:appname.com.asdf" --header "apns-push-type: alert" --cert "apns.pem" --key "apnskey.pem" --key-type PEM --data '{"aps":{"alert":"test APNS"}}' --http2 https://api.push.apple.com/3/device/d757574cb975099e682c33084be41583aeb661d3293c3030a1c68572a39b0000
Yours is a useful project.
Thanks
If, we have console curl:
-d type_name=xxx -d attributes='{"name":"test"}'
we get convert value
curl_setopt($ch, CURLOPT_POSTFIELDS, "type_name=xxx&attributes='{"name":"test"}'");
It is bug. Right convert:
curl_setopt($ch, CURLOPT_POSTFIELDS, "type_name=xxx&attributes={"name":"test"}");
I solved the issue here:
#6
Hello. Thanks for your script.
I tried convert dropbox api examples and got bug.
https://www.dropbox.com/developers/documentation/http/documentation#files-upload
curl -X POST https://content.dropboxapi.com/2/files/upload \
--header "Authorization: Bearer <get access token>" \
--header "Dropbox-API-Arg: {\"path\": \"/Homework/math/Matrices.txt\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}" \
--header "Content-Type: application/octet-stream" \
--data-binary @local_file.txt
And we got:
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://content.dropboxapi.com/2/files/upload");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$post = array(
"file" => "@" .realpath("local_file.txt")
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = "Authorization: Bearer <get access token>";
$headers[] = "Dropbox-Api-Arg: {\"path\": \"/Homework/math/Matrices.txt\",\"mode\": \"add\",\"autorename\": true,\"mute\": false}";
$headers[] = "Content-Type: application/octet-stream";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
Error in postfilds
. Correct variant:
curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents(local_file.txt));
Your code don't convert Form data to php
demo:
--data-raw "id=438^&novel=Going-Back-and-Forth-Between-Earth-and-The-Other-World-with-Space-Time-Magic^&max=430^&page=2" ^
curl -T foo.txt ...
isn't supported
I got an issue with sending audio file and certificate file to get data from server, I have tried with the code like below
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'my_api_url');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
//setting file
curl_setopt($ch, CURLOPT_POSTFIELDS, array('file'=>'@'.realpath('audio/sound.wav')));
$headers = array();
$token = 'mytoken';
$headers[] = 'token:'.$token;
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CAINFO ,dirname(__FILE__).'\certification.crt');
curl_setopt($ch, CURLOPT_CAPATH,dirname(__FILE__).'\certification.crt');
// curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
echo $result;
The CURL format for the code above like below
curl --request POST --cacert $CERT_FILE -H "token: $YOUR_TOKEN" -F "file=@$PATH_TO_FILE" my_api_url
But it doesn't working, Are there any suggestion to solve this issue. Thank you in advance for any sugguestion.
I tried running two consecutive PHP cURL outputs with two to the same URL, first one is set to OPTIONS:
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "OPTIONS");
Second one is supposed to be POST but only the CURLOPT_POST was set:
curl_setopt($ch, CURLOPT_POST, 1);
But after running the PHP script the second -supposedly- "POST" request kept returning empty result for no particular reason, but after enabling the verbose flag on cURL I discovered that the second request was still set to OPTIONS, so I had to add:
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
Example not converted:
curl -s --user 'api:YOUR_API_KEY' \
https://api.mailgun.net/v3/YOUR_DOMAIN_NAME/messages \
-F from='Excited User <mailgun@YOUR_DOMAIN_NAME>' \
-F to=YOU@YOUR_DOMAIN_NAME \
-F [email protected] \
-F subject='Hello' \
-F text='Testing some Mailgun awesomeness!'
Currently Curlopt_COOKIE isnt taken care off
curl -X POST "https://api.squarespace.com/1.0/commerce/products/123/images" -i -H "Authorization: Bearer YOUR_API_KEY_OR_OAUTH_TOKEN" -H "User-Agent: YOUR_CUSTOM_APP_DESCRIPTION" -F [email protected]
Results in:
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.squarespace.com/1.0/commerce/products/123/images');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'file' => '@' .realpath('steak.png'));
$headers = array();
$headers[] = 'Authorization: Bearer YOUR_API_KEY_OR_OAUTH_TOKEN';
$headers[] = 'User-Agent: YOUR_CUSTOM_APP_DESCRIPTION';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
It should be:
curl_setopt($ch, CURLOPT_POSTFIELDS, array('file' => '@' .realpath('steak.png')));
When have a !
char (or maybe similar chars) in the cookies (Cookie: FOO=BAR!BAZ
), the chrome Copy as cURL(bash)
menu return be like this:
curl 'http://example.com/' \
-H $'Cookie: FOO=BAR\u0021BAZ' \
--compressed
When I use that in the https://incarnate.github.io/curl-to-php/ url, see:
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://example.com/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
$headers = array();
$headers[] = $_ENV["'Cookie": ';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
You can see appears a syntax bug in $headers[] = $_ENV["'Cookie": ';...
curl -H "server-method: SendNew" -H "client-ver: 105" --data-binary '{"a":[],"y":null,"km":22}' --compressed http://localhost/post.php
curl-to-php generate
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
need to
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); curl_setopt($ch, CURLOPT_POSTFIELDS, '{"a":[],"y":null,"km":22}');
curl command:
curl -X PUT -F "file=@./test.txt" -H "X-Object-Meta-One: two" -H "X-Object-Meta-Three: Four" http://localhost:8080/test.txt
result line 8:
curl_setopt($ch, CURLOPT_POSTFIELDS, 'file' => '@' .realpath('./test.txt'));
should be:
curl_setopt($ch, CURLOPT_POSTFIELDS, array('file' => '@' . realpath('./test.txt')));
Currently headers are done with single quotes. PHP style guides usually want single quotes.
curl -H "Travis-API-Version: 3" -H "Authorization: token aaaa" https://api.travis-ci.com/build/89494784
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.travis-ci.com/build/89494784");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$headers = array();
$headers[] = "Travis-Api-Version: 3";
$headers[] = "Authorization: token aaaa";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
curl --compressed
now generates this code:
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
While curl --compressed localhost --libcurl -
will show that a better code version would be:
curl_setopt($ch, CURLOPT_ENCODING, '');
IOW: an empty string. According to the docs:
To aid applications not having to bother about what specific algorithms this particular libcurl build supports, libcurl allows a zero-length string to be set ("") to ask for an Accept-Encoding: header to be used that contains all built-in supported encodings.
With an empty string, we can be sure that curl will ask for compression methods it knows how to decompress, which may include the ones you've picked but may not and may also include brotli and others in the future.
When There are Post parameters Still Request Type is set to get
Also Post parameters are totally not available in request
The --data-urlencode is currently not handled by the script. Would be nice if someone could add it,
The tool is CamelCasing header names. It also deduplicates afterwards. Which is creating confusion. Look at the below request. I need both Cache-Control & cache-control to be sent but the converter removes the second header. Also, check apikey: it converts it to Apikey
Here's the request
curl -X POST https://thathost.com \
-H 'Cache-Control: no-cache' \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'apikey: xxx' \
-H 'cache-control: no-cache' \
-d 'something-doesntmatter-payload'
that generates
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://thathost.com');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "something-doesntmatter-payload");
$headers = array();
$headers[] = 'Cache-Control: no-cache';
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
$headers[] = 'Apikey: xxx';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
// it would be nice if something like this existed:
$bash_command = curl_get_bash_equivalent($curl);
// 'curl -X POST "http://..."'
curl_close($curl);
curl -H "Content-Type: multipart/form-data" -H "X-Token: token" -F payloadSend="{ "alias": "000000", "startDate": null,"endDate": null, "period": null, "recurrencesNumber": null,"recurrencesInterval": null, "validityPeriod": null, "sentNotify":false, "deliveredNotify": false, "msisdnToNotify":null, "sr": false,"campaignChannel": { "channelName": "UCP", "zone": null,"zoneType": null, "collectionTime": null }, "campaignMessage": {"messageType": null, "messageContent": "text","template": null, "file": "string", "msisdn": "000000","listMsisdn": null, "fileMsisdn": "null", "name": null,"surname": null, "extra": null, "status": "string", "delivered": "string" } };type=application/json" "https://xxxxxxxx.com/channels/UCP/campaigns?destination=msisdn&message=static&delivery=immediate&type=single"
result:
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, '"alias":');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'payloadSend' => '"{');
$headers = array();
$headers[] = 'Content-Type: multipart/form-data';
$headers[] = 'X-Token: token';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
can someone help me?
thanks
curl -X POST -H "Authorization: Bearer ABCDE" -H "Content-Type: application/json" -d '{"entity_id": "script.server_boot_test"}' \https://XXX.duckdns.org:8123/api/services/script/turn_on
Thanks for help
--insecure should be rewritten as: curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
CURLOPT_POST
should be set before CURLOPT_POSTFIELDS
. Otherwise don't post data.
Following code has wrong:
<?php
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt ($ch, CURLOPT_POST, 1);
?>
But this one works.
<?php
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postfields);
?>
-L should be rewritten as: curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
FYI I tried out a curl that included -H 'content-type: application/json'
and data sent as --data-binary '{...
, and this script spit out $headers[] = "Content-Type: application/x-www-form-urlencoded";
, when it should have been $headers[] = "Content-Type: application/json";
.
Otherwise, worked great!
not work for this command
curl -v -X POST -F "name=4444" -F "service-key=zzzz" -F ufile=@"/data/publicweb/xxx.zip" "http://1.0.13.13
2:56/upload"
curl --data-binary '123' --compressed http://127.0.0.1/post.php
With --compressed adding in php :
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
will be good if you add some optional option like 'CURLOPT_SSL_VERIFYPEER', because when the url using SSL the curl will be error. Add 'curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0);' and boom work
Steps to reproduce:
curl 'http://sejm.gov.pl/Sejm8.nsf/posel.xsp?id=001&type=A&$$ajaxid=view%3A_id1%3A_id2%3AfacetMain%3A_id187%3AholdBiura'
Result:
// Generated by curl-to-PHP: http://incarnate.github.io/curl-to-php/
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $_ENV["http://sejm.gov.pl/Sejm8.nsf/posel.xsp?id=001&type=A&ajaxid=view%3A_id1%3A_id2%3AfacetMain%3A_id187%3AholdBiura"]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
In particular, please note the part: $_ENV["http://sejm.gov.pl/Sejm8.nsf/posel.xsp?id=001&type=A&ajaxid=view%3A_id1%3A_id2%3AfacetMain%3A_id187%3AholdBiura"]
.
I see there's something clever going on here, because of adding the $_ENV
bit. This is easy to catch because on executing this, PHP will complain about 'no index found' (in array $_ENV
). However, only after quite some time I also realized the dollar signs ($) in the bit $$ajaxid
in the input string were dropped in the resulting code, and caused the requests not to work. If this needs to be done to prevent some XSS, etc, then could the converter be more explicit about it?
Proposed solution:
Provide a warning label if dollar characters are removed from input URL.
Thank you for a great tool.
curl -X POST \
'http://xx.cc/xm/lllopas?authorization=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ2ZXJzaW9uIjoiMS4xLjAiLCJicmFuZCI6ImFwcGxlIiwibW9kZWwiOiJpcGhvbmUgeCIsInN5c3RlbSI6Imlvc18xMi4yLjIiLCJmb3JtIjoiaW9zX2FwcCIsImV4cCI6MTU2MzI4NjU2Mn0.1Vtzez04JAZexfcKQ0Kg7_byB6sJ91tfo9duDXW316Q' \
-F openid=oXbIZ4zuenxYO6M9OfTpCr961Nl4 \
-F unionid=1120 \
-F nickname=1234561 \
-F 'avatar=https://www.baidu.com/img/superlogo_c4d7df0a003d3db9b65e9ef0fe6da1ec.png?where=super'
like this
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.