ItSolutionStuff.com

PHP Curl Request with Username and Password Example

By Hardik Savani • May 14, 2024
PHP

This article is focused on php curl request with username and password. if you want to see example of php curl basic auth username:$password then you are a right place. you can understand a concept of php curl authorization username:password. you can see php curl basic auth example. you will do the following things for php curl basic auth username password.

In this example, we will use CURLOPT_HTTPAUTH and CURLOPT_USERPWD to pass username and password for authorization. so let's see bellow simple example code here:

Example:

<?php

$username = 'username';

$password = 'password';

$URL = 'https://api.mywebtuts.com/api/users';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,$URL);

curl_setopt($ch, CURLOPT_TIMEOUT, 30);

curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);

curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);

curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");

$result=curl_exec ($ch);

$status_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close ($ch);

?>

i hope it can help you...

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

PHP Curl PUT Request Example Code

Read Now →

PHP Curl Delete Request Example Code

Read Now →

PHP Curl POST Request with Headers Example

Read Now →

PHP Curl Get Request with Parameters Example

Read Now →

PHP Curl Request with Certificate (cert pem file option) Example

Read Now →

Codeigniter Curl Post Request with Parameters Example

Read Now →

PHP CURL Post Request with Parameters Example

Read Now →

Laravel CURL Request Example using Ixudra/curl

Read Now →

PHP Download File from URL using CURL Request Example

Read Now →

How to Upload Image using jQuery Ajax in PHP?

Read Now →

How to Find Day Name from Specific Date in PHP?

Read Now →

How to Remove Null Values from Array in PHP?

Read Now →