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);
Related
I have download the library by using composer & add this code in php file:
require_once __DIR__.'/vendor/autoload.php';
$parser = new PhpMimeMailParser\Parser();
But it return the error in log file:
PHP Fatal error: Uncaught Error: Class 'PhpMimeMailParser\Parser' not found
mailparse extension already installed on my server
I believe the right syntax is
require_once __DIR__.'/vendor/autoload.php'; $parser = new PhpMimeMailParser();
Look at the Reference here.
After some digging i found that it has to be loaded with:
$parser = new eXorus\PhpMimeMailParser\Parser();
you might also need to install mailparse extension and enable it in php.ini
you can find updated usage in:
vendor/php-mime-mail-parser/php-mime-mail-parser/README.md
I'm trying the following setup and am getting this error:
\nFatal error: Uncaught Error: Found top-level code in /home/user/code/xhp-simple/src/example.php:7\nStack trace:\n#0 (): main()\n#1 {main}
Setup:
composer.json
{
"require": {
"hhvm/hhvm-autoload": "^3.1",
"facebook/xhp-lib": "^4.0"
}
}
src/index.hack
use type Facebook\XHP\HTML\div;
// require_once(__DIR__."/../vendor/hh_autoload.hh"); // also tried here instead of in main
<<__EntryPoint>>
function main(): void {
require_once(__DIR__."/../vendor/hh_autoload.hh");
echo <div>{1 + 2}</div>;
}
hh_autoload.json
{"roots": ["src/"]}
run command:
hhvm -m server -p 8080 -d hhvm.server.default_document=./src/example.hack
I have hhvm v 4.83.1 installed
I think that you're running into the fact that hhvm-autoload hasn't caught up with the recent restrictions to top-level code. In particular, where require_once seems to no longer be allowed at the top level. With your hh_autoload.json, hhvm-autoload generates this hh_autoload.hh:
<?hh // partial
require_once(__DIR__.'/autoload.hack');
Facebook\AutoloadMap\initialize();
Where I believe that require_once is illegal. If you place this code in your main, it should work. I tested this with no problems on HHVM 4.84.0:
// src/index.hack
use type Facebook\XHP\HTML\div;
<<__EntryPoint>>
async function main(): Awaitable<void> {
require_once(__DIR__.'/../vendor/autoload.hack');
Facebook\AutoloadMap\initialize();
echo await (<div>{1 + 2}</div>)->toStringAsync();
}
$ # run with:
$ hhvm src/index.hack
Also note that all rendering is async now with XHP-lib, so you can't just echo XHP objects directly; instead:
Calls to $xhp->toString() need to be updated to $xhp->toStringAsync().
I've just noticed the updated README on hhvm-autoload, where indeed hh_autoload.hh has been phased out and you need to need to generate the autoload map yourself (emphasis mine):
Replace any references to vendor/autoload.php with vendor/autoload.hack and call Facebook\AutoloadMap\initialize()
I want to generate private/public keys using Elliptic Curve Cryptography in PHP.
I have used this library: https://github.com/simplito/elliptic-php
my code:
<?php
use Elliptic\EC;
// Create and initialize EC context
// (better do it once and reuse it)
$ec = new EC('secp256k1');
// Generate keys
$key = $ec->genKeyPair();
$publicKey = $key->getPublic('hex');
$privateKey = $key->getPrivate('hex');
// Print the keys to the console
echo "The address1 is {$publicKey}. \r\n";
echo "The address1 is {$privateKey}. \r\n";
but it shows me this error:
PHP Fatal error: Uncaught Error: Class 'Elliptic\EC' not found in /home/istabraq/bctest/test1/keygenerator.php:6
I have installed composer Composer 1.6.3
from this tutorial:
https://linuxize.com/post/how-to-install-and-use-composer-on-ubuntu-18-04/
then installed sudo apt-get install php7.2-gmp
and installed composer require simplito/elliptic-php and finally installed composer require simplito/bn-php but the last command line shows me this output:
Using version ^1.1 for simplito/bn-php
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Writing lock file
Generating autoload files
what I have missed please help? I search for problem but did not have tutorial.
try include file with class or if exists vendor/autoload.php include it, for example:
<?php
include 'path/vendor/autoload.php'; //or 'path/file/EC.php'
use Elliptic\EC;
// Create and initialize EC context
// (better do it once and reuse it)
$ec = new EC('secp256k1');
// Generate keys
$key = $ec->genKeyPair();
$publicKey = $key->getPublic('hex');
$privateKey = $key->getPrivate('hex');
// Print the keys to the console
echo "The address1 is {$publicKey}. \r\n";
echo "The address1 is {$privateKey}. \r\n";
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 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