Coder Social home page Coder Social logo

incarnate / curl-to-php Goto Github PK

View Code? Open in Web Editor NEW
397.0 397.0 146.0 82 KB

Convert curl commands to PHP code in your browser

Home Page: https://incarnate.github.io/curl-to-php/

License: MIT License

HTML 7.99% CSS 14.38% JavaScript 77.63%
converts-curl curl php

curl-to-php's People

Contributors

incarnate avatar mkp95 avatar nabikaz avatar pifagor87 avatar simmac avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

curl-to-php's Issues

Problem with post curl field and quotes

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

Problem with data-binary

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

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" ^

Problem with post audio and certificate file

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.

POST has to be explicitly defined using CURLOPT_CUSTOMREQUEST

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");

-F not supported

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!'

Issue with -F=

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')));

BUG: Cookies with `!` context value

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": ';...

--data-binary not work correctly

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}');

CURLOPT_POSTFIELDS should be array

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')));

Use single quote

Currently headers are done with single quotes. PHP style guides usually want single quotes.

Test case

curl -H "Travis-API-Version: 3" -H "Authorization: token aaaa" https://api.travis-ci.com/build/89494784

Current output


// 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);

--compressed is non-optimal

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.

Post Parameters

When There are Post parameters Still Request Type is set to get
Also Post parameters are totally not available in request

Forcing CamelCase on headers

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);

problem with -F and JSON

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

Bug: `CURLOPT_POST` should be set before `CURLOPT_POSTFIELDS`.

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 not supported

-L should be rewritten as: curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

data-binary wrong

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 working for this command

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"

SSL Optional

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

Unexpected handling of dollar signs (`$`) in URL during conversion.

Steps to reproduce:

  1. Paste the following string into the conversion box:
    curl 'http://sejm.gov.pl/Sejm8.nsf/posel.xsp?id=001&type=A&$$ajaxid=view%3A_id1%3A_id2%3AfacetMain%3A_id187%3AholdBiura'
  2. Wait until result is produced.

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.

not support form data

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.