php webservice实现客户端提交数据库数据到服务器并返回另一份数据库数据

来源:互联网 发布:苹果电脑怎么使用软件 编辑:程序博客网 时间:2024/06/05 06:30

由于公司需求,需使用webservice来开发公司erp的服务器和客户端的两边数据库交换。

即每次把客户端更新的数据上传到生产用的服务器端并把服务器端刚更新的数据返回回来。(服务器端有脚本在运行更新数据)


由于使用的是php语言,当前网络上大部分解决上传问题的都是java和C。小白的我在学习过程中吃了很多苦头,

包括也去尝试使用socket来进行上传文件。还有利用fsockpen GET/POST提交表单及其上传文件。

这两种对于只会php的我来说都是具有可行性的,不过老大要我尽量用webservice,因此最后决定回归webservice,并使用soap的传输特性来完成这个小项目。


对于webservice,我们一开始都会给服务器增加身份验证功能,即客户端要与服务器连通,必须要提前配置好对应的账号密码,不然服务器拒绝客户端使用它的东西。

身份验证:

SoapHeader - SoapHeader类

  • SoapHeader :: __ construct - SoapHeader构造函数
  • SoapHeader :: SoapHeader - SoapHeader构造函数

关于身份验证就不多说,我之前的博客有说过,网上也很多相关文章。

因此,客户端在这过程中提交了账号,密码到服务器。并根据验证结果返回值。

那我就可以通过这个,来把客户端获取到的数据,在传输账号密码的时候顺带传过去。

SoapVar - SoapVar类

  • SoapVar :: __构造 - SoapVar构造函数
  • SoapVar :: SoapVar - SoapVar构造函数

这里使用soapvar把所需传输给服务器的数据封到里面。具体可以查查相关资料。

因为我试了下服务器只接收字符串,因此我这里采用的是把数组先转换为字符串,到服务器的时候再转换回数组来。

其实我也不知道soapvar定义数据类型的时候能否定义数组形式,下次优化的时候回过头来再试试。


上代码,server服务器端的 soapservice_local.php

<?php/* * 定义一个SOAP服务器  */header("Content-type: text/html; charset=utf-8"); ini_set("soap.wsdl_cache_enabled", "0"); //创建一个类class mysoapclass {      private $Authenticated =FALSE;    private $posted =FALSE;    //定义一个判断的函数,来识别客户端是否为非法入侵。     public function authenticate($value)    {                    //$name =$value->item[0]->value;          //$pwd =$value->item[1]->value;      if($value->username == 'admin'&& $value ->password=='123456')     {        $this->Authenticated = true;                        }     else     {        $this->Authenticated = FALSE;          }    if ($this->Authenticated)     {        $aaa=$value->clientsoap;        $array_client = explode('##',$aaa);         for($index=0;$index<count($array_client);$index++)         {         echo $array_client[$index];echo "</br>";         }             $mysql=@mysql_connect("localhost","weberp","erp.shai3c.com");            if (!$mysql)            {              die('Could not connect: ' . mysql_error());            }            //$get_now=$array_client[17];            $mysql_database="weberp";            $aa=mysql_query("set names utf8;");            $mysql_database=mysql_select_db($mysql_database);             $sql="update prices set startdate='".$array_client[12]."',                                    enddate='".$array_client[14]."',                                    updatedate='".$array_client[16]."',                                    operator='".$array_client[18]."',                                    storelink='".$array_client[22]."',                                     competitorid='".$array_client[24]."',                                     price='".$array_client[26]."',                                     sales='".$array_client[28]."',                                     store_sales='".$array_client[30]."',                                     state='".$array_client[32]."',                                     store_state='".$array_client[34]."',                                     getpricedate='".$array_client[36]."',                                     storedate='".$array_client[38]."'                                                                     where stockid='".$array_client[0]."'                                 and typeabbrev='".$array_client[2]."'";            $result = mysql_query($sql,$mysql);            //判断服务器是否接收到数据,并返回值            if ($array_client[16]=='2017-08-05')             {                            $this->posted = TRUE;             }            else             {                $this->posted = FALSE;                  }    }        }     //验证登录信息是否正确    function feline()    {            if($this->Authenticated)      {        $mysql=@mysql_connect("localhost","root","");        if (!$mysql)        {          die('Could not connect: ' . mysql_error());        }        $get_now=date('2017-05-09');        $mysql_database="weberp";        $aa=mysql_query("set names utf8;");        $mysql_database=mysql_select_db($mysql_database);         $sql="select * from prices where updatedate='".$get_now."' limit 5";        //获取有竞争者id的记录。        $result = mysql_query($sql,$mysql);        //遍历数据库 并把数据做成数组形式          while($row = mysql_fetch_array($result))         {         //var_dump($row);            $array[]=$row;                }        //var_dump($array[0]['getpricedate']);        return $array;        }          else         {           return '验证错误,请检查';              }      }    function postval()    {    if ($this->posted)     {        return "已获取数据";    }        elseif($this->posted='FALSE'){ return "获取数据失败";}      }    //连接数据库,获取数据并转换为数组  } $options = array('uri'=>'XXXXXXXXX/'); $server = new SoapServer(NULL,$options); try {    //$server->setClass('MySoapServer');    $server->setClass('mysoapclass');     //$server->addFunction("add_number");    $server->handle(); } catch(SoapFault $fault) {    echo 'SoapFault:'.$fault->faultstring; } //var_dump($server);?>

以上代码作用是服务器端接收到客户端提交的数据后,先验证账号密码是否为admin,123456,然后再执行对字符串数据的解析。

然后更新到服务器的数据库中。

下面那一段是从服务器数据库获取一些数据,传回给客户端client。并返回提交数据成功的值即”已获取数据”。

当然,如果账号密码错误。会返回失败值。


接下来为客户端。soapClient_local.php


<?php/* * 定义一个SOAP客户端  */header("Content-type: text/html;charset=utf-8");ini_set("soap.wsdl_cache_enabled", "0"); 
//下面这里就是简单的定义,和身份验证一样。class authentication_header {       var $username;       var $password;      var $clientsoap;      public function __construct($username, $password,$clientsoap)     {       $this->username = $username;       $this->password = $password;       $this->clientsoap = $clientsoap;      } } $options = array(        'location'=>'XXXXXXXXX/soapService_local.php',        'uri'=>'XXXXXXXXX/'        ); $client = new SoapClient(NULL,$options); try {    //获取人工修改过新增的数据,和账号密码一并封装,发送给service端。        $mysql=@mysql_connect("localhost","root","");                if (!$mysql)                {                  die('Could not connect: ' . mysql_error());                }                $mysql_database="weberp";                $aa=mysql_query("set names utf8;");                $mysql_database=mysql_select_db($mysql_database);         $post_sql="select * from prices where updatedate='2017-08-05' limit 1";        $post_result=mysql_query($post_sql,$mysql);        while($post_row=mysql_fetch_array($post_result))        {            $array[]=$post_row;        }
         //var_dump($array[0]);        //从数据库获取到的数据为数组形式,将其转化为字符串形式。        $clientsoap=implode("##",$array[0]);        //var_dump($clientsoap);
         //把账号密码和所需传输的字符串数据封到一起。       $auth = new authentication_header('admin', '123456',$clientsoap);       $authvalues = new SoapVar($auth, SOAP_ENC_OBJECT, 'authenticate','XXXXXXXXX/');         $header = new SoapHeader('XXXXXXXXX/', 'authenticate', $authvalues,true);       $re=$client->__setSoapHeaders(array($header));         //$message = $client->get_message();        $result =$client->__soapCall('feline',array());              $postval =$client->__soapCall('postval',array());              var_dump($postval);        var_dump($result);         }   catch(SoapFault $fault) {    var_dump($fault); }?>

以上客户端的功能就是把数据库内获取到得数组形式的数据,和账号密码一起打包,然后发送到服务器。


对此,有一个基础的框架,剩下的在这上面再完善下,对于安全性来说应该有很大的问题,不过对于小公司用的erp来说,暂且可以使用着。

谢谢




阅读全文
0 0
原创粉丝点击