PHP Service Layer Example Part 2

This article is part 2 of PHP Service Layer Example. In the previous article, I explained how to initiate an authentication request to get a Service Layer session id and route id. In this article we use these details to execute a request to retrieve a list of business partners.

To follow along with this article, I would suggest reading my previous article PHP Service Layer Example Part 1.

The B1SESSION and ROUTEID must be sent as cookies in the request header for subsequent requests. Using the curl_setopt() function, you can set the request header as shown in Listing 1 below.

Listing 1

...

$sessionId = "8daf55f4-01bf-11e8-8000-002655851f48";
$routeId = ".node0";
$headers[] = "Cookie: B1SESSION=" . $sessionId . "; ROUTEID=" . $routeId . ";";

curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

Listing Business Partners


To get a list of Business Partners, you need to call the /b1s/v1/BusinessPartners resource URI. As this resource returns data, you must use the HTTP GET request method. Listing 2 below shows a complete code sample, that returns all business partners.

Listing 2

$host = "";
$port = 50000;

$sessionId = "3d78cc04-01c5-11e8-8000-002655851f48";
$routeId = ".node2";
$headers[] = "Cookie: B1SESSION=" . $sessionId . "; ROUTEID=" . $routeId . ";";

$curl = curl_init();
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_URL, $host . ":" . $port . "/b1s/v1/BusinessPartners");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);

$response = curl_exec($curl);

echo $response;

A successful request will return an HTTP 200 OK status with a JSON response. If the B1SESSION is invalid, an HTTP 401 Unauthorized is returned along with the following JSON response.

Listing 3

{
   "error" : {
      "code" : 301,
      "message" : {
         "lang" : "en-us",
         "value" : "Invalid session."

      }
   }
}

Selecting Properties And Applying Filters


By default, all properties relating to a business partner is returned resulting in a huge amount of data being transfered. This can have performance issues, most especially when you have a lot of business partners.

Thankfully as the Service Layer uses the oData protocol, you can query the BusinessPartners resource to return properties that are of interest to you thereby reducing the data being transfered. Listing 4 below shows how to return business partners with selected properties.

Listing 4

...
$select = '?$select=CardCode,CardName,CardType,Address,City,Country,ZipCode';
...
curl_setopt($curl, CURLOPT_URL, $host . ":" . $port . "/b1s/v1/BusinessPartners" . $select);

Additionally, you can filter the result by applying a filter option to the query. Listing 5 below shows how to return business partners where the CardCode property starts with 'A'.

Listing 5

...
$select = '?$select=CardCode,CardName,CardType,Address,City,Country,ZipCode';
$filter = '&$filter=' . rawurlencode("startswith(CardCode, 'A')");
...
curl_setopt($curl, CURLOPT_URL, $host . ":" . $port . "/b1s/v1/BusinessPartners" . $select . $filter);

It is important to remember that you must escape the query where appropriate. Failure to do so will result in a bad request. The oData protocol provides a range of operators and functions to query data. Consult the oData documentation for further details.

SAP Business One

HANA DB

Rust

Java

Node.js