최근 PHP에서 웹 서비스에 대한 많은 정보를 읽었습니다. 공유해 준 Yan Shiba에게 감사를 표하며 서버 구축에 도움을 주었습니다. Yan Shiba의 노트를 포함하여 다음과 같이 학습노트를 기록하세요.
웹서비스
1 웹서비스를 빠르게 이해
일반인의 관점에서 말하면 특정 XML 형식에 따라 원격 서버의 메서드를 호출하면 서버는 특정 형식의 XML 콘텐츠를 반환합니다.
"특정 형식"------SOAP(Simple Object Access Protocol) Simple Object 액세스 프로토콜은 분산된 환경에서 정보를 교환하기 위한 간단한 프로토콜입니다. XML 기반 프로토콜입니다
.
원격 서버 ---- 일반적으로 HTTP 프로토콜을 통해 메시지를 전달합니다
요약 : WebServie == HTTP 프로토콜 + Soap 형식의 XML
예시 1: 비누 요청
POST /WebServices/MobileCodeWS.asmx HTTP/1.1
호스트: webservice.webxml.com.cn
콘텐츠 유형: text/xml; charset=utf-8
콘텐츠 길이: 354
SOAPAction: "http://WebXml.com.cn/getMobileCodeInfo"
="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3 .org/2001/XMLSchema" cn/getMobileCodeInfo >
PHP.ini 수정
Extension=php_soap.dll 앞의 ";"를 제거합니다.
아파치를 다시 시작합니다.
PHP SoapClient 클래스를 사용하여 WebService를 요청할 수 있습니다
$soap = <span>new</span> soapClient(<span>'</span><span>http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx?WSDL</span><span>'</span><span>); print_r($soap</span>->getMobileCodeInfo( array(<span>'</span><span>mobileCode</span><span>'</span>=><span>'</span><span>13**********</span><span>'</span>) ) );
<span>Array ( [</span><span>0</span>] =><span> getMobileCodeInfoResponse getMobileCodeInfo(getMobileCodeInfo $parameters) [</span><span>1</span>] =><span> getDatabaseInfoResponse getDatabaseInfo(getDatabaseInfo $parameters) ) Array ( [</span><span>0</span>] => <span>struct</span><span> getMobileCodeInfo { </span><span>string</span><span> mobileCode; </span><span>string</span><span> userID; } [</span><span>1</span>] => <span>struct</span><span> getMobileCodeInfoResponse { </span><span>string</span><span> getMobileCodeInfoResult; } [</span><span>2</span>] => <span>struct</span><span> getDatabaseInfo { } [</span><span>3</span>] => <span>struct</span><span> getDatabaseInfoResponse { ArrayOfString getDatabaseInfoResult; } [</span><span>4</span>] => <span>struct</span> ArrayOfString {문자열 문자열;
}
<span>//</span><span> 调用方法</span> print_r($soap->getMobileCodeInfo( array(<span>'</span><span>mobileCode</span><span>'</span>=><span>'</span><span>13426060134</span><span>'</span>) ) );결과 반환
stdClass Object ( [getMobileCodeInfoResult] => <span>13*********</span>:北京 北京 北京移动动感地带卡 )3 WebService 서버 구축
wsdl이란 무엇인가요?
wsdl은 WebService의 사양입니다.
<?xml version =<span>'</span><span>1.0</span><span>'</span> encoding =<span>'</span><span>UTF-8</span><span>'</span> ?> <definitions name=<span>'</span><span>自定义名称[可选]</span><span>'</span><span> targetNamespace</span>=<span>'</span><span>命名空间[一般为URL]</span><span>'</span><span> xmlns:tns</span>=<span>'</span><span>命名空间[值同targetNamespace]</span><span>'</span><span> xmlns:soap</span>=<span>'</span><span>http://schemas.xmlsoap.org/wsdl/soap/</span><span>'</span><span> xmlns:xsd</span>=<span>'</span><span>http://www.w3.org/2001/XMLSchema</span><span>'</span><span> xmlns:soapenc</span>=<span>'</span><span>http://schemas.xmlsoap.org/soap/encoding/</span><span>'</span><span> xmlns:wsdl</span>=<span>'</span><span>http://schemas.xmlsoap.org/wsdl/</span><span>'</span><span> xmlns</span>=<span>'</span><span>http://schemas.xmlsoap.org/wsdl/</span><span>'</span>> <!--<types> 元素定义 web service 使用的数据类型,WSDL 使用 XML Schema 语法来定义数据类型,也可以自定义Schema不包含的类型--> <types> <xsd:schema xmlns:xsd=<span>"</span><span>http://www.w3.org/2001/XMLSchema</span><span>"</span><span> targetNamespace</span>=<span>"</span><span>[值同上tns]</span><span>"</span>> </xsd:schema> </types> <!-- <message><span> 元素可定义每个消息的部件,以及相关联的数据类型. </span>--> <message name=<span>'</span><span>操作名Request</span><span>'</span>> <part name=<span>"</span><span>term</span><span>"</span> type=<span>"</span><span>xsd:string</span><span>"</span>/> </message> <message name=<span>'</span><span>操作名Response</span><span>'</span>> <part name=<span>"</span><span>value</span><span>"</span> type=<span>"</span><span>xsd:string</span><span>"</span>/> </message> <!-- <portType><span> 元素是最重要的 WSDL 元素.它可描述一个 web service、可被执行的操作,以及相关的消息. 它告诉你去哪个WebService的连接点,扮演了一个控制者. </span>--> <portType name=<span>'</span><span>操作列表名</span><span>'</span>> <operation name=<span>'</span><span>操作名</span><span>'</span>> <input message=<span>'</span><span>tns:操作名Request</span><span>'</span>/> <output message=<span>'</span><span>tns:操作名Response</span><span>'</span>/> </operation> </portType> <!--<binding> 元素为每个端口定义消息格式和协议细节--> <binding name=<span>'</span><span>WS下的频道名称</span><span>'</span> type=<span>'</span><span>tns:频道下的操作列表</span><span>'</span>> <!--style:属性可取值 <span>"</span><span>rpc</span><span>"</span> 或 <span>"</span><span>document</span><span>"</span>,ransport:属性定义了要使用的 SOAP 协议.在这个例子中我们使用 HTTP--> <soap:binding style=<span>'</span><span>rpc</span><span>'</span><span> transport</span>=<span>'</span><span>http://schemas.xmlsoap.org/soap/http</span><span>'</span>/> <!--operation 元素定义了每个端口提供的操作符,对于每个操作,相应的 SOAP 行为都需要被定义--> <operation name=<span>'</span><span>test</span><span>'</span>> <soap:operation soapAction=<span>'</span><span>http://www.cwtservice.cn/newOperation/</span><span>'</span>/> <input> <soap:body use=<span>'</span><span>encoded</span><span>'</span> <span>namespace</span>=<span>'</span><span>urn:xmethods-delayed-quotes</span><span>'</span><span> encodingStyle</span>=<span>'</span><span>http://schemas.xmlsoap.org/soap/encoding/</span><span>'</span>/> </input> <output> <soap:body use=<span>'</span><span>encoded</span><span>'</span> <span>namespace</span>=<span>'</span><span>urn:xmethods-delayed-quotes</span><span>'</span><span> encodingStyle</span>=<span>'</span><span>http://schemas.xmlsoap.org/soap/encoding/</span><span>'</span>/> </output> </operation> </binding> <!--<service>包含一个或者多个port元素,每个port元素表示一个不同的Web服务--> <service name=<span>'</span><span>WebService名称[如weatherWS,shopWS]</span><span>'</span>> <port name=<span>'</span><span>WS下的频道名称[如cartSoap,购物车服务]</span><span>'</span> binding=<span>'</span><span>tns:[频道名,同左]</span><span>'</span>> <soap:address location=<span>'</span><span>http://[webservice地址]</span><span>'</span>/> </port> </service> </definitions>이상 내용을 포함하여 WebService-php-1(16)을 소개하였습니다. PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.