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:
http://ask.ocsinventory-ng.org/735/demande-dinformations-web-service-ocs-inventory
https://www.tnpi.net/support/forums/index.php?topic=1037.0
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:
decompress it using tar -zxf HTTP-Message-6.04.tar.gz
call the new directory cd HTTP-Message-6.04/
perl Makefile.PL
make
make test
make install
and finally, check if the module got downgraded successfully by typing cpan -D HTTP::Message(it should output ... Installed: 6.04 ...)
restart the server - service apache2 restart
Run the PHP snippet shown above to test it again.
Related
just using playing around with Google's Natural Language API with php, but I can't seem to run a simple example.
Here is the basic for my php:
<?php
# Imports the Google Cloud client library
use Google\Cloud\Language\LanguageClient;
# Your Google Cloud Platform project ID
$projectId = '<My Project Name>';
# Instantiates a client
$language = new LanguageClient([
'projectId' => $projectId
]);
# The text to analyze
$text = 'Hello, world!';
# Detects the sentiment of the text
$annotation = $language->analyzeSentiment($text);
$sentiment = $annotation->sentiment();
echo 'Text: ' . $text . 'Sentiment: ' . $sentiment['score'] . ', ' . $sentiment['magnitude'];
?>
But it comes up with this error:
Fatal error: Uncaught Error: Class 'LanguageClient' not found in /User/zan/Zan/classifier/test.php:11
Stack trace:
#0 {main}
thrown in /Users/zan/Zan/classifier/test.php on line 11
I used composer to install google/cloud, but have no clue why it's unable to find LanguageClient. Can anyone point me in the right direction?
Did you include the composer autoloader?
include __DIR__ . "/vendor/autoload.php";
This example assumes you ran composer install in the same directory your code is running. Modify the path accordingly to match your configuration.
just a quick update, so I managed to get it working in the end. Since it was a new laptop, most of my php files weren't setup correctly.
I was missing these components:
autoconf
pecl
php-unit
grpc
protobuf
pcre
Lastly, running a
composer update
within the directory and adding the header
include __DIR__ . "/vendor/autoload.php";
I then ran the script and it worked, thanks to jdp for add and Martin for the heads up.
I'm using PHP to try using the Google Cloud Spanner. I already did the gCloud settings and everything, and that's right. Now I need to make the connection via PHP to do a CRUD with the database that is in Spanner, but the code below always returns the error:
PHP Fatal error: Undefined constant 'Grpc\STATUS_UNKNOWN' in
/xxx/xxxx/www/vendor/google/cloud-spanner/Connection/Grpc.php on line
129
The code I have is:
<?php
require 'vendor/autoload.php';
use Google\Cloud\Spanner\SpannerClient;
/* Error start here */
$spanner = new SpannerClient([
'projectId' => 'my-project-id'
]);
$db = $spanner->connect('instance', 'database');
$userQuery = $db->execute('SELECT * FROM usuario WHERE login = #login', [
'parameters' => [
'login' => 'devteam'
]
]);
$user = $userQuery->rows()->current();
echo 'Hello ' . $user['login'];
The requirements I use in the composer are:
"require": {
"google/cloud": "^0.32.1",
"google/cloud-spanner": "^0.2.2"
}
I noticed that if I enter through the browser, the error presented above continues to appear. If I run the command php teste.php on the terminal, it runs the script correctly, ie, the terminal works and the browser does not.
Google Cloud PHP's spanner client is gRPC only. This means to use it you will need to install the gRPC PHP extension:
pecl install grpc
Once you have done that, add google/proto-client-php and google/gax to your composer.json and run composer update. After this is done, the error will be resolved.
For those wanting more detailed instructions, see this page for installing and enabling gRPC for PHP!
Since you mentioned that it works on CLI but not on browser, I can say that you need to enable the grpc extension on your php web server config.
E.g. Add
extension=grpc.so to your /etc/php/5.6/apache2/php.ini
I've got an issue running my ownCloud instance on a Synology NAS. I get to following log error:
PHP Startup: No such handler: DBA_DEFAULT at Unknown#0
... which should be about the connection to the database.
It is very confusing, because the database can be called by ownCloud, all connected clients work, the web interface works and so on... Only this message remains.
My configuration details:
Synology DS216j with DSM 6.0.2-8451 Update 7
PHP 5.6.28
MariaDB 5.5.53
ownCloud 9.1.3
I can find absolutely no information about that error...
I've been seeing this message on my Synology NAS recently and this is what I've found. My NAS has multiple Php installations, and I think this error is coming from the default installation.
root#synology:~# /bin/php --ri dba
Extension 'dba' not present.
but using my alternate php
root#synology:~# /usr/local/bin/php56 --ri dba
dba
DBA support => enabled
Supported handlers => gdbm cdb cdb_make db4 inifile flatfile
Directive => Local Value => Master Value
dba.default_handler => flatfile => flatfile
So one solution is to ensure that your application is invoking the correct Php version. For this you can manage the HTTP server and PHP version from the Synology Web Station.
Alternately I noticed that this message is E_WARNING level only and can be ignored (unless you need Berkley Database). I added this code to my application
function shutdownHandler()
{
try {
$error = error_get_last();
if ( $error !== NULL && $error["type"] == E_ERROR) {
$backtrace = "(E_ERROR) " . $error["message"];
$trace = debug_backtrace();
foreach($trace as $item) {
$backtrace .= "\n\t" . (isset($item['file']) ? $item['file'] : '<unknown file>')
. ' ' . (isset($item['line']) ? $item['line'] : '<unknown line>')
. ' calling ' . (isset($item['function']) ? $item['function'] : '<unknown function>') . '()';
}
\Logger::LogError( $backtrace, $error["file"], $error["line"]);
}
}
catch (Exception $e) {
print get_class($e)." thrown within the shutdown handler. Message: ".$e->getMessage()." on line ".$e->getLine();
}
}
# Registering shutdown function
register_shutdown_function('shutdownHandler');
I'm guessing ownCloud has a similar function, but it is probably logging all error messages, which is why you are seeing this message.
This happend on my DS114 with DSM 6.0.2-8451 Update 7 also, after the upgrade of Apache 2.2 on 6 January 2017. Before the upgrade all worked fine.
I have changed /volume1/#appstore/Apache2.2/usr/local/etc/apache22/conf/extra/mod_xsendfile.conf
from
XSendFilePath /var/services/web /var/services/homes
to
XSendFilePath /volume1/owncloud
and now it works again
I'm not very experienced with using LDAP and have been looking at a few stack overflow questions and tried to piece some code together.
I'm using the PEAR LDAP2 package with my php. So far I have set up my filters, but im not searching for anything yet.
All I am trying to do is set up my connection to the server but when my code reaches:
$ldap= Net_LDAP2::connect($config);
The script freezes and produces a white screen. How can i fix this?
Script below:
<?php
include '../config/connection.php';
require_once '../Scripts/Net_LDAP2-2.0.12/Net/LDAP2/LDAP2.php';
//retrieve information from the form
$username = $_POST['username'];
$password = $_POST['password'];
$usernamefilter = Net_LDAP2_Filter::create('username', 'equals', $username);
$passwordfilter = Net_LDAP2_Filter::create('password', 'equals', $password);
$combinedFilter = Net_LDAP2_Filter::combine('and', array($usernamefilter, $passwordfilter));
echo "filters have been created. <br />";
// The configuration array:
$config = array (
'binddn' => 'username',
'bindpw' => 'password',
'basedn' => 'ou=People,dc=campus,dc=aston,dc=ac,dc=uk',
'host' => 'gc.campus.aston.ac.uk',
'filter' => $combinedFilter
);
echo "config array has been set up. <br />";
// Connecting using the configuration:
$ldap = Net_LDAP2::connect($config);
echo "connection to ldap has been sent. <br />";
// Testing for connection error
if (PEAR::isError($ldap)) {
die('Could not connect to LDAP-server: '.$ldap->getMessage());
}
Consider verifying that the LDAP directory server is correctly responding to LDAP client requests by using a known good client. ldapsearch as distributed by the vendor is the best choice for this task. Execute the following search to verify that the server is accepting connecting client connections and responding:
ldapsearch -h gc.campus.aston.ac.uk -p <your-port> \
-D the-distinguished-name -w password \
-b 'ou=People,dc=campus,dc=aston,dc=ac,dc=uk' \
-s base '(&)' 1.1
If this hangs, fails, or returns no entries, correct the problem and re-test until the above command succeeds.
Note: if you are using the old legacy OpenLDAP ldapsearch, add the -x command line option
I'm a complete PHP/wamp noob but am just trying to get a simple bit of code to work. I'm using a library to help me out with OAuth for Twitter, and have been writing and testing my code with wamp on Windows.
This is my code:
<html>
<body>
<?php
include 'tmhOAuth.php';
include 'tmhUtilities.php';
$tweet_text = 'Test Tweet. If you can see this, my PHP code is working! Yay';
echo "Posting...\n";
$result = post_tweet($tweet_text);
echo "Response code: " . $result . "\n";
function post_tweet($tweet_text) {
$connection = new tmhOAuth(array(
'consumer_key' => 'xxxxxxxxxx',
'consumer_secret' => 'xxxxxxxxxxxx',
'user_token' => 'xxxxxxxxxxxxxxxxx',
'user_secret' => 'xxxxxxxxxxxxxx',
));
$connection->request('POST',
$connection->url('1/statuses/update'),
array('status' => $tweet_text));
return $connection->response['code'];
}
?>
</body>
</html>
And the error message I get:
Fatal error: Call to undefined function curl_init() in C:\wamp\www\PHP\tmhOAuth.php on line 581
tmhOAuth is one of the library files I'm using.
After a quick Google, I followed this tutorial:
http://www.phpmind.com/blog/2011/02/how-to-enable-curl-in-wamp/
I found php.ini in both the apache and php folders, and uncommented the lines about curl. I also replaced php_curl.dll in the ext folder with a different version of the file as instructed by another tutorial I found.
Please help
Did you check which php.ini is loaded with your PHP? Make php file and put phpinfo() in it, run it and check for "Loaded configuration file"
Did you restart webserver? Because php.ini is reloaded on webserver restart in Your case.
Is this class using https protocol? Then You need to uncomment openssl extension also.
A good you to test your PHP.INI configuration in windows, is to run the PHP-CLI or check the log.
You can open the cmd and run the command php to see what's going on with your conf.