The php gRPC server framework with php-fpm and nginx.

Overview

php-grpc-server-protobuf

The php grpc server framework with protobuf and DO NOT use any 3rd libraries or use Swoole. Support protobuf and json request.

Architecture

mode with nginx & php-fpm (only support php client and http json request)

  1. gRPC Client => nginx => php-fpm => this framework => custom services => protobuf binary response
  2. http/https json request (content-type:application/json) => nginx => php-fpm => this framework => custom services => json response

mode with swoole (all gRPC Client and http json request)

  1. gRPC Client => Swoole => this framework => custom services => protobuf binary response
  2. http/https json request (content-type:application/json) => Swoole => this framework => custom services => json response

Usage

  1. install with composer
composer require "hetao29/php-grpc-server-protobuf:dev-main"
  1. use in php file, like samples/www/index.php (php-fpm mode)
<?php
define("ROOT",				dirname(__FILE__)."/../");
define("ROOT_LIBS",			ROOT."/libs");
define("ROOT_APP",			ROOT."/app");
define("ROOT_PROTO_GENERATED",		ROOT."/proto_generated");
require_once(ROOT_LIBS."/vendor/autoload.php");
spl_autoload_register(function($class){
	$root = ROOT_PROTO_GENERATED."/".str_replace("\\","/",$class).".php";
	if(is_file($root)){
		require_once($root);
	}
});
spl_autoload_register(function($class){
	$root = ROOT_APP."/".str_replace("\\","/",$class).".php";
	if(is_file($root)){
		require_once($root);
	}
});

try{
	$content_type = (isset($_SERVER['HTTP_CONTENT_TYPE']) && $_SERVER['HTTP_CONTENT_TYPE']=='application/json') ? 'json' : null; //json | null (default)
	if(($r=GRpcServer::run(null,null,$content_type))!==false){
		echo($r);
	}
}catch(Exception $e){
	print_r($e);
}
  1. or swoole server
<?php
require __DIR__ . '/../libs/vendor/autoload.php';

define("ROOT",				__DIR__."/../");
define("ROOT_APP",			__DIR__."/../app");
define("ROOT_PROTO_GENERATED",		__DIR__."/../proto_generated/");
spl_autoload_register(function($class){
	$root = ROOT_PROTO_GENERATED."/".str_replace("\\","/",$class).".php";
	if(is_file($root)){
		require_once($root);
	}
});
spl_autoload_register(function($class){
	$root = ROOT_APP."/".str_replace("\\","/",$class).".php";
	if(is_file($root)){
		require_once($root);
	}
});


$http = new Swoole\Http\Server('0.0.0.0', 50000, SWOOLE_BASE);

$http->on('request', function (Swoole\Http\Request $request, Swoole\Http\Response $response) use ($http) {
	$content_type = (isset($request->header['content-type']) && $request->header['content-type']=='application/json') ? 'json' : null; //json | null (default)
	if($content_type=="json"){
		$response->header('content-type', 'application/json');
	}else{
		$response->header('content-type', 'application/grpc');
	}
	try{
		if(($r=GRpcServer::run($request->server['request_uri'], $request->rawContent(), $content_type))!==false){
			//echo($r);
			$response->header('trailer', 'grpc-status, grpc-message');
			$trailer = [
				"grpc-status" => "0",
				"grpc-message" => ""
			];
			foreach ($trailer as $trailer_name => $trailer_value) {
				$response->trailer($trailer_name, $trailer_value);
			}
			$response->end($r);
	}
	}catch(Exception $e){
		$response->header('trailer', 'grpc-status, grpc-message');
		$trailer = [
			"grpc-status" => $e->getCode(),
			"grpc-message" => $e->getMessage(),
		];
		foreach ($trailer as $trailer_name => $trailer_value) {
			$response->trailer($trailer_name, $trailer_value);
		}
		$response->end();
	}
});
$http->start();

Write App Services

  1. proto and genproto to php files
cd proto && make
  1. write gRPC Server in services dir like helloworld
<?php
namespace Test\Helloworld;
class Greeter{
	/**
	 * @return HelloReply
	 */
	public function SayHello(HelloRequest $request) : HelloReply{
		$reply = new HelloReply();
		$reply->setMessage("Hello, ".$request->getName()."!");
		return $reply;
	}
}
  1. config nginx && php-fpm
server {
	listen 50010 http2; #with http2 is grpc protocol
	#listen 50010; #without http2 is json protocol
	root /data/server/;
	location / {
		if (!-e $request_filename){
			rewrite ^/(.+?)$ /index.php last;
		}
	}
	location ~ \.php$ {
		fastcgi_param REMOTE_ADDR $http_x_real_ip;
		fastcgi_pass   127.0.0.1:9000;
		fastcgi_index  index.php;
		fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
		include        fastcgi_params;
	}
}
  1. test
# cli mode
php client/hello.php

# curl command (json request)
# swoole (50000 port)
curl -d '{"name":"xx"}' -v http://127.0.0.1:50000//Test.Helloworld.Greeter/SayHello -H "content-type:application/json"
# nginx php-fpm (50010 port)
curl -d '{"name":"xx"}' -v http://127.0.0.1:50010//Test.Helloworld.Greeter/SayHello -H "content-type:application/json"

# or web browser 
You might also like...
PHP Server Monitor

PHP Server Monitor Version 3.6.0.beta2 PHP Server Monitor is a script that checks whether your websites and servers are up and running. It comes with

Linfo PHP Server Health Status
Linfo PHP Server Health Status

Linfo - Server stats UI/library Linfo is a: Light themable Web UI and REST API displaying lots of system stats Ncurses CLI view of WebUI Extensible, e

Web based groupware server written in PHP
Web based groupware server written in PHP

EGroupware Tools Usage runs unit-tests after each commit scrutinizer runs static analysis on our codebase manual testing with unusual browser versions

NamelessMC is a free, easy to use & powerful website software for your Minecraft server
NamelessMC is a free, easy to use & powerful website software for your Minecraft server

NamelessMC - v2 pre-release 10 NamelessMC is a free, easy to use & powerful website software for your Minecraft server, which includes a large range o

A simple, not so bad looking Minecraft Server's website template

Minecraft Server 官网模板 本仓库为 Minecraft 服务器官网模板,主要通过 Bootstrap 和 Argon 组件库实现 本项目基于 https://github.com/nyancatda/mcserverweb 二开 config.json参数说明 参数 说明 备注 s

Easily manage your HP server's fans speeds, anywhere!

iLO Fans Controller See my comment on r/homelab to know the reason why I made this! How it works To get the current speeds of the fans, the PHP script

Chat Application developed for Server Side Development

ChatApp 💬 Chat Application developed for Server Side Development (@IESClaraDelRey) 🧐 About this file The purpose of this file is to provide overview

CI4-Lic is a software license manager modul for Codeigniter 4, connecting to WordPress license server based on the Software License Manager Plugin.

CI4-Lic CI4-Lic is a software license manager modul for Codeigniter 4, connecting to WordPress license server based on Software License Manager Plugin

Automatic SASS-to-CSS compiling for Laravel 4 (and any other framework too), config-free, in pure PHP, works with latest SASS 3.2 .scss syntax, imports and mixins

laravel-sass Automatic Sass-to-CSS compiling for Laravel 4 (and any other framework by the way) while being in development. Every time you run your ap

Owner
HeTao
HeTao
Engintron for cPanel/WHM is the easiest way to integrate Nginx on your cPanel/WHM server.

Engintron for cPanel/WHM is the easiest way to integrate Nginx on your cPanel/WHM server. Engintron will improve the performance & web serving capacity of your server, while reducing CPU/RAM load at the same time. It does that by installing & configuring the popular Nginx webserver to act as a reverse caching proxy for static files (like CSS, JS, images etc.) with an additional micro-cache layer to significantly improve performance of dynamic content generated by CMSs like WordPress, Joomla or Drupal as well as forum software like vBulletin, phpBB, SMF or e-commerce solutions like Magento, OpenCart, PrestaShop and others.

Engintron 632 Dec 14, 2022
This prj we have two NODEMCU ( ESP8266) and two RFID_RC522 and some rfid tags we used ARDUINO IDE on NODEMCU and running server with XAMPP

This prj we have two NODEMCU ( ESP8266) and two RFID_RC522 and some rfid tags we used ARDUINO IDE on NODEMCU and running server with XAMPP

Mohammadamir Soltanzadeh 2 Mar 29, 2022
A great looking and easy-to-use photo-management-system you can run on your server, to manage and share photos.

Lychee A great looking and easy-to-use photo-management-system. Since the 1st of April 2018 this project has moved to it's own Organisation (https://g

Tobias Reich 6.2k Jan 5, 2023
While BotMan itself is framework agnostic, BotMan is also available as a bundle with the great Laravel PHP framework.

BotMan Studio About BotMan Studio While BotMan itself is framework agnostic, BotMan is also available as a bundle with the great Laravel PHP framework

BotMan 322 Dec 26, 2022
This is a plugin written in PHP programming language and running on the PocketMine platform that works stably on the API 4.0.0 platform. It allows you to query some other server information

QueryServer This is a plugin written in PHP programming language and running on the PocketMine platform that works stably on the API 4.0.0 platform. I

Thành Nhân 1 Jul 6, 2022
Online-examination-System in PHP and Mysql using XAMPP server

online-examination-systen-in-php Online Examination System Today Online Examination System has become a fast growing examination method because of its

Web_Warriors 1 Nov 28, 2021
A unified front-end for different queuing backends. Includes a REST server, CLI interface and daemon runners.

PHP-Queue A unified front-end for different queuing backends. Includes a REST server, CLI interface and daemon runners. Why PHP-Queue? Implementing a

CoderKungfu 646 Dec 30, 2022
Server manager is a open source project made for people so that they can add the servers to one single place irrespective of their provider and manage it through one location.

Server Manager Are you sick of having to log into hundreads of different website just to access your server? Well we got you, Server manager is a open

null 8 Aug 9, 2022
CTFx is a CTF Platform forked from mellivora, that focuses on low memory footprint and low server CPU usage

CTFx is a CTF Platform forked from mellivora, that focuses on low memory footprint and low server CPU usage. It has a futuristic interface that's optimized for slower hardware, meaning that there is no bulky Javascript running in the background, nor length CSS stylesheets. CTFx improves on the mellivora CTF engine by the UI redesign and the addition of new features.

Milkdrop 7 Dec 22, 2022
A self-hosted, drag-and-drop, & nosql file conversion server that supports 62x file formats

A self-hosted, drag-and-drop, & nosql file conversion server that supports 62x file formats

Justin Grimes 388 Dec 26, 2022