Converting Curl Commands to PHP Code

You can convert Curl commands to PHP code using the ReqBin Online Curl client. Curl to PHP Converter uses PHP Client URL Library (based on libcurl) for generated code to send HTTP requests. Using our Curl to PHP Converter, you can convert almost any Curl command into PHP code in just a few clicks. Curl Converter generates valid PHP code for all provided HTTP headers, data, and forms. Click Run to execute the Convert Curl to PHP example online and see the results.
Converting Curl Commands to PHP Code Run
curl -X POST https://reqbin.com/echo/post/json
	 -H "Content-Type: application/json" 
     -d "{\"login\":\"my_login\",\"password\":\"my_password\"}"
Updated: Viewed: 10493 times

What is PHP Curl Library?

Curl (short for Client URL) is a command-line tool created by Daniel Stenberg that allows you to send data to a server using many different types of protocols. Curl is based on the libcurl and currently supports HTTP, HTTPS, FTP, and SFTP. Curl has built-in support for SSL certificates, HTTP GET, POST, PUT methods, file uploads, form submissions. It supports proxies, cookies, and user + password authentication. Curl is widely used for testing and interacting with APIs. To use cURL in your PHP code, you need to install libcurl 7.10.5 or newer and compile PHP with the --with-curl option. The behavior of the PHP Curl functions is affected by the curl.cainfo setting in the php.ini file.

PHP Curl Example

To use Curl in PHP code, you must first call the curl_init() method and then the curl_setopt() method to set all your parameters. After that, you need to execute the curl_exec() method to send the request. Below is an example PHP Curl request:

PHP Curl Code Example
<?php

$url = "https://reqbin.com/echo";

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);

?>

Curl to PHP Example

The following is an example of converting Curl syntax to PHP code:

Curl Command Example
curl -X POST https://reqbin.com/echo/post/json
   -H "Content-Type: application/json" 
   -d "{\"login\":\"my_login\",\"password\":\"my_password\"}"

You will get generated PHP code:

PHP Code Example
<?php

$url = "https://reqbin.com/echo";

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

$resp = curl_exec($curl);
curl_close($curl);
var_dump($resp);

?>

See also