31

How to translate following php curl request to curl executable command.

 $curlOpts = array(
                    CURLOPT_PORT           => "3000",
                    CURLOPT_URL            => 'www.example.com',
                    CURLOPT_RETURNTRANSFER => true,
                    CURLOPT_HTTPHEADER     => array("Cookie: connect.sid=aASD234SDFfds", "content-type:application/json"),
                    CURLOPT_POST           => true,
                    CURLOPT_POSTFIELDS     => {"email": "test.com",  
   "password": "123456"},
                );

curl_setopt_array($ch, $curlOpts);
 $output = curl_exec($ch);

Respected Curl command which I want

curl -X GET --header 'Accept: application/json' 'http://www.example.com?sort=clicks&order=des'


curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{ \ 
   "email": "test.com", \ 
   "password": "123456" \ 
 }' 'http://example.com/login'

Please help me for the same.

HItesh Tank
  • 636
  • 1
  • 6
  • 13
  • 1
    Simply read the manual for cURL and you will manage to do this on your own ;-) [Info 1](http://php.net/manual/en/function.curl-setopt.php) ----- [Info 2](http://php.net/manual/en/curl.constants.php) – Alex Odenthal Mar 08 '17 at 12:42
  • Just put the things that you need and variables and build the string as you want it, it's not a big deal ... – codtex Mar 08 '17 at 12:42
  • Php curl function not return curl command url ? @AlexOdenthal – HItesh Tank Mar 08 '17 at 12:52
  • Your question has nothing to do with programming, and a little bit too broad, please try to be a little more specific next time. As for your problem, I posted a suggestion you can start with. Cheers. – Gergely Lukacsy Mar 08 '17 at 13:42
  • 14
    this question is completely fine and related to programming, I am also looking for solution to extract PHP curl call to command line command. – talsibony Jul 12 '18 at 11:12

3 Answers3

3

Please note that this is a workaround only.

Try to assemble your http request in Postman which is a really rich tool for testing APIs. It is a Chrome plugin, and available from the Chrome webstore for free (link). Alternatively, you can install it as a standalone client too from their website.

It has a nice feature which let you to grab the curl command wrapped in different languages/formats of your preference. In your case, in BASH too.

Put the request together then:

  • click "Code" (Right under the "Save" button)
  • choose "cURL" from the dropdown list
  • click "copy to Clipboard"

...and that's it, you have your preformatted cURL command line.

Also, there's a tool called cURL-to-PHP written in JavaScript, which does the exact thing you'd like to do.

If you take some time, you can translate the converter logic to PHP with little effort (eg. you don't need to research every aspects of cURL internals).

Gergely Lukacsy
  • 2,881
  • 2
  • 23
  • 28
  • 1
    Thank you for answer but I want to generate dynamically using php code and store into log file. – HItesh Tank Mar 09 '17 at 07:00
  • I see. Unfortunately, there's no PHP tool for that, but you can take a look for [cURL-to-PHP](https://github.com/incarnate/curl-to-php). It's basically doing exactly what you'd like to do, written in JavaScript. If you'd take the time, you can translate the converter code for PHP with little effort. – Gergely Lukacsy Mar 09 '17 at 08:41
  • 2
    This doesn't really answer the question. Hence not being accepted as "the solution" – ChristoKiwi Aug 14 '18 at 23:33
3

take a look at https://github.com/biganfa/php2curl. Start a webserver, send your request, and you will get the command line cURL version of the request using the library.

Vasily
  • 981
  • 10
  • 12
  • Doesn't really work when you are sending it to an external service from what I can tell. – Patrick Mar 05 '21 at 16:40
  • @Patrick you can send your request to [built-in web server](https://www.php.net/manual/en/features.commandline.webserver.php) anytime instead of external service as a workaround. – Vasily Mar 07 '21 at 22:36
  • Caution The eval() language construct is very dangerous because it allows execution of arbitrary PHP code. Its use thus is discouraged. If you have carefully verified that there is no other option than to use this construct, pay special attention not to pass any user provided data into it without properly validating it beforehand. – Yidir Jan 18 '23 at 20:23
1

Check the below link for PHP Constant values of CURL. E.X. While you check curlopt array by print_r($curlout). You get all constant values.

Array ( [78] => 30 [19913] => 1 [13] => 120 [32] => 0 [10018] => storeden-php-1.1b [10023] => Array ( [0] => key: f2e7fd282883935b7a52ae4757917e1ec6026076368641084feec997023001099 [1] => exchange: a5622d643d09785a679c381238a067cfb60edf0c2bdd290b6a1435d50e [2] => Expect: ) [10002] => ApiURL)

https://gist.github.com/bayleedev/4626922

check the above array keys where it defines the constant values CURLOPT_URL......................10002

Use the above constant identifier and add their respective identifier in Postman.

After that you can check the code with curl convert to transform PHP to curl commandline.

Hope that help you.

pmehta
  • 11
  • 2