BSL and PHP

Connection Example with PHP and Authorization

 

In PHP it's simple to connect to Beas Service Layer

Example: get.php

 

See Video:

youtube BSL and PHP

 

<?php
 
// Connection information
$bsl_url = 'http://localhost:8080/odata4';
// Connection by User and Password. Define this in Personnel - Tab "Login"
$bsl_user = 'test01';
$bsl_password = '1234';
$auth_cookie = '';
// Autenticate
$auth = CallAPI($auth_cookie, 'POST', $bsl_url.'/Login', json_encode(["User"=>$bsl_user,"Pwd"=>$bsl_password]));
if ($auth['ret_code']!=0) die('Authentication error<br>'.print_r($auth,true));
$auth_cookie = 'beas-sessionid='.$auth['beas-sessionid'];
// Get Items (ItemCode,ItemName, which start between C and D)
$items = CallAPI($auth_cookie, 'GET', $bsl_url.'/Item?$select=ItemCode,ItemName&$filter=ItemCode%20gt%20"C"%20and%20ItemCode%20lt%20"D"&$format=json');
if ($items['ret_code']!=0) die('Error<br>'.print_r($items,true));
// Display result
echo '<table><thead><tr><th>Item Code</th><th>Item Name</th></tr>';
foreach($items['value'] as $item) echo '<tr><td>'.$item['ItemCode'].'</td><td>'.$item['ItemName'].'</td></tr>';
echo '</table>';
// Display result (no json format!!)
echo $items;
// Logout
$items = CallAPI($auth_cookie, 'POST', $bsl_url.'/Logout');
exit;
 
// ----------------------------------------------------------------------
function CallAPI($cookie, $method, $url, $data = false)
{
    $curl = curl_init();
    switch ($method)
    {
        case "POST":
            curl_setopt($curl, CURLOPT_POST, 1);
            if ($data) {
                curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));               
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
            }
            break;
        case "PUT":
            curl_setopt($curl, CURLOPT_PUT, 1);
            break;
        default:
            if ($data)
                $url = sprintf("%s?%s", $url, http_build_query($data));
    }
 
    if ($cookie!='') curl_setopt($curl, CURLOPT_HTTPHEADER, array("Cookie: ".$cookie));
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($curl);
    curl_close($curl);
    return json_decode($result, true);
}
?>