Accessing the SilverWebservice using PHP

When writing the post on how to integrate Sumatra in an Essence Portal, I already suspected questions about this. And yes, The question I expected was asked rather quickly after that post went online. Some people noticed that we have a report that shows statistics on our website traffic. So, how do we gather the data for these statistics?

We have the Channels and ChannelFacts

In our own database, we have tables for Channels. Every website is classified as a separate Channel, as are our Campaigns. For each Channel, we can record what's happening in our ChannelFacts table. Using these tables, we have extracted 2 BusinessControllers in our Portal.

So far, that's just pretty basic and straight forward stuff. So, how do we fill the ChannelFacts table?

Accessing the SilverWebservice using PHP

From every webpage we have, we use the SilverWebservice to monitor webpage activity. By sending a Webservice Request to the SilverWebservice, containing the information we want. Most asp.net C# developers are familiar in doing that, and there are more than enough code samples you can find online. 

But we also have some PHP webpages, and we found that PHP programmers are less familiar with Webservices. So here is a code snippet from one of our own pages, showing how to send a Request (using cURL) to the SilverWebservice on every page load.

Do keep in mind that we are not experienced PHP programmers, but we are sure you will appreciate the example. More information about the SilverWebservice can be found in this document.

Code snippet

$url = "https://my.silveressence.net/CentralStation/Services/SilverWebService.svc/Operate";

$input_xml = '<SilverBusinessController name="ChannelFact">
<Request consumerAuthenticationToken="" consumerRefreshAuthenticationToken="" consumerUserAuthenticationToken="">
<Authentication>
<Consumer>
<Username>Silver_Consumer</Username>
<Password></Password>
<ConsumerKey>[PLACE_YOUR_CONSUMERKEY_HERE]</ConsumerKey>
<Solution>[PLACE_YOUR_SOLUTIONNAME_HERE]</Solution>
</Consumer>
<ConsumerUser><Username>[PLACE_YOUR_USERNAME_HERE]</Username></ConsumerUser>
</Authentication>
<BusinessEntity name="ChannelFact" id="">
<Create>
<Properties>
<Property propertyName="ChannelFactTypeID" propertyValue="A0F45D1C-B8D6-46E5-AF7F-C51A94D52F70" />
<Property propertyName="ChannelID" propertyValue="662c5881-adeb-43b3-9b2f-cf3da807c161" />
<Property propertyName="RawURL" propertyValue="RAW_URL" />
<Property propertyName="Referrer" propertyValue="REFERRER" />
<Property propertyName="VisitorIP" propertyValue="127.0.0.1" />
<Property propertyName="UserAgent" propertyValue="USER_AGENT" />
<Property propertyName="Description" propertyValue="RAW_URL" />
</Properties>
</Create>
</BusinessEntity>
</Request>
<Response>
<QueryString>
<Controller name="ChannelFact">
<BusinessEntities>
<BusinessEntity name="ChannelFact" id=""></BusinessEntity>
</BusinessEntities>
</Controller>
</QueryString>
</Response>
</SilverBusinessController>';

$input_xml = str_replace("127.0.0.1", $_SERVER['REMOTE_ADDR'], $input_xml);
$actual_link = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$input_xml = str_replace("RAW_URL", $actual_link, $input_xml);
$input_xml = str_replace("REFERRER", $_SERVER['HTTP_REFERER'], $input_xml);
$input_xml = str_replace("USER_AGENT", $_SERVER['HTTP_USER_AGENT'], $input_xml);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $input_xml);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 2);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 9800);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
$input_xml = curl_exec($ch);
print curl_error($ch);
curl_close($ch);

if (curl_errno($ch))
print curl_error($ch);
else
curl_close($ch);