首页 > 代码库 > 处理WebService asmx的经验

处理WebService asmx的经验

项目的需求,需要和一个.net系统进行数据交换,合作方提供了一个WebService接口。这个与一般的PHP POST或GET传值再查库拿数据的思路有点不一样,需要用到SOAP模块,处理方法也很简单,就是有一些需要注意的事情。大都会娱乐城

首先确认你的PHP.ini开启了.SOAP,就是 extension=php_soap.dll 这前面的分号去咯。

代码很简单:

view source
 
print?
01<?php
02 
03$client = new SoapClient(‘http://www.nowamagic.net/SearchService.asmx?WSDL‘);
04 
05$client->soap_defencoding = ‘utf-8‘
06$client->decode_utf8 = false;  
07$client->xml_encoding = ‘utf-8‘;
08 
09$param = array(‘param1‘=>‘01‘, ‘param2‘=>‘02‘);
10 
11//$param["param1"]="01";
12//$param["param2"]="02";
13 
14//$result = $client->__soapCall("GetArticle", array( $param ));
15$result = $client->__Call("GetArticle", array( $param ));
16 
17if (is_soap_fault($result))
18{
19    trigger_error("SOAP Fault: (faultcode: {$result->faultcode}, faultstring: {$result->faultstring})", E_USER_ERROR);
20}
21else
22{
23    $data = $result->GetArticleResult;   //这里返回的是类,必须使用->得到元素的值
24    print_r($data);
25}
26?>

需要注意的一点是,参数是数组外再包一层数组,就是 array( array() )

附SOAP接口的一些参数:

以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。

view source
 
print?
01POST /SearchService.asmx HTTP/1.1
02Host: 202.105.183.61
03Content-Type: text/xml; charset=utf-8
04Content-Length: length
05SOAPAction: "http://tempuri.org/GetTrafficViolationInfo"
06 
07<?xml version="1.0" encoding="utf-8"?>
08<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/">
09  <soap:Body>
10    <GetArticle xmlns="http://tempuri.org/">
11      <param1>string</param1>
12      <param2>string</param2>
13    </GetArticle>
14  </soap:Body>
15</soap:Envelope>
view source
 
print?
01HTTP/1.1 200 OK
02Content-Type: text/xml; charset=utf-8
03Content-Length: length
04 
05<?xml version="1.0" encoding="utf-8"?>
06<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/">
07  <soap:Body>
08    <GetArticleResponse xmlns="http://tempuri.org/">
09      <GetArticleResult>string</GetArticleResult>
10    </GetArticleResponse>
11  </soap:Body>
12</soap:Envelope>

处理WebService asmx的经验