Protocol Buffer学习笔记(PHP)

2018-03-19  本文已影响0人  飘逸峰

摘要

项目中引入proto的依赖

在项目根目录下执行如下命令,composer会自动下载最新版本,目前是"google/protobuf": "^3.5"
composer require "google/protobuf"

准备proto文件

生成proto对应的php文件

public function __construct() {
    \App\GPBMetadata\OTIpcDef::initOnce();
    parent::__construct();
}

php中使用protobuf

创建ProtoController

namespace App\Http\Controllers;
use App\Proto\BaseInfoCompany;
use App\Helper\HttpUtils;

class ProtoController extends Controller
{
    /**
     * ProtoController constructor.
     */
    public function __construct()
    {
    }
}

1.主动获取protobuf数据

public function build_proto_data()
    {
        $baseInfoCompany  = new BaseInfoCompany();
        $baseInfoCompany->setAddress(110011);
        $baseInfoCompany->setCompanyId("companyId");
        $baseInfoCompany->setCompanyName("companyName中文");
        $baseInfoCompany->setIdentifier("identifier");
        $baseInfoCompany->setBusinessScope("BusinessScope");
        $baseInfoCompany->setContactAddress("ContactAddress");
        $baseInfoCompany->setEconomicType("EconomicType");
        $baseInfoCompany->setRegCapital("RegCapital");
        $baseInfoCompany->setLegalName("LegalName");
        $baseInfoCompany->setLegalID("LegalID");
        $baseInfoCompany->setLegalPhone("LegalPhone");
        $baseInfoCompany->setState(0);
        $baseInfoCompany->setFlag(1);
        $baseInfoCompany->setUpdateTime(20180226121212);
        // $protostr = $baseInfoCompany->serializeToString();
        //生成json
        $protostr = $baseInfoCompany->serializeToJsonString();
        return $protostr;
    }

public function receive_proto_data(){       
        $protoData = file_get_contents("http://localhost:83/proto/build");     
        try {   
            $baseInfoCompany = new BaseInfoCompany();
//            $baseInfoCompany->mergeFromString($protoData);
            $baseInfoCompany->mergeFromJsonString($protoData);
            echo $baseInfoCompany->getCompanyName();

            return $baseInfoCompany->serializeToJsonString();
        } catch (Exception $ex) {
            die('something is wrong');
        }
    }

2.被动接收protobuf数据

public function send_proto_data(){
        $data = $this->build_proto_data();
        $receive = HttpUtils::request_by_curl("http://localhost:83/proto/receive",$data);
        echo $receive;
    }

public function receive_proto_data(){
        // $protoData = file_get_contents("http://localhost:83/proto/build");   
        $protoData = $_POST["data"];
        try {       
            $baseInfoCompany = new BaseInfoCompany();
//            $baseInfoCompany->mergeFromString($protoData);
            $baseInfoCompany->mergeFromJsonString($protoData);
            echo $baseInfoCompany->getCompanyName();
            return $baseInfoCompany->serializeToJsonString();        
        } catch (Exception $ex) {
            die('something is wrong');
        }
    }

namespace App\Helper;
class HttpUtils
{

    public static function request_by_curl($remote_server, $post_string) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $remote_server);
        curl_setopt($ch, CURLOPT_POSTFIELDS, 'data=' . $post_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_TIMEOUT, 1);//异步处理
        $data = curl_exec($ch);
        if (curl_errno($ch)) { //响应错误的处理
        }
        curl_close($ch);
        return $data;
    }
}
上一篇下一篇

猜你喜欢

热点阅读