PHP Curl Request with Username and Password Example

By Hardik Savani November 5, 2023 Category : 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...

Tags :
Shares