socol 发表于 2013-2-7 02:01:13

Fedora 13+httpd+mysql+php搭建soap,并使用curl模拟soap xml请求

1. 安装相关rpm包,如果你足够懒的话,可以直接使用install-soap.sh
install-soap.sh:

#! /bin/shyum install mysql* -yyum install php-mysql -yyum install php php-soap php-pear-SOAP -ypear install -f -o SOAP 
2. 建立soap服务端php,并且放到“/var/www/html/servercenter“
soap_all_srv.php:

<?php// PEAR::SOAP einbindenrequire_once "SOAP/Server.php";$skiptrace =& PEAR::getStaticProperty('PEAR_Error', 'skiptrace');$skiptrace = true;// Service-Classclass mytimeserv {// __dispatch_mappublic $__dispatch_map = array ();// In/Out param -> __dispatch_mappublic function __construct() {    $this->__dispatch_map["now"] =      array ("in" => array("format" => "string"),             "out" => array("time" => "string"));}// get back __dispatch_map in __dispatchpublic function __dispatch($methodname) {    if (isset($this->__dispatch_map[$methodname])) {      return $this->__dispatch_map[$methodname];    }      return NULL;}// servicemthod with parametersfunction now ($format) {    // formaterror?    if (($format == null) || (trim($format) == "")) {      // send errormessage      return new SOAP_Fault("Kein Parameter angegeben","0815", "Client");    }    date_default_timezone_set('Europe/Berlin');    $time = date ($format);      // return SOAP-Obj.    return (new SOAP_Value('time','string', $time));}   }// service-class$service = new mytimeserv();// server$ss = new SOAP_Server();// add service with name$ss->addObjectMap (&$service,"urn:mytimeserv");// service or wsdlif (isset($_SERVER["REQUEST_METHOD"])&& $_SERVER["REQUEST_METHOD"] == "POST") {    // postdata -> service    $ss->service ($HTTP_RAW_POST_DATA);} else {// wsdl-param in urlif (isset($_SERVER['QUERY_STRING']) && strcasecmp($_SERVER['QUERY_STRING'],'wsdl') == 0) {      // DISCO_Server for WSDL    require_once "SOAP/Disco.php";    $disco = new SOAP_DISCO_Server ($ss,"mytimeserv","My Time Service");      // set HTML-Header    header("Content-type: text/xml");    // return wsdl    print $disco->getWSDL ();}}?>  

3. 建立测试soap服务的客户端php,并且放到“/var/www/html/servercenter“
soap_all_client.php:
<?phpprint "client send request...\n";require_once "SOAP/Client.php";// SOAP/WSDL//$sw = new SOAP_WSDL ("http://example.com/soap_all_srv.php?wsdl");$sw = new SOAP_WSDL ("http://localhost/servercenter/soap_all_srv.php?wsdl");// Proxy-Obj.$proxy = $sw->getProxy ();// servicemthod$erg = $proxy->now ("H:i:s");print "wsdl retrun:";// returnprint $erg."\n";?> 4. 启动httpd服务器
sudo service restart httpd 5. 以上我们就建立好了一个soap wsdl服务页,可以在firefox中输入:
http://localhost/servercenter/soap_all_client.php
得到返回结果:
client send request...wsdl retrun:11:33:56
 
6. 接下来,我们来点不一样的:)
使用curl命令psot xml来模拟请求:
curl -d @request.xml -H "Content-Type: text/xml;charset=UTF-8 "http://localhost/servercenter/soap_all_srv.php?wsdl
request.xml:
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="urn:mytimeserv"><soap:Body><now><format>H:i:s</format></now></soap:Body></soap:Envelope> 
返回结果:
<?xml version="1.0" encoding="UTF-8"?><SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns4="urn:mytimeserv" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns4:nowResponse><time xsi:type="xsd:string">12:42:37</time></ns4:nowResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>  
 
7. 通过php调用mysql
a. 建立mysql db 和对应表结构
create-sample-tables.sql:
CREATE TABLE users (user_idINTNOT NULL,nameTEXT(8)NOT NULL,addTEXT(35)NOT NULL,PRIMARY KEY (user_id)); # mysqladmin -u root -p create testdb// ------------------------------------------------------------// If this step get error, you can try below command:// # service mysqld stop// # mysqld_safe --skip-grant-tables &// # mysql -uroot -p// mysql> update user set password=PASSWORD("111111")where user="root";// mysql> flush privileges;// mysql> quit// # service mysqld restart// ------------------------------------------------------------# mysql -u root -p testdb < create-sample-tables.sql  b. 插入数据
# mysql -u root -p testdbmysql> describe users;mysql> INSERT INTO users VALUES (123000, 'namebbb', 'addressaaaa');mysql> select * from users; c.新建测试php
conmysql.php:
<?php    // Delimiters may be slash, dot, or hyphen    $userinfo = "1649011,张三,住址aaa ";    list($id, $name, $add) = split(',', $userinfo);    // Connecting, selecting database    $link = mysql_connect('localhost', 'root', '111111')      or die('Could not connect: ' . mysql_error());    echo 'Connected successfully, and query result:<br />';    mysql_select_db('testdb') or die('Could not select database');    $insert="INSERT INTO users VALUES ('$id', '$name', '$add')";    mysql_query ($insert) or die('Query failed: ' . mysql_error());    // Performing SQL query    $query = 'SELECT * FROM users';    $result = mysql_query($query) or die('Query failed: ' . mysql_error());      // Printing results in HTML    echo "<table>\n";    while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {      echo "\t<tr>\n";      foreach ($line as $col_value) {            echo "\t\t<td>$col_value</td>\n";      }      echo "\t</tr>\n";    }    echo "</table>\n";      // Free resultset    mysql_free_result($result);      // Closing connection    mysql_close($link);?>  d.在firefox中输入:
       http://localhost/servercenter/conmysql.php
 
    得到运行结果:
 
Connected successfully, and query result:123000   namebbb addressaaaa'1649011 张三          住址aaa
页: [1]
查看完整版本: Fedora 13+httpd+mysql+php搭建soap,并使用curl模拟soap xml请求