How to enable the OCS Inventory interface described in OCS WebServices? Is there a sample code for using this Web Service in PHP?
The OCS interface is disabled by default, it is necessary to turn it on before use it. OCS has a core code developed in Perl and it runs over Apache HTTP.
First, edit the file /etc/apache2/conf-enabled/z-ocsinventory-server.conf
changing the option value for OCS_OPT_WEB_SERVICE_ENABLED
to 1.
If the web service is not enabled you should get a 401 Forbidden response. This is a SOAP WebService and there's no WSDL to describe the features, just the docs available in OCS WS Documentation.
Check if the location tag for /ocsinterface looks like the following snippet:
<Location /ocsinterface>
SetHandler perl-script
PerlHandler Apache::Ocsinventory::SOAP
# By default, you can query web service from everywhere with a valid user
Order deny,allow
Allow from all
AuthType Basic
AuthName "OCS Inventory SOAP Area"
# Use htpasswd to create/update soap-user (or another granted user)
AuthUserFile "/etc/apache2/passwd/soapinterface"
Require valid-user
</Location>
You should create a password for this location for security purposes, however, to turn the authentication off just comment out all Auth... and Require attributes.
Restart apache server, and use the PHP code below to test the web service integration
<?php
$proto = 'http';
$host = 'localhost';
$port = '80';
$user = ''; //basic authentication, if necessary
$pass = '';
$options = array(
'location' => "$proto://$host:$port/ocsinterface",
'uri' => "$proto://$host:$port/Apache/Ocsinventory/Interface",
'login' => $user,
'password' => $pass,
'trace' => TRUE,
'soap_version' => SOAP_1_1,
);
$request = '
<REQUEST>
<ENGINE>FIRST</ENGINE>
<ASKING_FOR>META</ASKING_FOR>
<CHECKSUM>131071</CHECKSUM>
<OFFSET>0</OFFSET>
<WANTED>131071</WANTED>
</REQUEST>';
try {
$client = new SoapClient(NULL, $options);
} catch (Exception $e) {
echo "<b>Construct Error</b>: " . $e->getMessage() . "<br>";
}
try {
$result = $client->get_computers_V1($request);
echo "<b>Headers:</b><pre>" . $client->__getLastRequestHeaders() . " </pre><br>";
echo "<b>Request:</b><pre>" . $client->__getLastRequest() . "</pre><br>";
echo "<b>Result:</b><pre>";
var_dump($result);
echo "</pre><br>";
} catch (Exception $e) {
echo "<b>Connection Error</b>: " . $e->getMessage() . "<br><br>";
echo "<b>Headers:</b><pre>\r\n" . $client->__getLastRequestHeaders() . " </pre><br>";
echo "<b>Request:</b><pre>\r\n" . $client->__getLastRequest() . "</pre>";
}
If you get a HTTP 500 Internal Server Error, check apache error log (tail -f /var/log/apache2/error.log -n 100
) for the following error message:
Illegal field name 'APR::Table=HASH(0x7ff114bd75a8)' at /usr/local/share/perl/5.18.2/SOAP/Transport/HTTP2.pm line 103.\n
That error happens due to an incompatibility issue found in HTTP::Message perl module. The following links describes the problem and solution related to it:
To fix it, you need to downgrade the HTTP::Message perl module to version 6.04. Use the command cpan -D HTTP::Message
in your console to check which version you're using. This module version is a little bit old, so you won't find it in Search CPAN. In this regard, you should download the module HTTP-Message-6.04.tar.gz and install it manually by typing the following commands on your terminal:
tar -zxf HTTP-Message-6.04.tar.gz
cd HTTP-Message-6.04/
perl Makefile.PL
make
make test
make install
cpan -D HTTP::Message
(it should output ... Installed: 6.04 ...)service apache2 restart
Run the PHP snippet shown above to test it again.