Convert curl to PHP

curl2code converts curl commands to PHP code using cURL extension or Guzzle library. The conversion runs in your browser via WebAssembly for complete privacy. For similar server-side languages, check our curl to Python converter or curl to Ruby. Below are practical PHP examples.

How to copy curl from your browser

  1. 1

    Open DevTools

    Press F12 or Ctrl+Shift+I to open your browser developer tools.

  2. 2

    Go to the Network tab

    Click the Network tab and perform the action that triggers the HTTP request.

  3. 3

    Copy as cURL

    Right-click the request → CopyCopy as cURL. Then paste it above.

Frequently Asked Questions

What is PHP cURL?

PHP's curl extension is a wrapper around the C libcurl library, providing powerful HTTP client capabilities. It supports virtually all protocols and features that curl itself supports. Initialize with curl_init(), set options with curl_setopt(), and execute with curl_exec(). curl2code also supports Guzzle and Requests as alternatives.

PHP cURL vs Guzzle vs Requests — which should I use?

PHP cURL is low-level and built-in — no Composer needed. Guzzle is the most popular PHP HTTP client with PSR-7 support, middleware, and async requests. Requests offers a simple API for quick tasks. Use Guzzle for modern projects, PHP cURL for minimal setups. For similar options in other languages, see curl to Python.

How to handle authentication in PHP?

With cURL: curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer token']). For Basic auth: curl_setopt($ch, CURLOPT_USERPWD, 'user:pass'). Guzzle uses 'auth' => ['user', 'pass'] or 'headers' => ['Authorization' => 'Bearer token']. curl2code handles -u and Bearer flags automatically.

How to send multipart form data in PHP?

With cURL: pass an array to CURLOPT_POSTFIELDS containing new CURLFile('path') for files. Guzzle uses 'multipart' option with field/file arrays. curl2code converts -F flags to the correct format for each PHP library variant.

How to handle HTTP errors in PHP?

With cURL: check curl_errno($ch) for transport errors and parse the response status manually. Guzzle throws RequestException on failures — catch ClientException (4xx), ServerException (5xx), and ConnectException (network). Always call curl_close($ch) to free resources.

How to make async HTTP requests in PHP?

Guzzle supports async via $client->requestAsync('GET', $url) returning a Promise. Use Promise\Utils::all() for concurrent requests. With cURL, use curl_multi_* functions for parallel execution. For truly async PHP, consider ReactPHP or Amp frameworks with their HTTP clients.

How to set timeouts in PHP?

With cURL: CURLOPT_CONNECTTIMEOUT for connection and CURLOPT_TIMEOUT for total request time. Guzzle accepts 'connect_timeout' and 'timeout' in seconds. These map directly to curl's --connect-timeout and --max-time options.

How to use a proxy in PHP?

With cURL: curl_setopt($ch, CURLOPT_PROXY, 'http://proxy:8080'). For SOCKS5: add CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5. Guzzle uses 'proxy' => 'http://proxy:8080'. curl2code converts -x and --proxy flags to the corresponding PHP code.

How to send a POST request with JSON body in PHP?

Convert a curl POST command like curl -X POST -H "Content-Type: application/json" -d '{"key":"value"}' URL to PHP using curl2code. The generated code uses curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)). curl2code automatically detects -d/--data flags with JSON content and sets the appropriate Content-Type header in the cURL code.

How to add Bearer token authorization in PHP?

Pass curl -H "Authorization: Bearer YOUR_TOKEN" URL to curl2code and it generates cURL code with the auth header: curl_setopt($ch, CURLOPT_HTTPHEADER, ['Authorization: Bearer TOKEN']). curl2code detects Bearer tokens from both -H "Authorization: Bearer ..." and --oauth2-bearer flags. For Basic auth, use -u user:pass.

How to set Content-Type header in PHP?

curl2code converts curl -H "Content-Type: application/json" URL to cURL code with the proper header: curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']). For -d data, curl defaults to application/x-www-form-urlencoded; for -F form uploads, it uses multipart/form-data. curl2code maps each to the correct cURL API.

PHP cURL extension vs Guzzle — which should I use?

The PHP cURL extension (curl_* functions) is built into PHP and requires no extra dependencies — ideal for simple requests or when you want minimal overhead. Guzzle is a full-featured HTTP client with middleware, async requests, PSR-7/PSR-18 support, and automatic retries. Use cURL for lightweight scripts, Guzzle for complex applications. curl2code generates code for both — select your preferred variant in the language dropdown.

Useful Links

curl Guides

Convert curl to Other Languages