cURL with Basic auth in PHP
Paste a curl command that uses Basic auth and get PHP code using ext-curl. Toolpaper also detects secrets and offers a one-click redaction before sharing.
Quick answer
To convert a cURL command to PHP, paste it into the Toolpaper workbench below. It parses the command in your browser and emits idiomatic PHP code — headers, query string, request body, authentication (Bearer, Basic, API key), and cookies are all wired up automatically. No data is uploaded; conversion is fully client-side.
Prefer a different target? See the full cURL to PHP reference, or jump to the same guide in JavaScript, Node.js, or Python.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.example.com/me");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_USERPWD, "alice:s3cret");
$response = curl_exec($ch);
curl_close($ch);
echo $response;Example
A representative curl command for this scenario. Paste your own above to convert an exact match.
curl https://api.example.com/me \
-u alice:s3cretImplementation notes
- -u user:pass is decoded and re-emitted as an idiomatic ext-curl auth primitive rather than a hand-rolled base64 header.
- Secrets are visible in the parsed request. Use the Clean panel's redact-and-diff flow before you paste the command anywhere.
- Shareable links Base64-encode the input — they never leave your browser, and cleaned versions produce different links.
Common mistakes
- Hard-coding user:pass into source instead of reading from env vars — the generated snippet marks credentials so you notice.
- Not rotating a leaked credential because "it's only in a screenshot" — screenshots get OCR'd, redact first.
- Trusting HTTPS alone for API keys in query strings — proxies log URLs. Prefer headers, which the converter defaults to.
Common questions
- How is Basic auth rendered in PHP?
- -u user:pass becomes an idiomatic Basic auth call — auth=(user, pass) in Python, SetBasicAuth in Go, .setBasicAuth in Java.
- Can I redact the secret before sharing?
- Yes. Open the Clean panel — Toolpaper detects Bearer tokens, Basic credentials, and common API-key patterns and replaces them with <REDACTED> in a diffable view.
- Does anything leave my browser?
- No. Parsing, redaction, and generation are 100% client-side.
More auth flows in PHP
PHP hub →The same guide in other languages
All guides →In JavaScript
/how-to/curl-basic-auth-in-javascript
In Node.js
/how-to/curl-basic-auth-in-node
In Python
/how-to/curl-basic-auth-in-python
In Go
/how-to/curl-basic-auth-in-go
In Ruby
/how-to/curl-basic-auth-in-ruby
In Rust
/how-to/curl-basic-auth-in-rust
In Java
/how-to/curl-basic-auth-in-java
In Kotlin
/how-to/curl-basic-auth-in-kotlin
Full PHP reference on the cURL to PHP hub, or open the main converter for all 18 targets.