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.
-
php-sapb1-v2 Library Documentation v2
This library has been updated and includes bug fixes from V1.
07 November 2024 - 0 views -
Stock Transfer Part 2
In the previous article I exaplined how to transfer stock between warehouses and bin enabled warehouses. In this article, I discuss how to transfer batch enabled items.
07 August 2020 - 4310 views -
Stock Transfer Part 1
The SAP B1 Service Layer provides an API to move stock between warehouses. This article explains the construction of the JSON payload data that is posted to the StockTransfers API.
12 December 2019 - 6812 views -
php-sapb1 Library Documentation v1
A simple and easy to use PHP library for SAP Business One Service Layer API.
16 August 2019 - 8811 views -
node-sapb1 Library Documentation v1
This article provides documentation on the SAPb1 NodeJs library.
01 August 2019 - 8539 views -
PHP Service Layer Example Part 1
This article explains how to get connected to the Service Layer using PHP.
09 January 2018 - 14450 views -
Introduction To SAP Business One Service Layer
An introduction to the SAP Business One Service Layer.
08 December 2017 - 10717 views