How to use PHP with the SAP DI API

by Danny Goor

How to use PHP with the SAP DI API

PHP is as of 2019 one of the leading and preferred languages by developers due to its being easy to learn and deploy. It powers approximately 78.9 percent of all the websites whose server-side programming language we know, based on this survey.

Although .NET / C# is the preferred programming language by most SAP extension developers, SAP DI API is supported on numerous other languages including PHP.

In this article I show how to use the popular PHP language with the SAP DI API.


Connection

To use PHP, start by instantiating a new COM object. The COM class allows you to instantiate an OLE compatible COM object and call its methods and access its properties. In this case SAPbobCOM.company. If this function returns 0, the connection has successfully been created.

public function connectSAP($db) {
    $this->vCmp=new COM("SAPbobsCOM.company") or die ("No connection");
    $this->vCmp->server = "SAPSERVER";
    $this->vCmp->CompanyDB = $dbname;
    $this->vCmp->username = "sap_username";
    $this->vCmp->password = 'Password';
    $this->vCmp->DbServerType = 6;
    $this->vCmp->DbUserName = "sap_user";
    $this->vCmp->DbPassword = "sap_user_password";
    try {
	$char = $this->vCmp->Connect();
    } catch (com_exception $expt) {
	print_r($this->vCmp->GetLastError());
    }
    return $char;
}


Object Interaction

Start interacting with SAP objects by getting the business object. You can refer to this table for the integer value to pass in the GetBusinessObject method.

Afterwards, use the GetByKey method which returns a boolean, to determine if the entry exists.

Update

In the following code snippet we’re trying to deactivate an existing Item by updating the value for Frozen and Valid, then running the update method to finalize the update.

    $concode = $parse->connectSAP($dbname);
    if ($concode == 0) {
      $oitm = $parse->vCmp->GetBusinessObject(4);
      if($oitm->GetByKey($itemcode) == true) {
        $oitm->Frozen = 1;
        $oitm->Valid = 0;
        $m = $oitm->update();
        echo json_encode(array('status'=>'OK',
          'action'=>'deactivate',
          'code'=>$m));
        $oitm = null;
        $parse->closeConnection();
        return;
      } else {
        echo json_encode(array('status'=>'FAIL',
          'action'=>'deactivate',
          'message' => 'itemcode not found'));
        $oitm = null;
        $parse->closeConnection();
        return;
      }
    }

Cycling through an Array of Objects

Some objects have iterable properties. In the following BP Addresses example we access the Count properties/class variables in a for loop, and use the SetCurrentLine method to iterate to each address.

	for ($i = 0; $i < $this->oBP->Addresses->Count; $i++) {
	    $this->oBP->Addresses->SetCurrentLine($i);
        }

Add

To add to an object use the Count property to get the end of the array’s index. Use the SetCurrentLine method to position to the end and run the add method

After this you can set the properties then update the Object.

    $this->oBP->Addresses->add();
    $this->oBP->Addresses->Street = '120 Street Ave.';
    $this->oBP->Addresses->Block = 'No.34';
    $this->oBP->Addresses->City = 'Chatsworth';
    $this->oBP->Addresses->State = 'CA';
    $this->oBP->Addresses->ZipCode = '91324';
    $this->oBP->Addresses->Country = 'US';
    $this->oBP->update();

Delete

The following snippet shows how to remove/delete an Object.

        $oBom = $parse->vCmp->GetBusinessObject(66);
    	if ($oBom->GetByKey($itemcode) == true) {
    	   $oBom->remove();
           echo json_encode(array('status'=>'OK','message'=>'removed'));
    	}

Conclusion

As one can see, porting the code to PHP through the COM object is straightforward and easy. The difference in C# is it is easy to expose all objects and properties that are available to use for each object (on mouse hover in Visual Studio). If you follow the documentation and examples provided by the SAP DI API you can easily use the same methods and properties to manipulate SAP objects.

Using PHP gives you the flexibility to integrate SAP into existing PHP codebases. If your developer is more comfortable in PHP then it’s worth it trying this method.

At SAP Developers we are adept and flexible. If you have a current technology already in use, we can create extensions that make it easier to integrate. We are experienced in multiple programming languages and paradigms, and can help you out in designing, developing and deploying your SAP extension goal.

Contact us for a FREE consultation.