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

linzx00 2011-06-14

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

install-soap.sh:

#! /bin/sh
yum install mysql* -y
yum install php-mysql -y
yum install php php-soap php-pear-SOAP -y
pear install -f -o SOAP

2. 建立soap服务端php,并且放到“/var/www/html/servercenter“

soap_all_srv.php:

<?php
// PEAR::SOAP einbinden
require_once "SOAP/Server.php";
$skiptrace =& PEAR::getStaticProperty('PEAR_Error', 'skiptrace');
$skiptrace = true;

// Service-Class
class mytimeserv {
  // __dispatch_map
  public $__dispatch_map = array ();
  // In/Out param -> __dispatch_map
  public function __construct() {
    $this->__dispatch_map["now"] =
      array ("in" => array("format" => "string"),
             "out" => array("time" => "string"));
  }
  // get back __dispatch_map in __dispatch
  public function __dispatch($methodname) {
    if (isset($this->__dispatch_map[$methodname])) {
      return $this->__dispatch_map[$methodname];
    }  
    return NULL;
  }
  // servicemthod with parameters
  function 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 wsdl
if (isset($_SERVER["REQUEST_METHOD"])&& $_SERVER["REQUEST_METHOD"] == "POST") {
    // postdata -> service
    $ss->service ($HTTP_RAW_POST_DATA);
} else {
  // wsdl-param in url
  if (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:

<?php
print "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:";
// return
print $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_id	INT		NOT NULL,
  name		TEXT(8)	NOT NULL,
  add	TEXT(35)	NOT NULL,
  PRIMARY KEY (user_id)
);
 
[root@localhost soap-manual]# mysqladmin -u root -p create testdb
// ------------------------------------------------------------
// If this step get error, you can try below command:
// [root@localhost soap-manual]# service mysqld stop
// [root@localhost soap-manual]# mysqld_safe --skip-grant-tables &
// [root@localhost soap-manual]# mysql -uroot -p
// mysql> update user set password=PASSWORD("111111")where user="root";
// mysql> flush privileges;
// mysql> quit
// [root@localhost soap-manual]# service mysqld restart
// ------------------------------------------------------------
[root@localhost soap-manual]# mysql -u root -p testdb < create-sample-tables.sql

  b. 插入数据

[root@localhost soap-manual]# mysql -u root -p testdb
mysql> 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

相关推荐

amberom / 0评论 2014-03-02