Be sure to have a cacert file defined in your PHP configuration:
[...]
; A default value for the CURLOPT_CAINFO option. This is required to be an
; absolute path.
curl.cainfo = "C:\Program Files (x86)\EasyPHP-Devserver-17\eds-www\cacert.pem"
[...]
Then you can try to use the API with this kind of code:
<?php
/**
* NOTICE:
* THIS FILE IS PROVIDED "AS IS", THE PURPOSE IS TO SHOW HOW TO USE THE EBIHR API, WITHOUT WARRANTY OF ANY KIND.
* IT IS ONLY AN EXAMPLE OF HOW THE API CALLS SHOULD LOOK LIKE.
* ANY SUGGESTION TO IMPROVE THIS CODE WILL BE WELCOMED.
* LAST MODIFICATION: April 25th 2022
*/
// Your credentials
$username = 'UserName';
$password = 'PassWord';
// The API you want to call
$server = "https://ebihr-qual.bihr.net/api"; // <- https://ebihr-qual.bihr.net/api for test ; https://api.bihr.net/api for prod
$version = "v2"; // <- v2 or v2.1
$apiCall = "Inventory/fullcsv"; // <- See https://ebihr-qual.bihr.net/api-docs/index.html or https://api.bihr.net/api-docs/index.html
// $version = "v2.1"; // <- v2 or v2.1
// $apiCall = "Catalog/EssentialHardPart"; // <- See https://ebihr-qual.bihr.net/api-docs/index.html or https://api.bihr.net/api-docs/index.html
// $apiCall = "Catalog/LZMA/JSON/Prices/Full"; // <- See https://ebihr-qual.bihr.net/api-docs/index.html or https://api.bihr.net/api-docs/index.html
// The path where to save the file
$directorypath = './';
//execute
echo "<table><thead><tr><th>Time</th><th>Function</th><th>Variable</th><th>Value</th></tr></thead><tbody>";
$result = download_catalog($server, $version, $apiCall, $username, $password, $directorypath);
echo "</tbody></table>";
echo "<strong>Result:</strong> ".$result;
function download_catalog($server, $version, $apiCall, $username, $password, $directorypath) {
set_time_limit(300); // 5 minutes maximum for script execution
// Call the API to get an access token
$myToken = get_access_token($server."/".$version."/Authentication/token", "UserName=".$username."&PassWord=".$password);
if ($myToken == "KO"){
return "No access token gotten";
}
echo "<tr><td>".date(DATE_ISO8601)."</td><td>download_catalog</td><td>myToken</td><td>{$myToken}</td></tr>";
$authorization = 'Authorization: Bearer '.$myToken;
// Call the API to get a ticket ID
if($version == "v2"){
$ticketId = get_ticket_id($server."/".$version."/".$apiCall, $authorization, $version);
} else if($version == "v2.1"){
$ticketId = post_ticket_id($server."/".$version."/".$apiCall, $authorization, $version, $directorypath);
} else {
return 'version not implemented yet';
}
if($ticketId == "OK"){
return "File downloaded successfully";
}
if($ticketId == "KO"){
return "No ticket ID gotten";
}
echo "<tr><td>".date(DATE_ISO8601)."</td><td>download_catalog</td><td>ticketId</td><td>{$ticketId}</td></tr>";
// Call the API to get a download ID
while (true) {
if($version == "v2"){
$downloadId = get_download_id($server."/".$version."/Catalog/status?ticketId=".$ticketId, $authorization);
} else if($version == "v2.1"){
$downloadId = get_download_id($server."/".$version."/Catalog/GenerationStatus?ticketId=".$ticketId, $authorization);
} else {
return 'version not implemented yet';
}
echo "<tr><td>".date(DATE_ISO8601)."</td><td>download_catalog</td><td>downloadId</td><td>{$downloadId}</td></tr>";
if($downloadId == 'ERROR') {
return 'download_catalog: error on eBihr side';
}
if($downloadId != 'KO') {
// Call the API to download the file
if($version == "v2"){
$downloaded = download_catalog_file($server."/".$version."/Catalog/download?downloadId=".$downloadId, $authorization, $directorypath);
if($downloaded){
return "File downloaded successfully";
}
return 'File download failed';
}
if($version == "v2.1"){
$downloaded = download_catalog_file($server."/".$version."/Catalog/GeneratedFile?downloadId=".$downloadId, $authorization, $directorypath);
if($downloaded){
return "File downloaded successfully";
}
return 'File download failed';
}
return 'download_catalog: version not implemented yet';
}
// Wait 10 seconds ... then new call
sleep(10);
}
}
function get_access_token($url, $parameters) {
echo "<tr><td>".date(DATE_ISO8601)."</td><td>get_access_token</td><td>url</td><td>{$url}</td></tr>";
$options = array(
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $parameters,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_URL => $url
);
$ch = curl_init();
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
$infos = curl_getinfo($ch);
curl_close($ch);
if (!$infos || $infos["http_code"] >= 400){
echo "<tr><td>".date(DATE_ISO8601)."</td><td>get_access_token</td><td>content</td><td>{$content}</td></tr>";
return "KO";
}
$resArr = json_decode($content, true);
return $resArr['access_token'];
}
function get_ticket_id($url, $authorization, $version) {
echo "<tr><td>".date(DATE_ISO8601)."</td><td>get_ticket_id</td><td>url</td><td>{$url}</td></tr>";
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-Type: text/json', $authorization)
);
$ch = curl_init($url);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
$infos = curl_getinfo($ch);
curl_close($ch);
if(!$infos || $infos["http_code"] != 200) {
echo "<tr><td>".date(DATE_ISO8601)."</td><td>get_ticket_id</td><td>content</td><td>{$content}</td></tr>";
return "KO";
}
return json_decode($content, true)['TicketId'];
}
function post_ticket_id($url, $authorization, $version, $directorypath) {
echo "<tr><td>".date(DATE_ISO8601)."</td><td>post_ticket_id</td><td>url</td><td>{$url}</td></tr>";
$options = array(
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-Type: text/json', $authorization, "Content-Length: 0")
);
$headers = [];
$ch = curl_init($url);
curl_setopt_array($ch, $options);
curl_setopt($ch, CURLOPT_HEADERFUNCTION,
function($curl, $header) use (&$headers)
{
$len = strlen($header);
$header = explode(':', $header, 2);
if (count($header) < 2) // ignore invalid headers
return $len;
$headers[strtolower(trim($header[0]))][] = trim($header[1]);
return $len;
}
);
$content = curl_exec($ch);
$infos = curl_getinfo($ch);
curl_close($ch);
if(!$infos || $infos["http_code"] >= 400) {
echo "<tr><td>".date(DATE_ISO8601)."</td><td>post_ticket_id</td><td>content</td><td>{$content}</td></tr>";
return "KO";
}
if($infos["http_code"] == 202) {
return json_decode($content, true)['TicketId'];
}
if($infos["http_code"] == 200) {
$content_disposition = $headers["content-disposition"];
preg_match("/filename=.*;/", $content_disposition[0], $matches);
$fileName = substr($matches[0], 9, -1);
file_put_contents($directorypath.$fileName, $content);
echo "<tr><td>".date(DATE_ISO8601)."</td><td>post_ticket_id</td><td>filePath</td><td>{$directorypath}{$fileName}</td></tr>";
return "OK";
}
return "KO";
}
function get_download_id($url, $authorization) {
echo "<tr><td>".date(DATE_ISO8601)."</td><td>get_download_id</td><td>url</td><td>{$url}</td></tr>";
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-Type: text/json' , $authorization)
);
$ch = curl_init($url);
curl_setopt_array($ch, $options);
$content = curl_exec($ch);
$infos = curl_getinfo($ch);
curl_close($ch);
if (!$infos || $infos["http_code"] >= 400){
echo "<tr><td>".date(DATE_ISO8601)."</td><td>get_download_id</td><td>content</td><td>{$content}</td></tr>";
return "KO";
}
$resArr = json_decode($content, true);
if($resArr['RequestStatus'] == 'DONE') {
return $resArr['DownloadId'];
}
if($resArr['RequestStatus'] == 'ERROR') {
return 'ERROR';
}
return "KO";
}
function download_catalog_file($url, $authorization, $directorypath) {
echo "<tr><td>".date(DATE_ISO8601)."</td><td>download_catalog_file</td><td>url</td><td>{$url}</td></tr>";
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-Type: text/json' , $authorization)
);
$headers = [];
$ch = curl_init($url);
curl_setopt_array($ch, $options);
curl_setopt($ch, CURLOPT_HEADERFUNCTION,
function($curl, $header) use (&$headers)
{
$len = strlen($header);
$header = explode(':', $header, 2);
if (count($header) < 2) // ignore invalid headers
return $len;
$headers[strtolower(trim($header[0]))][] = trim($header[1]);
return $len;
}
);
$content = curl_exec($ch);
$infos = curl_getinfo($ch);
curl_close($ch);
if(!$infos || $infos["http_code"] != 200) {
echo "<tr><td>".date(DATE_ISO8601)."</td><td>download_catalog_file</td><td>content</td><td>{$content}</td></tr>";
return false;
}
$content_disposition = $headers["content-disposition"];
// echo "<tr><td>".date(DATE_ISO8601)."</td><td>download_catalog_file</td><td>content_disposition</td><td>".print_r($content_disposition,true)."</td></tr>";
preg_match("/filename=.*;/", $content_disposition[0], $matches);
$fileName = substr($matches[0], 9, -1);
echo "<tr><td>".date(DATE_ISO8601)."</td><td>download_catalog_file</td><td>filePath</td><td>{$directorypath}{$fileName}</td></tr>";
file_put_contents($directorypath.$fileName, $content);
return true;
}
function create_cart($server, $version, $username, $password, $payload) {
set_time_limit(300); // 5 minutes maximum for script execution
// Call the API to get an access token
$myToken = get_access_token($server."/".$version."/Authentication/token", "UserName=".$username."&PassWord=".$password);
if ($myToken == "KO"){
return "No access token gotten";
}
echo "<tr><td>".date(DATE_ISO8601)."</td><td>create_cart</td><td>myToken</td><td>{$myToken}</td></tr>";
$authorization = 'Authorization: Bearer '.$myToken;
// Call the API to post a cart
if ($version == "v2"){
$result = post_cart($server."/".$version."/Order/Create", $authorization, $payload);
} else if ($version == "v2.1"){
$result = post_cart($server."/".$version."/Order/Request", $authorization, $payload);
} else {
return 'version not implemented yet';
}
echo "<tr><td>".date(DATE_ISO8601)."</td><td>create_cart</td><td>result</td><td>{$result}</td></tr>";
if ($result == "KO"){
return "No cart created";
}
return $result;
}
function post_cart($url, $authorization, $payload){
echo "<tr><td>".date(DATE_ISO8601)."</td><td>post_cart</td><td>url</td><td>{$url}</td></tr>";
$options = array(
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $payload,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => array('Content-Type: text/json', $authorization, "Content-Length: " . strlen($payload))
);
$headers = [];
$ch = curl_init($url);
curl_setopt_array($ch, $options);
curl_setopt($ch, CURLOPT_HEADERFUNCTION,
function ($curl, $header) use (&$headers)
{
$len = strlen($header);
$header = explode(':', $header, 2);
if (count($header) < 2) // ignore invalid headers
return $len;
$headers[strtolower(trim($header[0]))][] = trim($header[1]);
return $len;
}
);
$content = curl_exec($ch);
$infos = curl_getinfo($ch);
curl_close($ch);
if (!$infos || $infos["http_code"] >= 400) {
echo "<tr><td>".date(DATE_ISO8601)."</td><td>post_cart</td><td>content</td><td>{$content}</td></tr>";
return "KO";
}
if ($infos["http_code"] == 200) {
return json_decode($content, true)['ResultCode'].": ".json_decode($content, true)['TicketId'];
}
return "KO";
}
?>