Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert Php CURL request to command line curl

Tags:

php

curl

php-curl

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.

like image 639
HItesh Tank Avatar asked Mar 08 '17 12:03

HItesh Tank


People also ask

How do I cURL using CMD?

cURL makes HTTP requests just like a web browser. To request a web page from the command line, type curl followed by the site's URL: The web server's response is displayed directly in your command-line interface. If you requested an HTML page, you get the page source -- which is what a browser normally sees.


2 Answers

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.

like image 122
Vasiliy Avatar answered Oct 18 '22 09:10

Vasiliy


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

like image 21
Gergely Lukacsy Avatar answered Oct 18 '22 08:10

Gergely Lukacsy