I am on a work project using Codeigniter 3. I am trying to hit php restful api with basic authentication using this code generated from postman(already tested in postman and it works) :
<?php
use chriskacerguis\RestServer\RestController;
require APPPATH . 'libraries/RestController.php';
class Api_S extends RestController
{
public function index()
{
parent::__construct();
}
public function get_dp()
{
$curl = curl_init();
$username = "xxx";
$password = "xxx";
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://xxx',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => 'start=2&limit=10',
CURLOPT_USERPWD => $username . ":" . $password,
CURLOPT_HTTPAUTH => CURLAUTH_BASIC,
CURLOPT_HEADER => true,
CURLOPT_HTTPHEADER => array(
'api_auth_key: xxx',
'Authorization: Basic xxx',
'Content-Type: application/x-www-form-urlencoded'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
}
}
But whenever i try to redirect to call the function get_dp() in Api_S controller the browser shows
which ask username and password, but i already defined the username, password and the token in php curl code above. Even when i input it in the alert input field and clicked sign in it wont do anything. What i expect is when i redirect to call the function in api controller it will simply run the function (it will echo the api response) without asking for authorization again since i already include it in the php curl code above. What did i do wrong?How do i achieve that?
UPDATE 1 I also tried to change the code without specifying the authorization information in CURLOPT_USERPWD since its already stated in the request header (CURLOPT_HTTPHEADER) with base64 encoding like below:
<?php
use chriskacerguis\RestServer\RestController;
require APPPATH . 'libraries/RestController.php';
class Api_S extends RestController
{
public function index()
{
parent::__construct();
}
public function get_dp()
{
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://xxx',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => 'start=2&limit=10',
CURLOPT_HEADER => true,
CURLOPT_HTTPHEADER => array(
'api_auth_key: xxx',
'Authorization: Basic xxx',
'Content-Type: application/x-www-form-urlencoded'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
}
}
But the above code returns the same result as the previous code.
I also tried using similar code as the answer in this link by using curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password); and adjust it to my needs like below:
<?php
use chriskacerguis\RestServer\RestController;
require APPPATH . 'libraries/RestController.php';
class Api_S extends RestController
{
public function index()
{
parent::__construct();
}
public function get_dp()
{
$username = "xxx";
$password = "xxx";
$ch = curl_init('https://xxx');
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, 'start=2&limit=10');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$return = curl_exec($ch);
curl_close($ch);
}
}
and it still shows the same result.