About cURL to PHP Converter
PHP has a built-in cURL extension, and its options map closely to the flags of the cURL command line. This tool translates a cURL command into PHP code that uses curl_init(), curl_setopt_array() and curl_exec().
The method becomes CURLOPT_CUSTOMREQUEST, headers go into CURLOPT_HTTPHEADER, the body goes into CURLOPT_POSTFIELDS and -u becomes an Authorization header. A timeout and basic error handling are included, so the code is a safe starting point for a plugin, a script or a cron job.
How to use cURL to PHP Converter
- Paste the cURL command into the input box.
- Click Run Tool to generate the PHP code.
- Copy it into your project, replace placeholders, and add your own response handling.
Example
Input
curl -X POST https://api.example.com/users -H "Content-Type: application/json" -d '{"name":"Aman"}'Output
$ch = curl_init();
curl_setopt_array( $ch, array(
CURLOPT_URL => 'https://api.example.com/users',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 15,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
),
CURLOPT_POSTFIELDS => '{"name":"Aman"}',
) );
$response = curl_exec( $ch );
if ( curl_errno( $ch ) ) {
echo 'cURL error: ' . curl_error( $ch );
}
curl_close( $ch );Common uses
- Calling an API in a PHP script after testing it in a terminal.
- Moving a request from documentation into a WordPress plugin.
- Learning which CURLOPT constants correspond to which cURL flags.
Privacy and safety
This tool runs in your browser. The text you enter is processed on your device and is not sent to NHP Learning.
Frequently asked questions
Should I use cURL or wp_remote_get in WordPress?
Inside WordPress, prefer the HTTP API functions such as wp_remote_get() and wp_remote_post(). They respect site settings and work on hosts without the cURL extension.
How do I decode the JSON response?
After curl_exec(), pass the result to json_decode( $response, true ) to get an array.
Is SSL verification disabled?
No. The generated code keeps the default of verifying certificates, which is what you want in production.