Sending Curl Request with Username and Password [PHP Code]

To make a Curl request with user credentials (with username and password ), you need to use the --user "username:password" command line parameter and pass the username and password to Curl. The username and password are separated by colons. In this Curl Request with User Сredentials Example, we send a username and password to the ReqBin echo URL. Click Run to execute the Curl with Username and Password example online and see the results. The PHP code was automatically generated for the Curl Request Credentials example.
Sending Curl Request with Username and Password [PHP Code] Run
curl https://reqbin.com/echo/get/json
	--user "name:password"
Updated: Viewed: 46099 times
PHP code for Curl Request Credentials example

PHP code for Curl Request Credentials Example

This PHP code snippet was generated automatically for the Curl Request Credentials example.
<< Back to the Curl Request Credentials example

What is Curl?

Curl (Client URL) is a command-line tool that can transfer data to/from a server using a number of network protocols, including HTTP, HTTPS, SCP, SFTP, FTP, and works on almost any platform, including Linux, Windows, and macOS. Curl is used for API testing, has built-in support for proxies, SSL, HTTP Cookies, certificate validation, user authentication.

What is HTTP Authentication?

HTTP Authentication is the process that determines whether the client is authorized to access the requested resource. HTTP supports authentication from the box to restrict access to protected resources.

What is Basic Authentication?

Basic Authentication is an authentication method built into the HTTP protocol. Basic Authentication is based on base64 encoded text and sends the username and password obfuscated but utterly readable by anyone listening on the network between you and the remote server. For security reasons, Basic Authentication should only be used in conjunction with other security mechanisms such as HTTPS and SSL.

What are Credentials?

Credentials are cookies, authorization headers, TLS client certificates, by which a client obtains credentials from a service or user and secures this information for future presentation of the authentication purpose. Credentials used in authentication are digital documents that associate a user's identity with some form of proof of identities, such as a certificate or password.

How to make Curl request with Credentials

To tell Curl to send a request with HTTP authentication, you need to pass the credentials using the -u/-user command-line option and separate the username and password with a colon.

Curl With Credentials Request Example
curl https://reqbin.com/echo/get/json
   --user "name:password"

Server response to our Curl with Credentials request:

Curl With Credentials Response Example
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 19
Connection: keep-alive

{"success":"true"}

What is Proxy Authentication?

Proxy Authentication is a built-in HTTP mechanism that prevents unauthorized use of the proxy server. Proxy Authentication blocks the client's requests until the user provides valid credentials to access the proxy. To Authenticate Proxies with Curl, you need to use the -U/--proxy-user command-line options and provide credentials, separated by colons.

Curl Proxy Authentication Syntax
curl --proxy-user "user:password"

Cookie-based authentication: what does it entail?

A cookie is a data string sent from a web server to a browser and stored on a user's device. Browsers then send cookies back to the server with every request to the server. Websites typically use cookies to store authorization data in a user's browser so that users do not have to log in again each time they visit a website. Websites may use cookies to remember any information about a user, such as the items in their shopping cart. Each web framework implements the cookie-based authentication method differently, but they all set cookies that remember the authenticated user. Whenever a request is made to a web application that requires authentication, the web server can identify the user making the request using cookies.

Curl Cookie Based Authentication Syntax
curl --cookie "authCookie=my_auth_cookie"

See also

Generate code snippets for PHP and other programming languages

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