略微加速

略速 - 互联网笔记

PHP5 扩展SOAP 调用 webservice

2020-11-19 leiting (1699阅读)

标签 PHP

php4时代调用webservice大部分使用的nusoap。到了php5已经有了自己的soap扩展。所以可以完全的抛弃nusoap这个许久没有更新过的东西了。

因为目前是本地开发需要。只说windows下的。

配置环境

windows下找到php安装目录下的php.ini。打开后编辑。找到

extension=php_soap.dll

然后将前面的;号去掉。

然后就是写一个php文件来验证一下。

实例程序

在apache的htdocs目录下创建ws.php

代码如下:

<?php
header("content-type:text/html;charset=utf-8");
try {
    $client = new SoapClient( 'http://erp.test.com/SendSMS/Service.asmx?wsdl',array('trace' => true, 'exceptions' => true ));
    var_dump($client->__getFunctions());
} catch (SOAPFault $e) {
    print_r($e);
}
?>

在浏览器中输入:http://localhost/ws.php后会出现

array
  0 => string 'SendSMSResponse SendSMS(SendSMS $parameters)' (length=44)
  1 => string 'SendSMS1Response SendSMS1(SendSMS1 $parameters)' (length=47)
  2 => string 'SendMailResponse SendMail(SendMail $parameters)' (length=47)
  3 => string 'IsSendSMSResponse IsSendSMS(IsSendSMS $parameters)' (length=50)
  4 => string 'IsSendSMS1Response IsSendSMS1(IsSendSMS1 $parameters)' (length=53)
  5 => string 'SendSMSResponse SendSMS(SendSMS $parameters)' (length=44)
  6 => string 'SendSMS1Response SendSMS1(SendSMS1 $parameters)' (length=47)
  7 => string 'SendMailResponse SendMail(SendMail $parameters)' (length=47)
  8 => string 'IsSendSMSResponse IsSendSMS(IsSendSMS $parameters)' (length=50)
  9 => string 'IsSendSMS1Response IsSendSMS1(IsSendSMS1 $parameters)' (length=53)

现在逐行解释一下。

                    $client = new SoapClient(

                    'http://erp.test.com/SendSMS/Service.asmx?wsdl',array('trace' =>

                    true, 'exceptions' => true ));
                   这里的SoapClient类可以作为给定的ws的客户端。这个SoapClient有两种操作模式。
                   一个是WSDL模式,一个是Non-WSDL模式。
                   当然这里用的是WSDL模式。所以重点来说第一种。
                   WSDL模式中,SoapClient的构造参数分别是ws的请求地址以及各种请求配置参数。
                   var_dump($client->__getFunctions());
                   这里就是访问后输出的这个接口可提供的方法,返回值以及参数。
                   那么如何去调用方法呢。

<?php
header("content-type:text/html;charset=utf-8");
try {
    $client = new SoapClient( 'http://erp.test.com/SendSMS/Service.asmx?wsdl',
                                array('trace' => true, 'exceptions' => true ));
    var_dump($client->__getFunctions());
    //第一个参数是命名空间,第二个参数是SoapHeader头的类名,第三个是SoapHeader参数的数组可以写成array
    $v = array("Token"=>"");
    $headers = new SoapHeader("http://test.com/","AuthenticationHeader",$v, false, SOAP_ACTOR_NEXT);
    $client->__setSoapHeaders(array($headers));
    //$types = $client->__getTypes();    //这里是为了查看方法的类型
    //print_r($types);
    //这里就是根据方法参数的需要虚拟出来一个sms类型的数组
    $sms1 = array(
        'Id'=>100000,
        'SjNo'=>'13512222222',
        'UnickName'=>'tuangou',
        'SmsContent'=>'test',
        'Type'=>1000,
        'OrderIdString'=>'1231114567'
    );
    $param = array(
        'sms1'=>$sms1
    );
    //这里是需要注意到地方。调用方法的参数必须是一个数组。而且默认以parameters字段标识为参数数组。真正的参数都要放在$param变量中。
    $return = $client->__soapCall("SendSMS1",array('parameters'=>$param));
    print_r($return);
} catch (SOAPFault $e) {
    print_r('Exception:'.$e);
}
?>
POST /SendSMS/Service.asmx HTTP/1.1
Host: erp.test.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://test.com/SendSMS1"

<?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:Header>
    <AuthenticationHeader xmlns="http://test.com/">
      <Token>string</Token>
    </AuthenticationHeader>
  </soap:Header>
  <soap:Body>
    <SendSMS1 xmlns="http://test.com/">
      <sms1>
        <Id>int</Id>
        <SjNo>string</SjNo>
        <UnickName>string</UnickName>
        <SmsContent>string</SmsContent>
        <Type>int</Type>
        <OrderIdString>string</OrderIdString>
      </sms1>
    </SendSMS1>
  </soap:Body>
</soap:Envelope>

这里可以看到。soap的Header和Body。Header里就是Token。Body里就是具体的方法了。

SendSMS1节点是方法名。

sms1节点就是参数。

sms1节点下的就是参数的属性。

一定要按照顺序对属性进行逐个赋值。


http://www.360doc.com/content/11/0212/16/15103_92457365.shtml


北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3