Title:学习笔记-XML之远程实例化类库示例Create:2019-04-10 ◊ :668

:2019-04-10 01:03
: TabKey9  :0  :0  


不知道为什么,我的笔记好像只有我才能看得懂,所以,别往下看了,应该很乱。 要看就看总结吧!

总结:适合用来写授权相关功能。就是把项目核心功能放在我的服务器上,再给授权了的用户提供公钥私钥,也让他们用这种方式来远程 new class($url); 这样就很好的保护了核心代码,即使源码被恶意传播并篡改也无伤大雅(◔◡◔)

function getRenzheng(外网类库URL,自定义参数1,参数2,参数3){
    $url=$url.'?wsdl';//URL传参,wsdl:ws类库代理的意思,外网类库的作者定义的
    header("Content-type: text/html; charset=utf-8");
    $xmldata='<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Header>
    <MySoapHeader xmlns="http://www.gxeea.cn/">
      <SecurityKey>参数1</SecurityKey>
    </MySoapHeader>
  </soap:Header>
  <soap:Body>
    <getRenzheng xmlns="http://www.gxeea.cn/">
      <pDaihao>参数2</pDaihao>
      <pData>参数3</pData>
    </getRenzheng>
  </soap:Body>
</soap:Envelope>';
    header("text/xml; charset=utf-8");//改成xml文档类型
//PHP的try函数,详见:https://www.php.net/manual/zh/language.exceptions.php
    try{
        $client = new SoapClient($url);//实例化外网类库,成功则继续执行代码,失败则执行catch错误捕捉
        $result = $client->__doRequest($xmldata,$url,'http://www.gxeea.cn/getRenzheng',1,0);//实例化成功,调用发送XML的方法,后面这句原话暂时看不懂:“发送xml必须使用__doRequest”
        $xml=simplexml_load_string($result);//这时接受到XML字符串,转换成对象
        $res=$xml->children('http://schemas.xmlsoap.org/soap/envelope/')->children('http://www.gxeea.cn/')->getRenzhengResponse->getRenzhengResult;//不用在意这行,无非就是从对象里取某些值出来
        return $res;//return
    }catch (SoapFault $e){//第一个错误捕捉
        echo $e->getMessage();
    }catch(Exception $e){//第二个错误捕捉,不知道为什么写两个?
        echo $e->getMessage();
    }
}//虽然还是有不理解的地方,但我至少学会怎么用了

请求和响应示例:218.65.204.245:8005/ws_renzheng.asmx?op=getRenzheng