Using Curl -u Option [PHP Code]

The curl -u (or --- user) command sends the user's basic authentication data to the server. The user's credentials must be provided directly after the -u command line switch. The curl -u command is used to authorize a user using the HTTP basic authentication scheme that is built into the HTTP protocol. Curl converts the provided user authentication data into a standard Authorization: Basic {base64string} HTTP header and sends it to the server along with the other data in your request. In this Curl -u example, we send a request with user credentials to the ReqBin echo URL. Click Run to execute the Curl Basic Authentication example online and see the results. The PHP code was automatically generated for the Curl -u Flag example.
Using Curl -u Option [PHP Code] Run
curl -u "login:password" https://reqbin.com/echo/get/json  
Updated: Viewed: 34896 times
PHP code for Curl -u Flag example

PHP code for Curl -u Flag Example

This PHP code snippet was generated automatically for the Curl -u Flag example.
<< Back to the Curl -u Flag example

What is Curl?

Curl is a command-line utility with a cross-platform library (libcurl) for transferring data between servers, designed to work without user intervention. Curl can upload or download data, submit web forms, and make API requests using over 25 protocols, including HTTP, HTTPS, FTP, and SFTP. Curl has built-in support for HTTP Cookies, certificate validation, SSL, user authentication and is available on all modern platforms, including Linux, Windows, and Mac.

What is HTTP Authorization?

HTTP authorization is a built-in mechanism for user authentication and access control to protected resources. HTTP authorization is performed by sending authentication credentials in the Authorization header to authenticate the HTTP client on the server.

What is Basic Authorization?

Basic Authorization is one of the standard authorization schemes that is used to access protected resources on the server. For Basic Authorization, the client sends an HTTP request header field in the form "Authorization: Basic {base64string}". In base64String, a username and password are encoded in Base64 and concatenated with a single colon ":". Because the username and password can be easily decrypted from a base64 string, basic authentication does not provide the highest level of security. For security reasons, Basic Authorization should only be used over HTTPS/SSL connections. Curl supports Basic Authorization out of the box.

Authorization Header Syntax
Authorization: Basic {base64string}

How to send the Basic Authentication data using Curl?

To send a Curl request with Basic Authorization credentials, you can use the -u (or --user) command line parameter:

Curl Basic Authentication Syntax
curl -u "login:password" [URL]

Where:
  • -u: specifies the username and password that will be used to authenticate the server.

See also

Generate code snippets for PHP and other programming languages

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