惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

K
Kaspersky official blog
Martin Fowler
Martin Fowler
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
V
Visual Studio Blog
博客园_首页
Engineering at Meta
Engineering at Meta
The Cloudflare Blog
MongoDB | Blog
MongoDB | Blog
Blog — PlanetScale
Blog — PlanetScale
T
The Blog of Author Tim Ferriss
雷峰网
雷峰网
D
Docker
博客园 - 司徒正美
S
SegmentFault 最新的问题
M
MIT News - Artificial intelligence
博客园 - 叶小钗
博客园 - 三生石上(FineUI控件)
U
Unit 42
J
Java Code Geeks
A
About on SuperTechFans
N
Netflix TechBlog - Medium
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
Security Affairs
I
Intezer
Cisco Talos Blog
Cisco Talos Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
B
Blog RSS Feed
P
Privacy & Cybersecurity Law Blog
T
Tenable Blog
T
Threatpost
H
Hacker News: Front Page
G
Google Developers Blog
博客园 - 【当耐特】
Hugging Face - Blog
Hugging Face - Blog
Apple Machine Learning Research
Apple Machine Learning Research
L
Lohrmann on Cybersecurity
大猫的无限游戏
大猫的无限游戏
Google DeepMind News
Google DeepMind News
A
Arctic Wolf
S
Secure Thoughts
GbyAI
GbyAI
NISL@THU
NISL@THU
S
Security @ Cisco Blogs
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Webroot Blog
Webroot Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
O
OpenAI News
Spread Privacy
Spread Privacy
Application and Cybersecurity Blog
Application and Cybersecurity Blog

博客园 - 林宁

apache2 SSI 再论mysql_unbuffered_query与mysql_query的区别 Linux下的软链接和硬链接的不同(转载) TortoiseSVN 简明使用 在linux下定时执行PHP脚本(定时执行crontab使用方法详解) php中构造函数和析构函数执行过程 如何从一个php文件向另一个地址post数据,不用表单和隐藏的变量(转) 字符截取 Linux若干小结备查寻 用 PHP 读取和编写 XML DOM 基本数据结构和php内置函数(转摘) 表单提交按钮图片形式的几种写法 php防注入 随机数、随机数种子 10 件(也许)你不了解 PHP 的事情 手机中的IMEI是什么意思? mysql替换数据库中的部分内容 一个很不错的验证类,扩展型很好(转载) 百度(baidu)的职业道德
php自带SOAP扩展调用web service
林宁 · 2008-06-16 · via 博客园 - 林宁

     提起php调用web service,在php4年代的人会立即想到用nusoap调用,这个nusoap是个好东东,不过年久未更新,恐其中会有众多漏洞了,正好已经进入了php5年代了,调用web service用自带的扩展岂不快哉!
     前提:打开php5的web service扩展
环境需求:
This extension makes use of the GNOME xml library. Download and install this library. You will need at least libxml-2.5.4.

Linux下,
This extension is only available if PHP was configured with --enable-soap
Windows下:
php.ini文件中 extension=php_soap.dll 去掉注释即可

    其次:调用实践,以下是俺近期调用的算是一些总结吧

手册摘录:
SoapClient->__soapCall()
说明
class SoapClient {

mixed __soapCall ( string function_name, array arguments [, array options [, mixed input_headers [, array &output_headers]]] )

}
This is a low level API function that is used to make a SOAP call. Usually, in WSDL mode, you can simply call SOAP functions as SoapClient methods. This method useful in non-WSDL mode when soapaction is unknown, uri differs from the default or when sending and/or receiving SOAP Headers.

On error, a call to a SOAP function can cause PHP to throw exceptions or return a SoapFault object if exceptions are disabled. To check if the function call failed to catch the SoapFault exceptions, check the result with is_soap_fault().

返回值
SOAP functions may return one, or multiple values. If only one value is returned by the SOAP function, the return value of __soapCall will be a simple value (e.g. an integer, a string, etc). If multiple values are returned, __soapCall will return an associative array of named output parameters.

范例
例 1. SoapClient->__soapCall() Examples

<?php

$client = new SoapClient("some.wsdl");
$client->SomeFunction($a, $b, $c);

$client->__soapCall("SomeFunction", array($a, $b, $c));
$client->__soapCall("SomeFunction", array($a, $b, $c), NULL,
                    new SoapHeader(), $output_headers);


$client = new SoapClient(null, array('location' => "http://localhost/soap.php",
                                     'uri'      => "http://test-uri/"));
$client->SomeFunction($a, $b, $c);
$client->__soapCall("SomeFunction", array($a, $b, $c));
$client->__soapCall("SomeFunction", array($a, $b, $c),
                    array('soapaction' => 'some_action',
                          'uri'        => 'some_uri'));
?> 
 

1.in WSDL mode,soapCall应用web service,例子用的是asp.net的web service,提供service.asmx页面,调用及查看都比较简单,手册上的example也大多是这个类型,比较简单

SOAP发送的协议:

POST /servicepath/service.asmx HTTP/1.1
Host: 211.186.1.4
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://211.186.5.15/Service/ServiceMethod"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <ServiceMethod xmlns="http://211.186.5.15/Service">
      <param1>string</param1>
      <param2>string</param2>
      <param3>string</param3>
    </ServiceMethod>
  </soap:Body>
</soap:Envelope>

调用方法:
$client = new SoapClient("http://www.xxx.com/service/service.asmx?WSDL");
//向SOAP服务方发送参数值
$param1 = "p1";
$param2 = "p2";
$param3 = "p3";

//serviceParam1,serviceParam2,serviceParam3为发送参数值所对应的参数名(或service端提供的字段名)
$param = array('serviceParam1' => $param1,'serviceParam2' => $param2,'serviceParam3' => $param3);

//方法名为ServiceMethod,参数数组为$param,默认以parameters字段标示传递的参数数组
$arr = $client->__soapCall('ServiceMethod',array('parameters' => $param));
print_r($arr);


2.in non-WSDL mode,这种情况下soapaction is unknown

SOAP发送协议

POST /services/SoapMethod?WSDL HTTP/1.1
Host: 220.211.1.12:8088
Connection: Keep-Alive
User-Agent: PHP-SOAP/5.2.5
Content-Type: text/xml; charset=utf-8
SOAPAction: "urn:SoapMethod#ServiceMethod"
Content-Length: 1297

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:SoapMethod" 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:ns2="http://220.211.1.12" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:ServiceMethod>
<ServiceMethodSection xsi:type="ns2:ServiceObjectType">
<param1 xsi:type="xsd:string">01019</param1>
<param2 xsi:type="xsd:long">10</param2>
<param3 xsi:type="xsd:long">0</param3>
<param4 xsi:type="xsd:long">11</param4>
</ServiceMethodSection>
</ns1:ServiceMethod>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

调用方法:
2.1传递一个参数:
try {
      $client = new SoapClient(null,  
      array('location' => 'http://192.168.1.180:8088/services/SoapPage?WSDL','uri' => 'http://192.168.1.180:8088/services/'));   
      $result =  $client->__soapCall('ServiceMethod', array('fieldName' => "data")); //以数组形式传递params
      //$result =  $client->__soapCall('ServiceMethod', array(new SoapParam("data", 'fieldName')));  //以构造服务端参数的形式构造参数传递给服务端 
      var_dump($result);
  }
  catch (Exception $e)
  {
      printf("Message = %s",$e->__toString());
  }
  
附:手册解释
class SoapParam {

__construct ( mixed data, string name )

}
参数

data
The data to pass or return. You can pass this parameter directly as PHP value, but in this case it will be named as paramN and the SOAP Service may not understand it.

name
The parameter name

2.2传递多个参数:
如果服务端在non wsdl的情况下要求传递一个对象参数,该对象中包含多个属性,则code如下

try {
      $client = new SoapClient(null, array('location' => 'http://192.168.1.180:8088/services/SoapPage?WSDL','uri' => 'urn:SoapName'));   //uri部分也可能是uri地址

      class Obj{ 
       public $param1 = '01019';
       public $param2 = 10;
       public $param3 = 0;
       public $param4 = 11;
      }

    $struct = new Obj();  //创建服务端要求传递的对象
    //如果服务端变态到传递的参数有的参数类型是你编程语言中没有的数据类型,如本人用php,没有Java的Long类型(一般做web service都要考虑到通用性,数据类型都是string或int型,而且我认为根本没必要用Long类型。没办法服务端不改,只能我这边改)就要将参数进行强制类型转换处理,这样的处理也只是在SOAP传输中将xml的参数类型替换成服务端所要求的类型名,并不是真实转化传递的数据类型
    $struct->param1 = iconv('gb2312','utf-8',$struct->param1);
    $struct->param2 = new SoapVar($struct->param2,XSD_LONG);
    $struct->param3 = new SoapVar($struct->param3,XSD_LONG);
    $struct->param4 = new SoapVar($struct->param4,XSD_LONG);

    //序列化对象中使用SoapVar的方法参考php手册对SoapVar的解释,每个参数都解释的很清楚
    $soapstruct = new SoapVar($struct, SOAP_ENC_OBJECT, "ServiceObjectType", "http://soapinterop.org/xsd"); //对象序列化,注意区分,SOAP对象的序列化不是用serialize
    
      $result =  $client-> ServiceMethod(new SoapParam($soapstruct, 'ServiceMethodSection'));
     //$result =  $client->__soapCall('ServiceMethod', array(new SoapParam($soapstruct, 'ServiceMethodSection')));
    
      var_dump($result);
  } catch (Exception $e) {
      printf("Message = %s",$e->__toString());
  }

附:PEAR Manual中找到的SoapVar的解释
SoapVar
SoapVar -- Changes the Returntype of a variable
Description

new SoapVar (mixed variable, long type)

Warning
This function is EXPERIMENTAL. That means, that the behaviour of this function, the function name, in concreto ANYTHING documented here can change in a future release of this package WITHOUT NOTICE. Be warned, and use this function at your own risk.
 
You can change the return type of variable - so that it works better with non-PHP applications for example.

type should be one of the following

XSD_1999_TIMEINSTANT
XSD_STRING
XSD_BOOLEAN
XSD_DECIMAL
XSD_FLOAT
XSD_DOUBLE
XSD_DURATION
XSD_DATETIME
XSD_TIME
XSD_DATE
XSD_GYEARMONTH
XSD_GYEAR
XSD_GMONTHDAY
XSD_GDAY
XSD_GMONTH
XSD_HEXBINARY
XSD_BASE64BINARY
XSD_ANYURI
XSD_QNAME
XSD_NOTATION
XSD_NORMALIZEDSTRING
XSD_TOKEN
XSD_LANGUAGE
XSD_NMTOKEN
XSD_NAME
XSD_NCNAME
XSD_ID
XSD_IDREF
XSD_IDREFS
XSD_ENTITY
XSD_ENTITYS
XSD_INTEGER
XSD_NONPOSITIVEINTEGER
XSD_NEGATIVEINTEGER
XSD_LONG
XSD_INT
XSD_SHORT
XSD_BYTE
XSD_NONNEGATIVEINTEGER
XSD_UNSIGNEDLONG
XSD_UNSIGNEDINT
XSD_UNSIGNEDSHORT
XSD_UNSIGNEDBYTE
XSD_POSITIVEINTEGER


HTTP协议精解.rar
SOAP协议规范.rar

posted on 2008-06-16 14:17  林宁  阅读(9835)  评论()    收藏  举报