Using PHP to build a socket service based on Swoole extension (with PHP extension installation steps)

Source: Internet
Author: User

In a recent company project, a socket service needs to be built with PHP.

Originally PHP is not suitable for service, because with the third party, need to adopt efficient and stable TCP protocol for data communication. After several attempts, the final choice of the open source PHP extension: Swoole, is written in C-multithreaded asynchronous server.

Swoole Official website:,

Swoole Getting Started tutorial and documentation: Https://

Where binary data communication uses the pack () and unpack () functions that are included with PHP. It is important to note that when you use Pack () to pack letters, you need to pass in their corresponding ASCII code values.

Attach a shell script that closes the socket service (when the code is updated and the socket service needs to be restarted):

#!/bin/SHEcho "start kill socket ..."PID= ' netstat-anp|grep 10001|awk '{printf $7}'|Cut-d/-F1 'Echo "PID: $PID"Kill- - "$PID"Echo "killed PID 10001"#假定此socket服务的端口为10001Kill-usr1
Shell Online Brochure: Http:// Online Brochure: installation steps for the ps:php extension (
1 First enter the source directory of the extension to be installed
2 Generate configure files under the Swoole folder #/usr/local/php5/bin/phpize
Configuring for:
PHP Api version:20041225
Zend Module Api no:20060613
The Zend Extension Api no:220060519 appears with such hints that can be extended. 3 Configuring compilation and Installation #./configure--with-php-config=/usr/local/php5/bin/php-config
#make #make INSTALL4 generates a file under/opt/php/lib/php/extensions 5 edit php.ini (if PHP is installed in/USR/LOCAL/PHP5 then php.ini in/usr/ Add in Local/php5/lib/php.ini
Extension_dir = "/usr/local/php5/lib/php/extensions/no-debug-non-zts-20060613/"
Extension = "" 6 restarts Apache, and then through Php-m or phpinfo () to see if the swoole extension was successfully loaded.

Using PHP to build a socket service based on Swoole extension (with PHP extension installation steps)

Related Article

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: 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.