09年末做过一次webservice,10年末又做一次webservice,比较两次,意境真不同,记录一下

09年是第一次做所以曲折不断,很怀念去年在兔哥公司的那段日子,PHP用的心潮澎湃、地动山摇。在此感谢网财给我的历练与磨难。

10年这次做算是很顺利,没有什么难度,感觉少了一份释然,作为一名老鸟已没有太多兴奋的情绪,多一点的只是感觉双肩背负的责任更重了一层。新年即将到来,祝大家财源广进、步步高升、儿孙满堂。。。

总结下webservice的实现过程:

第一、写服务端

<?php
/**
 * 文件名:server.php
 * 功     能:soap服务端
 * 作     者:陈飞
 * 日     期:2010年12月21日
 */
session_start();
class DemoServer{

 public function conn(){
  
  $link = mysql_connect('host','root','password')or die('数据库连接错误!');
  
  mysql_select_db('gdwh',$link)or die('数据库不存在!');
  
  mysql_query("set names 'gbk'");
 
 }
 
 
 
    /**
     * userstatus
     * @param string $username
     * @return string $username
     * @return string $uid
     * @return string $error
     */
 public function userStatus($username){//正确则返回会员的ID;错误则返回-100

  $this->conn();
  
  $sql = "select uid from user where uname='$username'";
  
  $result = mysql_query($sql);
  
  $row = mysql_fetch_array($result);
  
  $uid = $row[0];
 
  //$error = mysql_error();
  
  if($uid != ""){
   
   return "<?xml version='1.0' encoding='gb2312'?><user><username>$username</username><uid>$uid</uid></user>";
   
  }else{
   
   return "<?xml version='1.0' encoding='gb2312'?><user><username>-100</username><uid>-100</uid></user>";
  }
  
  
 }
 
 /**
  * creatComent
  * @param string $post
  * @param string $username
  * @return string $postid
  * @return string $error
  */
 public function creatComent($post,$username){
  
  $this->conn();

  if($post=="" or $username==""){
   
   return "<?xml version='1.0' encoding='gb2312'?><result>-10</result>";
   exit;
  }
  
  $time = time();
  
  $insert = "insert into videomainreview (mid,post,uname,creatTime) values ('','$post','$username','$time')"; 
  
  $result = mysql_query($insert);
  
  if($result){
   
   $postid = mysql_insert_id();
   
   return "<?xml version='1.0' encoding='gb2312'?><postid>$postid</postid>";
   
  }else{
   
   return "<?xml version='1.0' encoding='gb2312'?><postid>-20</postid>";
   
  }
  
 }
 
 
}

/*
$user = new DemoServer();
print_r($user->creatComent('测试创建视频主贴的接口是否正常工作!','user0002'));
*/

ini_set("soap.wsdl_cache_enabled", "0");//清除缓存
$server = new SoapServer('DemoServer.wsdl',array('soap_version' => SOAP_1_2));//加载soap类
$server->setClass('DemoServer');//设置类名
$server->handle();
?>

第二步:生成wsdl文件

还是用zend studio 6.0,新版的(8.0)试过跟老版本的有些差异,比较麻烦;

file->explort->php->wsdl

填好类,服务端php文件的路径,设置完毕ok

<?xml version='1.0' encoding='UTF-8'?>

<!-- WSDL file generated by Zend Studio. -->

<definitions name="DemoServer" targetNamespace="urn:DemoServer" xmlns:typens="urn:DemoServer" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/">
 <message name="conn"/>
 <message name="connResponse"/>
 <message name="creatComent">
  <part name="post" type="xsd:string"/>
  <part name="username" type="xsd:string"/>
 </message>
 <message name="creatComentResponse">
  <part name="creatComentReturn" type="xsd:string"/>
 </message>
 <message name="userStatus">
  <part name="username" type="xsd:string"/>
 </message>
 <message name="userStatusResponse">
  <part name="userStatusReturn" type="xsd:string"/>
 </message>
 <portType name="DemoServerPortType">
  <operation name="conn">
   <input message="typens:conn"/>
   <output message="typens:connResponse"/>
  </operation>
  <operation name="creatComent">
   <documentation>
    creatComent
   </documentation>
   <input message="typens:creatComent"/>
   <output message="typens:creatComentResponse"/>
  </operation>
  <operation name="userStatus">
   <documentation>
    userstatus
   </documentation>
   <input message="typens:userStatus"/>
   <output message="typens:userStatusResponse"/>
  </operation>
 </portType>
 <binding name="DemoServerBinding" type="typens:DemoServerPortType">
  <soap:binding style="rpc" transport="
http://schemas.xmlsoap.org/soap/http"/>
  <operation name="conn">
   <soap:operation soapAction="urn:DemoServerAction"/>
   <input>
    <soap:body namespace="urn:DemoServer" use="encoded" encodingStyle="
http://schemas.xmlsoap.org/soap/encoding/"/>
   </input>
   <output>
    <soap:body namespace="urn:DemoServer" use="encoded" encodingStyle="
http://schemas.xmlsoap.org/soap/encoding/"/>
   </output>
  </operation>
  <operation name="creatComent">
   <soap:operation soapAction="urn:DemoServerAction"/>
   <input>
    <soap:body namespace="urn:DemoServer" use="encoded" encodingStyle="
http://schemas.xmlsoap.org/soap/encoding/"/>
   </input>
   <output>
    <soap:body namespace="urn:DemoServer" use="encoded" encodingStyle="
http://schemas.xmlsoap.org/soap/encoding/"/>
   </output>
  </operation>
  <operation name="userStatus">
   <soap:operation soapAction="urn:DemoServerAction"/>
   <input>
    <soap:body namespace="urn:DemoServer" use="encoded" encodingStyle="
http://schemas.xmlsoap.org/soap/encoding/"/>
   </input>
   <output>
    <soap:body namespace="urn:DemoServer" use="encoded" encodingStyle="
http://schemas.xmlsoap.org/soap/encoding/"/>
   </output>
  </operation>
 </binding>
 <service name="DemoServerService">
  <port name="DemoServerPort" binding="typens:DemoServerBinding">
   <soap:address location="
http://113.108.192.245:81/gdwh/webserver/gdwh.php"/>
  </port>
 </service>
</definitions>

第三步:写客户端测试

<?php  
  $client = new SoapClient('http://113.108.192.245:81/gdwh/webserver/DemoServer.wsdl');   
  array(  
  "trace"=>1,  
  "exceptions"=>0,  
  "soap_version" => SOAP_1_2);  
$result = $client->creatComent('thank your test','user0004');  
print_r($result);
?>

基本就是这样实现的吧!

打个广告:小弟搞了个网站  http://www.580zq.com  混口饭吃 希望朋友们多照顾照顾!