【PHP+NuSoap】PHP+NuSoap构建WSDL服务器?

人狠话不多,废话不多说,直接上代码。使用的是ThinkPHP5.1.x框架

服务器端,许多复杂的结构和情况都涉及到了,可以作为参考

soap_defencoding = 'UTF-8';
        $server->decode_utf8 = false;
        $server->xml_encoding = 'UTF-8';
        $server->configureWSDL ('AsphaltMixplantDataUploadWsdl','urn:AsphaltMixplantDataUploadWsdl',"http://wsdl.local/services/Asphalt_Mixplant_Data_Upload_Intf?wsdl"); // 打开 wsdl 支持
        /**
         * 注册需要被客户端访问的程序类型对应值:
         * bool->"xsd:boolean"
         * string->"xsd:string"
         * int->"xsd:int"
         * float->"xsd:float"
         */
        $input = new \ReflectionClass(AsphaltMixplantDataUploadInput::class);
        $params = $input->getProperties();
        $props = [];
        foreach ($params as $param){
            if( $param->getName() === 'data' ){
                $props["{$param->getName()}"] = "tns:AsphaltMixplantDataUpload_Data";

                $data = new \ReflectionClass( AsphaltMixplantDataUploadData::class);
                $data_params = $data->getProperties();
                $datas = [];
                foreach ($data_params as $data_param){
                    $datas["{$data_param->getName()}"] = array("name" => $data_param->getName(),"type" => isset(AsphaltMixplantDataUploadType::$typeof['data'][$data_param->getName()]) ? "xsd:" . AsphaltMixplantDataUploadType::$typeof['data'][$data_param->getName()] : "xsd:" . AsphaltMixplantDataUploadType::$defaultType,'nillable' => "true",'minOccurs' => 0);
                }
                $server->wsdl->addComplexType(
                    'AsphaltMixplantDataUpload_Data',
                    'complexType',
                    'struct',
                    'all',
                    '',
                    $datas
                );

            }elseif( $param->getName() === 'formula' ){
                $props["{$param->getName()}"] = "tns:AsphaltMixplantDataUpload_Formula";

                $formula = new \ReflectionClass(AsphaltMixplantDataUploadFormula::class);
                $formula_params = $formula->getProperties();
                $formulas = [];
                foreach ($formula_params as $formula_param){
                    $formulas["{$formula_param->getName()}"] = array("name" => $formula_param->getName(),"type" => isset(AsphaltMixplantDataUploadType::$typeof['formula'][$formula_param->getName()]) ? "xsd:" . AsphaltMixplantDataUploadType::$typeof['formula'][$formula_param->getName()] : "xsd:" . AsphaltMixplantDataUploadType::$defaultType,'nillable' => "true",'minOccurs' => 0);
                }

                $server->wsdl->addComplexType(
                    'AsphaltMixplantDataUpload_Formula',
                    'complexType',
                    'struct',
                    'all',
                    '',
                    $formulas
                );

            }else{
                $props["{$param->getName()}"] = isset( AsphaltMixplantDataUploadType::$typeof[$param->getName()] ) ? "xsd:" . AsphaltMixplantDataUploadType::$typeof[$param->getName()] : "xsd:" . AsphaltMixplantDataUploadType::$defaultType;
            }

        }

        $server->register ( "asphaltMixplantDataUpload", // 方法名
            $props, // 参数,默认为 "xsd:string"
            array ("code" => "xsd:int","msg" => "xsd:string","transIdo" => "xsd:string" ),
            'urn:AsphaltMixplantDataUploadWsdl',     // namespace
            'urn:AsphaltMixplantDataUploadWsdl#asphaltMixplantDataUpload',    // soapaction
            'rpc',        // style
            'encoded',       // use
            'Asphalt Mixplant Data Upload'   // documentation
        ); // 返回值,默认为 "xsd:string"
        //isset  检测变量是否设置
        //$HTTP_RAW_POST_DATA = isset ( $HTTP_RAW_POST_DATA ) ? $HTTP_RAW_POST_DATA : '';
        $HTTP_RAW_POST_DATA = file_get_contents('php://input');
        //service  处理客户端输入的数据
        $server->service ( $HTTP_RAW_POST_DATA );
        exit();
    }

}

客户端方法,直接放在了common.php中,其中涉及到的获取传参,需要特别注意,传参的顺序一定要固定,不能变,不然数据会错乱。

/**
 * 沥青拌合楼数据上传
 * @return array
 */
function asphaltMixplantDataUpload(){
    // 获取当前所有参数
    list($dischargeSn,$mixingTime,$deviceSn,$dischargeTime,$verificateCode,$token,$data,$formula) = func_get_args();
    if( isset($token) && tokenValid($token) ){
        //设备号 + 时间 复合主键
        if( QcAsphaltMixPlant::where('dischargeSn',$dischargeSn)->count() > 0 ){
            return wsdlReturn(Code::REPEAT,'数据重复!',date('YmdHis'));
        }

        $model = new QcAsphaltMixPlant;

        $pk = $model->allowField(true)->save( [
            'dischargeSn' => $dischargeSn,
            'mixingTime' => $mixingTime,
            'deviceSn' => $deviceSn,
            'dischargeTime' => $dischargeTime,
            'verificateCode' => $verificateCode,
            'token' => $token
        ] );

        if( isset($data) && $pk && $data ){
            $data['pk_id'] = $model->id;
            $dm = new QcAsphaltMixPlantData();
            $dm->allowField( true )->save( $data );
        }

        if( isset($formula) && $pk && $formula){
            $formula['pk_id'] = $model->id;
            $df = new QcAsphaltMixPlantFormula();
            $df->allowField( true )->save( $formula );
        }
        return wsdlReturn(Code::SUCCESS,'返回成功!', $model->guid);
    }else{
        return wsdlReturn(Code::PARAM_ERROR,'Token错误!',date('YmdHis'));
    }
}

你可能感兴趣的:(PHP)