How to consume an API with username and password using php?

Issue

I am developing a website for a company, internally we are trying to develop a small reservation system (I am a bit new to this). I consume the information for the reservations from an external API in which they gave me a username and password as well… but I can’t get the data. I am using the cURL method to consume the service and I only get a 404 url ​​not fund error. I’m not sure if I’m doing something wrong in the code or the problem is directly with the provider. This is my code:


require_once "credenciales.php";

/* URL DE API's */

$url = 'https://api.hyperguest.com/hg-apitude/hotel-api/1.0/hotels/';
$url0 = 'https://api.hyperguest.com/hg-apitude/hotel-api/1.0/checkrates/';
$url1 = 'https://api.hyperguest.com/hg-apitude/hotel-api/1.0/bookings/';
$url2 = 'https://api.hyperguest.com/hg-apitude/hotel-content-api/1.0/';
$host = 'https://api.hyperguest.com';

$payload = json_encode($my_user);


/* Ingresamos la url de la api o servicio a consumir */
$curl = curl_init($url0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
/* Autorizamos enviar datos */
curl_setopt($curl, CURLOPT_POST, true);

/* convertimos los datos en el formato solicitado normalmente json */
$payload = json_encode($my_user);
curl_setopt($curl, CURLOPT_POSTFIELDS, $payload);

/* Archivo para guardar datos de sesion */
curl_setopt($curl, CURLOPT_COOKIEJAR,  __DIR__ . '/cookies.txt');
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));

/* Ejecutamos petición */
$result = curl_exec($curl);

$err = curl_error($curl);

curl_close($curl);


if ($err) {
  echo $err;
} else {
  echo $result;
}

The answer is the following:

{
  "error": {
    "code": "SN.404",
    "message": "Url not found"
  }
}

In advance thank you very much for the help!

Solution

[SOLVED]

After doing a lot of research, I found a program that helps you test API’s prior to their use and in its documentation, I could see that my correct connection method was the BEARER method, apart from the fact that I had to request a new endpoint to be able to have a new access token


$curl = curl_init();

curl_setopt_array($curl, array(
  CURLOPT_URL => 'https://api.hyperguest.io',
  CURLOPT_RETURNTRANSFER => true,
  CURLOPT_ENCODING => '',
  CURLOPT_MAXREDIRS => 10,
  CURLOPT_TIMEOUT => 0,
  CURLOPT_FOLLOWLOCATION => true,
  CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  CURLOPT_CUSTOMREQUEST => 'GET',
  CURLOPT_HTTPHEADER => array(
    'Authorization: Bearer xxxxxxxxxxxxxxxxxxxxxxxxx'
  ),
));

$response = curl_exec($curl);

curl_close($curl);

echo $response;

This code can be simplified a bit by doing it like this


$url = "https://api.hyperguest.io";

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

$headers = array(
   "Accept: application/json",
   "Authorization: Bearer xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

I hope someone will use this code later.

Thanks to everyone who commented.

Answered By – Gabriel Yturrino

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published