Use ZeroMQ and protobuf in PHP

Source: Internet
Author: User
Preface this record is divided into two parts: Build the environment. Simple tutorial. To build the environment and install the ZeroMQ library, PHP was first installed a long time ago, so I didn't want to re-compile PHP, but wanted to add this library using the dynamic loading module method. As zmermq's official website introduced, I learned how to dynamically install libraries for php.

Preface this record is divided into two parts: Build the environment. Simple tutorial. To build the environment and install the ZeroMQ library, PHP was first installed a long time ago, so I didn't want to re-compile PHP, but wanted to add this library using the dynamic loading module method. As zmermq's official website introduced, I learned how to dynamically install libraries for php.

Preface

This record is divided into two parts:

  1. Build the environment.
  2. Simple tutorial.
Build the environment and install the ZeroMQ Library

First, PHP was installed a long time ago, so I didn't want to re-compile PHP. Instead, I wanted to use the dynamic loading module method to add this library.

As zmermq's official website introduced, I learned how to dynamically install libraries for php.

However, before installing the zmq module of php, A zmw library of c ++ is required.

Therefore, install the c ++ Library first.

wget http://download.zeromq.org/zeromq-4.0.5.tar.gztar zxvf zeromq-4.0.5.tar.gz cd zeromq-4.0.5.tar.gz ./configuremakemake testmake install
Install php ZeroMQ Module

Then install the php version of the module, and the general installation only takes one more step: phpize

On github

git clone git://github.com/mkoppanen/php-zmq.gitunzip php-zmq-master.zipcd php-zmq-masterphpize./configuremake -Bmake testmake install
Build complete Problems
# ./configureconfig.status: creating config.hconfig.status: config.h is unchanged# makeBuild complete.Don't forget to run 'make test'.

The difference between the above two codes can be seen.

Since make only compiles the modified file chain, and our file is not modified, it will not be re-compiled.

Usemake -BForced re-compilation.

ZMQContext not found

The approximate error is as follows:

Fatal error: Class 'ZMQContext' not found ( but it is installed and works on the terminal )

Stackoverflow found three similar problems here, and here, stackoverflow3.

Their general conclusion is the issue of configuration files.

In addition, they prompt that the command line can be executed normally, so I try it on the command line and the result is really OK.

Php test. php # normal output

In addition, they all emphasize that php has two configuration files: Command Line and apache, but I usewhereis phpFind the place where php is available, and then check that only/etc/php.iniThis is a place.

# whereis phpphp: /usr/bin/php /etc/php.d /etc/php.ini /usr/lib64/php /usr/include/php /usr/local/php /usr/share/php /usr/share/man/man1/php.1.gz

So I guess: the biggest possibility is that apache is not used./etc/php.iniThis configuration file.

But when I thought that the command line could be used, I should use the command line first.

Let the entire client run first, so I don't care about this problem. (Finally, you need to solve this problem and find the cause. For details, see the following)

Install protobuf Library

Protobuf did not find the php version source code on google's official website, but found two on github. I chose this.

Then download and install

git clone https://github.com/allegro/php-protobuf/archive/master.zipunzip php-protobuf-master.zipcd php-protobuf-masterphpize./configuremakemake testmake install

Then the command line can run normally.

Simple and practical use of zmq in php

Because I only use php as a client to link the server in other places, only the client usage is recorded here.

Before reading the following code, you can first look at a simple ZMQ record I recorded earlier.

The language is the same, so I understand a language, and other languages can be used quickly.

// Create the environment $ context = new ZMQContext (); // create a socket $ requester = new ZMQSocket ($ context, ZMQ: SOCKET_REQ); // connect to the server. If it is a server, you just need to change it to bind and the second parameter of socket. $ Requester-> connect ("tcp: // 10.12.191.112: 5555"); // send data $ requester-> send ("hello "); // accept data $ ret = $ requester-> recv (); // process data dump ($ ret );
Use of protobuf in php

First, write your own protocol, such as a simple login protocol, and save it as a file with the extension proto, such as test. proto.

Package test; message api_req {required string username = 1; required string password = 2;} message api_rsp {required int32 ret = 1; // 0 pass verification 1 reject verification required string err_msg = 2; // error message}

Then convert our protocol using protoc-php.php

This protoc-php.php file can be found on github I mentioned above, and ProtobufCompiler will also download it.

php protoc-php.php test.proto

After execution, a php file will be output, which is our protocol class encapsulated with php.

Finally, we use our protocol class.

// Introduce our header file require_once 'pb _ proto_test.php '; // package our data and return the string $ req = new Test_ApiReq (); $ req-> setUsername ($ username); $ req-> setPassword ($ password); $ packed = $ req-> serializeToString (); // unpack the obtained string $ rsp = new Test_ApiRsp (); try {$ rsp-> parseFromString ($ packed);} catch (Exception $ ex) {die ('upss .. there is a bug in this example ');} $ ret = $ rsp-> getRet (); $ msg = $ rsp-> getErrMsg ();
Legacy problems

In the end, zeromq and protobuf in php can run normally, but only in the command line.

Then, I accidentally saw someone saying that the configuration file is incorrect. You need to specify php-config when compiling the library.

So I tried it.

cd php-zmq-master/usr/local/php/bin/phpize./configure -with-php-config=/usr/local/php/bin/php-configmake -Bmake testmake install

Then there was no prompt in the browser that it could not be found.ZMQContext.

However, a new problem occurs. The prompt is: ProtobufMessage cannot be found.

So we use the same method to compile protobuf.

cd php-protobuf-master/usr/local/php/bin/phpize./configure -with-php-config=/usr/local/php/bin/php-configmake -Bmake testmake install

Good, no prompt to find ProtobufMessage.

But the promptundefined symbol: zend_new_interned_string in Unknown on line 0.

So I began to query a lot of information.

The first one is here and here, but this module is not installed through pecl, so it is invalid.

But I still executephp -vThis parameter indicates that php is 5.3.3.

Then I am here and here, but the problem is not solved.

However, it prompts me to check the phpinfo.

Then I found that my php version is 5.6.

At this point, I realized a problem: I installed two php programs on this development machine.

Of course, I found it here at first, and here, and here, everyone said it was a bug. Now it seems that it is not a bug.

Solution

Since we are sure that there are two php versions, we need to first make the versions consistent.

So I used the brute-force method/usr/local/php/bin/Copy the file/etc/bin/Below.

Of course, back up the original file before overwriting.

ls /usr/local/php/bin/pear        peardev     pecl        phar        phar.phar   php         php-cgi     php-config  phpizecp /usr/local/php/bin/* /etc/bin/*

In this way, you do not need to specify the path and re-compile it. Restart apache, And the browser access will find that the result can be returned normally.

Summary

If these problems occur, check whether the configuration file is correct. If not, check whether the version is correct.

Conclusion

Original article address: Use ZeroMQ and protobuf in PHP. Thank you for sharing it with me.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.