PHP发送xml格式数式和接收xml数据并保存到数据库

步骤:服务器做了操作(新增用户)推送消息给客户端;因服务器不在我这里,我就写了个本地模拟发送xml数据;


    /**
     * @author tanwenwei
     * 描述:接收xml数据
     */
    public function getxml(){
        //1.获取到推送过来post数据(xml格式)
        $postArr = @$GLOBALS['HTTP_RAW_POST_DATA'];
        $postObj = simplexml_load_string($postArr);
        M('settings')->where(array('id'=>7))->save(['value'=>(string)$postObj->Content]);
//        $reqdata = "<xml><Encrypt><![CDATA[RypEvHKD8QQKFhvQ6QleEB4J58tiPdvo+rtK1I9qca6aM/wvqnLSV5zEPeusUiX5L5X/0lWfrf0QADHHhGd3QczcdCUpj911L3vg3W/sYYvuJTs3TUUkSUXxaccAS0qhxchrRYt66wiSpGLYL42aM6A8dTT+6k4aSknmPj48kzJs8qLjvd4Xgpue06DOdnLxAUHzM6+kDZ+HMZfJYuR+LtwGc2hgf5gsijff0ekUNXZiqATP7PF5mZxZ3Izoun1s4zG4LUMnvw2r+KqCKIw+3IQH03v+BCA9nMELNqbSf6tiWSrXJB3LAVGUcallcrw8V2t9EL4EhzJWrQUax5wLVMNS0+rUPA3k22Ncx4XXZS9o0MBH27Bo6BpNelZpS+/uh9KsNlY6bHCmJU9p8g7m3fVKn28H3KDYA5Pl/T8Z1ptDAVe0lXdQ2YoyyH2uyPIGHBZZIs2pDBS8R07+qN+E7Q==]]></Encrypt><MsgSignature><![CDATA[477715d11cdb4164915debcba66cb864d751f3e6]]></MsgSignature><TimeStamp><![CDATA[1409659813]]></TimeStamp><Nonce><![CDATA[1372623149]]></Nonce></xml>";
//        $sync = new \Sync(C('ssosync.token'),C('ssosync.encodingAesKey'),C('ssosync.corpId'));
//        $encrypt = $sync->decodedata($reqdata);
    }
    /**
     * @author tanwenwei
     * 描述:发送xml数据
     */
    public function postxml(){
        $xmlData = "<xml>
                    <Content><![CDATA[测试时间:2018-11-20]]></Content >
                    </xml >";
        $url = 'localhost:1299/index.php/sync/getxml'; //接收xml数据的文件
        $header[] = "Content-type: text/xml";      //定义content-type为xml,注意是数组
        $ch = curl_init ($url);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch,CURLOPT_POSTFIELDS, $xmlData);
        $response = curl_exec($ch);
        if(curl_errno($ch)){
            printcurl_error($ch);
        }
        curl_close($ch);
    }

模拟的时候在浏览器直接调用postxml方法,如果数据库插入成功数据,说明接收成功;注:便用HTTP_RAW_POST_DATA需要在php.ini文件配置always_populate_raw_post_data为on

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值