Fatal error: Uncaught Error: Class 'LanguageClient' not found - php

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.

Related

Having real problems with Composer

I have a dedicated server with WHM and cPanel. It already has composer installed on it.
I'm trying to run the following php lines in a webpage:
require __DIR__ . '/../vendor/autoload.php';
// Configure API key authorization: JWT
$config = \Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Authorization', '[my api key]');
$apiInstance = new \Swagger\Client\Api\MessagesApi(
new \GuzzleHttp\Client(),
$config
);
I've created a composer.json in the public_html folder and put the following in it:
{
"autoload": {
"psr-4": { "Swagger\\Client\\" : "lib/" }
}
}
And then I ran composer update in the terminal which seemed to install the dependencies and all the relevant files.
It's seeing the autoload.php file but I'm still getting a class not found error:
Fatal error: Uncaught Error: Class 'Swagger\Client\Configuration' not found in /home/mywebsite/public_html/converter/sms.php:9 Stack trace: #0 {main} thrown in /home/mywebsite/public_html/converter/sms.php on line 9
I've been at this for 4 hours now. What am I doing wrong? I can't find anything online that will guide me in the right direction.
In Composer Basic Usage documentation about autoloading it says
For libraries that specify autoload information, Composer generates a vendor/autoload.php file. You can include this file and start using the classes that those libraries provide without any extra work
So, as #NicoHaase said in the comments, if you installed Swagger with Composer (adding a require key and running composer update, for example) you don't need to specify the autoload path to Swagger in composer.json.

PHP protobuf error - Undefined method for encode/decode

I'm trying to learn about Protobuf in PHP using https://github.com/google/protobuf/tree/master/php . Currently I'm stuck in an error.
My steps to install protobuf:
Install protobuf through pecl with command:
sudo pecl install protobuf-3.2.0a1
Set composer.json as below, then run sudo composer install
{
"require": {
"google/protobuf": "^3.2"
}
}
Below is my code:
Proto file:
syntax = "proto3";
message APIReq {
string functionName = 1;
string name = 2;
int32 time = 3;
string type = 4;
}
Command to generate PHP Class from .proto file:
protoc --php_out=/var/www/html/ MsgFormat.proto
The protoc command resulted in two file, APIReq.php and GPBMetadata/MsgFormat.php
After that, I added require_once __DIR__ . '/vendor/autoload.php'; and require_once __DIR__ . '/GPBMetadata/MsgFormat.php'; in the generated PHP file because when I ran php APIReq.php it came up with
PHP Fatal error: Class 'Google\Protobuf\Internal\Message' not found in /var/www/html/testing/APIReq.php on line 13
After I added those line, the error disappeared, so I assume both line fixed the problem
my PHP file (following example from https://developers.google.com/protocol-buffers/docs/reference/php-generated, section Messages):
<?php
require __DIR__ . '/vendor/autoload.php';
include_once('APIReq.php');
$param = new APIReq();
$param2 = new APIReq();
$param->setFunctionname('functionname');
$param->setName('name');
$param->setTime(123456);
$param->setType('type');
$dt = $param->encode();
$param2->decode($dt);
?>
When I run the PHP code, it returns error message:
PHP Fatal error: Call to undefined method APIReq::encode()
How can I fix this?
Edit: Tried this with protobuf 3.3.0 as well, with same result.
Encode & Decode not exist in the codebase as I traced down.
This change was introduced in 3.3.0
//to encode message
$data = $param->serializeToString();
//to decode message
$param2 = new APIReq();
$param2->mergeFromString($data);

Push Notification: Fatal error: Class 'EccFactory' not found in pushnotification/vapidkeys.php on line 124

I am trying to implement push notification.
I downloaded web-push library from: https://github.com/web-push-libs/web-push-php
The first thing I wanted was VAPID keys .
filename is vapidkeys.php, this file is inside pushnotification directory, and in pushnotification directory I have web-push-php-master directory.
<?php
require('web-push-php-master');
use Minishlink\WebPush\WebPush;
var_dump(VAPID::createVapidKeys());
?>
But the above line throws following error:
Fatal error: Class 'EccFactory' not found in pushnotification/vapidkeys.php on line 124
Please help me solve this issue, I am new to push notification and namespaces
You have to get web-push-php with Composer, so that all the dependencies are installed.
Install Composer
Run composer require minishlink/web-push. This will install web-push-php and all its dependencies in the vendor folder.
In your PHP file, require it : require __DIR__ . '/vendor/autoload.php';
Here's a basic example that uses web-push-php: https://github.com/Minishlink/web-push-php-example
Hope this helps.
HTTPD Website Only Generate Key,Your website is HTTP can't generate Key

Elastic search configuration issue with client call php

I am using elasticsearch API php to build search results. I have configured everything in my xampp server. All the libraries downloaded from composer.json. In my composer.json file contains below code
{
"require": {
"elasticsearch/elasticsearch": "~2.0"
}
}
Libraries are successfully downloaded. After that i initialize the elastic search with below code
<?php
require 'vendor/autoload.php';
$client = ClientBuilder::create()->build();
It shows fatal error like as follows
Fatal error: Class 'ClientBuilder' not found in E:\Xampp\htdocs\codeporn\elasticsearch\app\init.php on line 4
So i change the config code as,
require_once 'vendor/autoload.php';
$es = new Elasticsearch\Client([
'hosts' => ['127.0.0.1:9200']]
]);
This also shows error like
Parse error: syntax error, unexpected ']' in E:\Xampp\htdocs\codeporn\elasticsearch\app\init.php on line 10
I am following the below youtube tutorial to build the search
https://www.youtube.com/watch?v=3xb1dHLg-Lk
Please suggest what i went wrong in Elasticsearch - PHP.
My PHP Version is 5.5.9
i have initialize the clientbuilder class, now it works fine
require 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$client = ClientBuilder::create()->build();
you need install it with composer
composer require elasticsearch/elasticsearch

Fatal error: Class 'OpenCloud\Rackspace' not found

For a custom application, I am trying to integrate Rackspace cloud files using php-opencloud library.
This is the link I followed for setup -https://github.com/srijanaravali/php-opencloud/blob/master/docs/getting-started.md
# Install Composer
curl -sS https://getcomposer.org/installer | php
# Require php-opencloud as a dependency
php composer.phar require rackspace/php-opencloud:dev-master
However, when I try to instantiate a client object, it throws an error:
Fatal error: Class 'OpenCloud\Rackspace' not found in /var/www/example/Project/sites/all/libraries/php-opencloud/test.php on line 7
Here is the code snippet:
<?php
require 'vendor/autoload.php';
use OpenCloud\Rackspace;
// 1. Instantiate a Rackspace client.
$client = new Rackspace(Rackspace::US_IDENTITY_ENDPOINT, array(
'username' => getenv('Axxxxxxx'),
'apiKey' => getenv('abcxxxxxxxxxxxxxxxxxxxx')
));
print_r($client); die('!!');
Any pointers about whats missing?
Got it working. For some strange reason, php-opencloud library was empty under vendors/rackspace/php-opencloud.
Cloned one from github and created a symlink to it from above directory. It is working fine now.

Categories