Sending Curl Request with Basic Authentication Credentials [PHP Code]

To send basic auth credentials with Curl, use the "-u login: password" command-line option. Curl automatically converts the login: password pair into a Base64-encoded string and adds the "Authorization: Basic [token]" header to the request. In this Curl request with Basic Auth Credentials example, we send a request with basic authorization credentials to the ReqBin echo URL. Click Run to execute the Curl Basic Authentication Credentials Example online and see the results. The PHP code was automatically generated for the Curl Basic Auth example.
Sending Curl Request with Basic Authentication Credentials [PHP Code] Run
curl https://reqbin.com/echo
   -u "login:password"
Updated: Viewed: 84978 times
PHP code for Curl Basic Auth example

PHP code for Curl Basic Auth Example

This PHP code snippet was generated automatically for the Curl Basic Auth example.
<< Back to the Curl Basic Auth example

What is Curl?

Curl is an open-source command-line tool for transferring data to or from the server using URL syntax. Curl supports over 25+ protocols, including HTTP, HTTPS, SFTP, FTP, and has built-in support for proxy, HTTP Cookies, SSL certificates. Curl is ideal for testing test APIs and works on platforms Linux, Windows, and macOS.

What is Basic Authentication?

Basic Authentication is a method for an HTTP client (such as a web browser) to provide a username and password to the server when making an HTTP request to protected resources. For Basic Authentication, the client sends an HTTP request header field in the form "Authorization: Basic base64String," where the base64String is a Base64-encoded username and password concatenated with a single colon. Basic authentication is not the most secure authentication method because the username and password can be easily decrypted from the base64 string. For security reasons, Basic Authentication should only be used over HTTPS/SSL connections.

Can I send Basic Auth Credentials using Curl?

Yes, Curl has built-in support for basic HTTP server authorization. To make a Curl request with basic authorization credentials, you need to use the following command-line parameter: -u username:password (or --user). Curl automatically converts the provided username:password pair into a Base64-encoded string and adds an appropriate HTTP header "Authorization: Basic bG9naW46cGFzc3dvcmQ=" to the request. If you omit part of the password in the command-line parameter, Curl will prompt the user for a password.

Curl Basic Auth Example
curl -u "login:password" [URL]

Curl Basic Authentication Examples

The following are examples of sending a Curl request with basic authentication:

Sending Credentials to Curl

To pass credentials using Basic Authentication, you can use the -u or --user command-line option. The following is an example of sending Basic Auth credentials to the ReqBin echo URL:

Curl Basic Auth Example
curl https://reqbin.com/echo
   -u "login:password"

Alternative Curl Basic Authentication Method

Alternatively, you can pass the basic auth credentials using the Curl -H "Authorization: Basic [token]" command-line option. The -H parameter passes the authorization header to the server, like any other custom header, without additional processing.

Alternative Curl Basic Authentication Example
curl https://reqbin.com/echo
   -H "Authorization: Basic bG9naW46cGFzc3dvcmQ"

See also

Generate code snippets for PHP and other programming languages

Convert your Curl Basic Auth request to the PHP, JavaScript/AJAX, Node.js, Curl/Bash, Python, Java, C#/.NET code snippets using the PHP code generator.