{"id":21586585,"url":"https://github.com/hetao29/php-grpc-server-protobuf","last_synced_at":"2025-04-10T20:21:07.245Z","repository":{"id":37721587,"uuid":"443675775","full_name":"hetao29/php-grpc-server-protobuf","owner":"hetao29","description":"The php gRPC server framework with php-fpm and nginx.","archived":false,"fork":false,"pushed_at":"2024-04-24T04:04:39.000Z","size":7533,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2024-04-25T05:08:03.764Z","etag":null,"topics":["framework","grpc","nginx","php","php-fpm","protobuf"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hetao29.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-01-02T03:49:47.000Z","updated_at":"2024-04-24T04:04:17.000Z","dependencies_parsed_at":"2024-04-24T04:05:49.455Z","dependency_job_id":null,"html_url":"https://github.com/hetao29/php-grpc-server-protobuf","commit_stats":{"total_commits":70,"total_committers":1,"mean_commits":70.0,"dds":0.0,"last_synced_commit":"7126b26f5a54e8b78ec333999bb4bd83c6555c61"},"previous_names":[],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hetao29%2Fphp-grpc-server-protobuf","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hetao29%2Fphp-grpc-server-protobuf/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hetao29%2Fphp-grpc-server-protobuf/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hetao29%2Fphp-grpc-server-protobuf/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hetao29","download_url":"https://codeload.github.com/hetao29/php-grpc-server-protobuf/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248289865,"owners_count":21078922,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["framework","grpc","nginx","php","php-fpm","protobuf"],"created_at":"2024-11-24T15:14:06.677Z","updated_at":"2025-04-10T20:21:07.215Z","avatar_url":"https://github.com/hetao29.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# php-grpc-server-protobuf\nThe php grpc server framework with protobuf and DO NOT use any 3rd libraries or use Swoole.\nSupport protobuf and json request.\n\n# Architecture\n\n## mode with nginx \u0026 php-fpm (only support php client and http json request)\n\n1. gRPC Client  =\u003e nginx =\u003e php-fpm =\u003e this framework =\u003e custom services =\u003e protobuf binary response\n2. http/https json request (content-type:application/json) =\u003e nginx =\u003e php-fpm =\u003e this framework =\u003e custom services =\u003e json response\n\n## mode with swoole (all gRPC Client and http json request)\n\n1. gRPC Client  =\u003e Swoole =\u003e this framework =\u003e custom services =\u003e protobuf binary response\n2. http/https json request (content-type:application/json)  =\u003e Swoole =\u003e this framework =\u003e custom services =\u003e json response\n\n# Usage\n\n1. install with composer\n\n```bash\ncomposer require \"hetao29/php-grpc-server-protobuf:dev-main\"\n```\n\n2. use in php file, like samples/www/index.php (php-fpm mode)\n\n```php\n\u003c?php\ndefine(\"ROOT\",\t\t\t\tdirname(__FILE__).\"/../\");\ndefine(\"ROOT_LIBS\",\t\t\tROOT.\"/libs\");\ndefine(\"ROOT_APP\",\t\t\tROOT.\"/app\");\ndefine(\"ROOT_PROTO_GENERATED\",\t\tROOT.\"/proto_generated\");\nrequire_once(ROOT_LIBS.\"/vendor/autoload.php\");\nspl_autoload_register(function($class){\n\t$root = ROOT_PROTO_GENERATED.\"/\".str_replace(\"\\\\\",\"/\",$class).\".php\";\n\tif(is_file($root)){\n\t\trequire_once($root);\n\t}\n});\nspl_autoload_register(function($class){\n\t$root = ROOT_APP.\"/\".str_replace(\"\\\\\",\"/\",$class).\".php\";\n\tif(is_file($root)){\n\t\trequire_once($root);\n\t}\n});\n\ntry{\n\t$content_type = (isset($_SERVER['HTTP_CONTENT_TYPE']) \u0026\u0026 $_SERVER['HTTP_CONTENT_TYPE']=='application/json') ? 'json' : null; //json | null (default)\n\tif(($r=GRpcServer::run(null,null,$content_type))!==false){\n\t\techo($r);\n\t}\n}catch(Exception $e){\n\tprint_r($e);\n}\n```\n\n3. or swoole server\n\n```php\n\u003c?php\nrequire __DIR__ . '/../libs/vendor/autoload.php';\n\ndefine(\"ROOT\",\t\t\t\t__DIR__.\"/../\");\ndefine(\"ROOT_APP\",\t\t\t__DIR__.\"/../app\");\ndefine(\"ROOT_PROTO_GENERATED\",\t\t__DIR__.\"/../proto_generated/\");\nspl_autoload_register(function($class){\n\t$root = ROOT_PROTO_GENERATED.\"/\".str_replace(\"\\\\\",\"/\",$class).\".php\";\n\tif(is_file($root)){\n\t\trequire_once($root);\n\t}\n});\nspl_autoload_register(function($class){\n\t$root = ROOT_APP.\"/\".str_replace(\"\\\\\",\"/\",$class).\".php\";\n\tif(is_file($root)){\n\t\trequire_once($root);\n\t}\n});\n\n\n$http = new Swoole\\Http\\Server('0.0.0.0', 50000, SWOOLE_BASE);\n\n$http-\u003eon('request', function (Swoole\\Http\\Request $request, Swoole\\Http\\Response $response) use ($http) {\n\t$content_type = (isset($request-\u003eheader['content-type']) \u0026\u0026 $request-\u003eheader['content-type']=='application/json') ? 'json' : null; //json | null (default)\n\tif($content_type==\"json\"){\n\t\t$response-\u003eheader('content-type', 'application/json');\n\t}else{\n\t\t$response-\u003eheader('content-type', 'application/grpc');\n\t}\n\ttry{\n\t\tif(($r=GRpcServer::run($request-\u003eserver['request_uri'], $request-\u003erawContent(), $content_type))!==false){\n\t\t\t//echo($r);\n\t\t\t$response-\u003eheader('trailer', 'grpc-status, grpc-message');\n\t\t\t$trailer = [\n\t\t\t\t\"grpc-status\" =\u003e \"0\",\n\t\t\t\t\"grpc-message\" =\u003e \"\"\n\t\t\t];\n\t\t\tforeach ($trailer as $trailer_name =\u003e $trailer_value) {\n\t\t\t\t$response-\u003etrailer($trailer_name, $trailer_value);\n\t\t\t}\n\t\t\t$response-\u003eend($r);\n\t}\n\t}catch(Exception $e){\n\t\t$response-\u003eheader('trailer', 'grpc-status, grpc-message');\n\t\t$trailer = [\n\t\t\t\"grpc-status\" =\u003e $e-\u003egetCode(),\n\t\t\t\"grpc-message\" =\u003e $e-\u003egetMessage(),\n\t\t];\n\t\tforeach ($trailer as $trailer_name =\u003e $trailer_value) {\n\t\t\t$response-\u003etrailer($trailer_name, $trailer_value);\n\t\t}\n\t\t$response-\u003eend();\n\t}\n});\n$http-\u003estart();\n\n```\n\n\n# Write App Services \n\n1. proto and genproto to php files\n\n```bash\ncd proto \u0026\u0026 make\n```\n\n2. write gRPC Server in services dir like helloworld\n\n```php\n\u003c?php\nnamespace Test\\Helloworld;\nclass Greeter implements GreeterInterface{\n\t/**\n\t */\n\tpublic function SayHello(HelloRequest $request) : HelloReply{\n\t\t$reply = new HelloReply();\n\t\t$reply-\u003esetMessage(\"Hello2, \".$request-\u003egetName().\"!\");\n\t\treturn $reply;\n\t}\n}\n\n```\n\n3. config nginx \u0026\u0026 php-fpm\n\n```conf\nserver {\n\tlisten 50010 http2; #with http2 is grpc protocol\n\t#listen 50010; #without http2 is json protocol\n\troot /data/server/;\n\tlocation / {\n\t\tif (!-e $request_filename){\n\t\t\trewrite ^/(.+?)$ /index.php last;\n\t\t}\n\t}\n\tlocation ~ \\.php$ {\n\t\tfastcgi_param REMOTE_ADDR $http_x_real_ip;\n\t\tfastcgi_pass   127.0.0.1:9000;\n\t\tfastcgi_index  index.php;\n\t\tfastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;\n\t\tinclude        fastcgi_params;\n\t}\n}\n```\n4. test\n\n\n```bash\n# cli mode\nphp client/hello.php\n\n# curl command (json request)\n# swoole (50000 port)\ncurl -d '{\"name\":\"xx\"}' -v http://127.0.0.1:50000//Test.Helloworld.Greeter/SayHello -H \"content-type:application/json\"\n# nginx php-fpm (50010 port)\ncurl -d '{\"name\":\"xx\"}' -v http://127.0.0.1:50010//Test.Helloworld.Greeter/SayHello -H \"content-type:application/json\"\n\n# or web browser \n\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhetao29%2Fphp-grpc-server-protobuf","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhetao29%2Fphp-grpc-server-protobuf","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhetao29%2Fphp-grpc-server-protobuf/lists"}