PHP App To Call c4c API
PHP App To Call c4c API
How-To Guide
Document Version: 1.0 2014.12.05
Content
1 Introduction ..................................................................................................................................................... 3
1.1 Business Scenario ........................................................................................................................................ 3
1.2 Prerequisites ................................................................................................................................................. 3
2 Download WSDL files from C4C ................................................................................................................... 4
2.1 Edit the WSDL files....................................................................................................................................... 5
3 PHP consumption application structure ...................................................................................................... 6
4 Create application index file .......................................................................................................................... 7
5 Extend PHP class SoapClient ..................................................................................................................... 10
6 Query C4C from PHP using Account ID ..................................................................................................... 11
6.1 Execute query in C4C system .................................................................................................................... 11
6.2 Display query results .................................................................................................................................. 12
6.3 Query an existing account from C4C ......................................................................................................... 14
7 Create an Account ........................................................................................................................................ 15
7.1 Create Account in the C4C system ............................................................................................................ 15
7.2 Display account creation return .................................................................................................................. 17
7.3 Create an account in C4C using web service ............................................................................................ 18
How to consume an account web service via PHP 3
1 Introduction
1.1 Business Scenario
Customers or Partners who use SAP Cloud for Customer solution tend to already have another
running system, which needs to export its data to SAP Cloud for Customer system or even
display the data stored inside SAP’s solution. This is the reason why a web service integration
between SAP Cloud for Customer and other systems is important.
1.2 Prerequisites
SAP Cloud for Customer
Initial setup and configuration was already performed in tenant as it is mentioned in the SAP Cloud for
Customer Administrator Guide. A business user with Customer work center assigned is required to execute
the web service’s requests afterwards.
The css folder will have the bootstrap library, and the js folder will have jQuery library, validation files and a
file containing country and state info, which will be used later by the app. As this is not the focus of the
guide, there will just this overview about it.
The wsdl folder is where are located the WSDL files which were downloaded and edited in the previous
step.
How to consume an account web service via PHP 7
The workaround for this is defining a login details array, where the proxy host and proxy port are defined,
when necessary. Additionally, a new parameter must be added to the location of the system: the sap-
language has to be defined as “EN”.
<?php
global $loginDetails;
// variable with proxy information, which is completed when necessary
$loginDetails = array(
'proxy_host' => '',
'proxy_port' => '',
'verify_peer' => true,
'allow_self_signed' => true,
'connection_timeout' => 300,
'exceptions' => true,
'login' => '',
'password' => ''
);
$this->defaultLocation = $location;
use_soap_error_handler(true);
parent::__construct($wsdl, array_merge($loginDetails, $options));
}
$location = $this->defaultLocation;
if (empty($soapResponse))
{
throw new Exception($this->__soap_fault->faultcode . ": Cannot connect to
host: $location. Reason:" . $this->__soap_fault->getMessage());
}
return $soapResponse;
}
}
?>
This class is going to be instantiated afterwards, in the web service consumption on both queries and
creations.
How to consume an account web service via PHP 11
First, it generates the XML that is going to be used later on the web service request. This XML is a
predefined string, with all the parameters required by C4C system to query an account, concatenated with
the account ID received via parameter from the form.
After that, the XML string is converted into a SOAP variable that will be passed as a parameter later on the
query.
The final step is execute the query. The SOAP client created refers to the extended class (created in step
3.2) which receives the WSDL location, the web service location and the authentication data as a
parameter. The authentication data may or may not have proxy information, depending on the user’s
connection.
<?php
require_once 'ByDSoapClient.php';
class QueryAccounts
{
$wsdl = 'wsdl/QueryAccounts.wsdl';
$location =
'https://<YOUR_SYSTEM_TENANT_HERE>/sap/bc/srt/scs/sap/querycustomerin1?sap-
vhost=<YOUR_SYSTEM_TENANT_HERE>';
$soapClient = new ByDSoapClient($wsdl, $location, $auth);
try {
$result = $soapClient->FindByElements(
new SoapParam($soapVar, 'CustomerSelectionByElements')
);
return $result;
} catch(Exception $e) {
How to consume an account web service via PHP 12
$message = $e->getMessage();
}
}
As seen, the XML does not need the <soapenv:Envelope>, <soapenv:Header> and <soapenv:Body> tags,
since they are provided automatically when the XML is converted to a SOAP variable.
In addition, the InclusionExclusionCode is hardcoded to I, which means the selection must return an
account with an ID equal to the ID given. The ProcessingConditions determine that the query can return an
unlimited number of registers, but our application will only be able to handle the first one. Additionally,
since it searches by ID, there must not be duplicated registers on the system.
The generateSoapVar function has the only purpose of converting the string into an SOAP parameter,
passing the required parameters to the standard PHP class SoapVar.
The function executeQuery is the one that runs the web service. The authentication has a validation for the
proxy, and the SOAP client is created using the extended class created on step 3.2. The SOAP client created
has all the web service’s operations, and the one used by the application’s query is “FindByElements”. This
operation requires a SOAP parameter, which has the name “CustomerSelectionByElements”.
All the errors that might happen (such as no account found for that ID or authentication error) are treated
in the PHP file that displays the return of the query.
After receiving the parameters via POST, an instance of QueryAccounts class is created and the query is
executed passing the required parameters. After that, the return from the query is treated as follows:
If the result is not set, there was an authentication error with the query preventing it from
being executed and an error is shown.
The response from C4C has a field where the number of accounts found by the query is
returned. It is located in ProcessingConditions > ReturnedQueryHitsNumberValue. If this
number is equal to zero, then an error message is shown.
If none of this errors occur, the results must be displayed.
<?php
require_once 'QueryAccounts.php';
$accountId = $_POST['accountId'];
$username = $_POST['username'];
$password = $_POST['password'];
$proxy_enabled = (isset($_POST['proxy']) ? true : false);
function displayQueryResults($info) {
$account_id = (isset($info->Customer->InternalID) ? $info->Customer->InternalID : " ");
$organization = (isset($info->Customer->Organisation->FirstLineName) ? $info->Customer-
>Organisation->FirstLineName : " ");
$address = (isset($info->Customer->AddressInformation->Address->FormattedAddress-
>FormattedPostalAddress) ? $info->Customer->AddressInformation->Address->FormattedAddress-
>FormattedPostalAddress : " ");
$address1 = (isset($address->FirstLineDescription) ? $address->FirstLineDescription : " ");
$address2 = (isset($address->SecondLineDescription) ? $address->SecondLineDescription : "
");
$address3 = (isset($address->ThirdLineDescription) ? $address->ThirdLineDescription : " ");
$phone = (isset($info->Customer->AddressInformation->Address->Telephone-
>FormattedNumberDescription) ? $info->Customer->AddressInformation->Address->Telephone-
>FormattedNumberDescription : " ");
$email = (isset($info->Customer->AddressInformation->Address->EmailURI->_) ? $info-
>Customer->AddressInformation->Address->EmailURI->_ : " ");
$contact_name = (isset($info->Customer->ContactPerson->GivenName) ? $info->Customer-
>ContactPerson->GivenName : " ");
$contact_name = (isset($info->Customer->ContactPerson->FamilyName) ? $contact_name . " " .
$info->Customer->ContactPerson->FamilyName : $contact_name);
$contact_email = (isset($info->Customer->ContactPerson->WorkplaceEmailURI->_) ? $info-
>Customer->ContactPerson->WorkplaceEmailURI->_ : " ");
function displayErrorNotFound($id) {
echo 'The Account ID ' . $id . ' was not found on the Cloud for Customer System. Try again
with another Account ID.';
}
function displayErrorAuthentication() {
echo 'Authentication error! Check username, password and proxy settings then try again.';
}
?>
How to consume an account web service via PHP 14
As seen, the only work done by this PHP file is receive the parameters from form, pass them to
QueryAccounts class, check the response and display the results by just accessing its relative path in the
response itself.
After executing the query by ID number 10009 on the PHP application, besides inserting correct C4C login
information, the result has to bring exactly the same data show in C4C:
How to consume an account web service via PHP 15
7 Create an Account
This section describes how to create an account in C4C from the PHP application. The create section of
the application consists in one PHP file responsible for displaying a message whether the account was
successfully created showing its ID and another PHP file responsible for creating the account on the C4C
system.
Actually the steps performed by the CreateAccount class are identical to the ones performed by the
QueryAccounts class (created in step 6.1), but of course performing a different action. The difference
between the two classes is the WSDL used, the web service location and the XML passed as parameter.
First the XML is generated based on the parameters received, returning a string which is then converted to
a SOAP variable.
The final step is create the account. A SOAP client is created using the extended class (created in step 5)
which receives the WSDL location, the web service location and the authentication data as a parameter.
The authentication data may or may not have proxy information, depending on the user’s connection.
<?php
require_once 'ByDSoapClient.php';
class CreateAccount
{
public function execute($username, $password, $accountName, $email, $countryCode,
$countryName, $stateCode, $stateName, $postalCode, $street, $number, $phoneNumber, $city,
$givenName, $familyName, $contactEmail, $proxy_enabled) {
$soapVar = $this->generateSoapVar($xml);
return $result;
}
$wsdl = 'wsdl/ManageAccounts.wsdl';
$location =
'https://<YOUR_SYSTEM_TENANT_HERE>/sap/bc/srt/scs/sap/managecustomerin1?sap-
vhost=<YOUR_SYSTEM_TENANT_HERE>';
$soapClient = new ByDSoapClient($wsdl, $location, $auth);
try {
$result = $soapClient->MaintainBundle_V1(
How to consume an account web service via PHP 16
?>
Just like in the QueryAccounts class, the XML file does not contain <soapenv:Envelope>, <soapenv:Header>
and <soapenv:Body> sections. In Customer, AddressInformation and ContactPerson, the action code is set
as 01, which means that it is a creation and not an update. The CategoryCode is hardcoded to “2” meaning
that it is an account being created, and not an individual customer. In addition, the CustomerIndicator is
hardcoded as “true”, meaning that this is a customer account.
The generateSoapVar function is the same as in QueryAccounts class, with the only purpose of converting
the string into an SOAP parameter, passing the required parameters to the standard PHP class SoapVar.
The function create is responsible for running the web service and creating the account. The authentication
follows the same process of validation for the proxy, and the SOAP client is created using the extended
class created on step 3.2. The SOAP client created has all the web service’s operations, and the one used
How to consume an account web service via PHP 17
for the creation is called “MaintainBundle_V1”. This function requires a SOAP parameter named
“CustomerBundleMaintainRequestMessage”.
All the errors that might happen (such as no account found for that ID or authentication error) are treated
in the PHP file that runs this class and displays the ID of the created account.
<?php
require_once 'CreateAccount.php';
$username = $_POST['username2'];
$password = $_POST['password2'];
$accountName = $_POST['accountName'];
$email = $_POST['accountEmail'];
$countryCode = $_POST['accountCountryCode'];
$countryName = $_POST['accountCountryName'];
$stateCode = $_POST['accountStateCode'];
$stateName = $_POST['accountStateName'];
$postalCode = $_POST['accountPostalCode'];
$street = $_POST['accountStreet'];
$number = $_POST['accountNumber'];
$phoneNumber = $_POST['accountPhone'];
$city = $_POST['accountCity'];
$givenName = $_POST['contactGivenName'];
$familyName = $_POST['contactFamilyName'];
$contactEmail = (isset($_POST['contactEmail']) ? $_POST['contactEmail'] : " ");
$proxy_enabled = (isset($_POST['proxy2']) ? true : false);
if ($result != null) {
displaySuccess($result);
}
else {
displayError();
}
function displaySuccess($info) {
echo '<h3>Account ' . $info->Customer->InternalID . ' Created</h3>';
}
function displayError() {
echo 'Authentication error! Check username, password and proxy settings then try again.';
}
?>
How to consume an account web service via PHP 18
As seen, this PHP file only receives the parameters and submits them to CreateAccount class, which creates
the account and returns the Account ID or an authentication error.
After clicking in create, the web service is executed and the account is created on the system:
How to consume an account web service via PHP 19
Searching the system for the account ID, the same data inserted in the PHP application must appear on the
account:
How to consume an account web service via PHP 20