최근에 PHP를 사용하여 호출할 C# 및 JAVA용 웹 서비스를 만들어야 합니다. 3일 동안 검색하고 시도한 끝에 마침내 C#에서 호출하는 데 성공했습니다. , 여기에 과정을 기록하겠습니다.
이 글은 많은 글을 인용하고 있으며, 그 예시도 글에 사용되었습니다.
이 글의 디렉토리는 다음과 같습니다
1. php로 웹서비스를 생성합니다
2.
3. c# php 웹서비스 호출 1. php로 웹서비스 만들기 1. PHP 환경 아파치를 사용하고 있습니다. php_soap.dll이 설치되어 있는 경우 ini 파일에서 주석 확장자=php_soap.dll을 제거하기만 하면 됩니다. 환경 테스트, cmd 창에 다음 명령어를 입력하여 비누가 정상인지 확인합니다 c:>php --ini ; phpinfo()로 보는 내용이 일치하지 않을 수 있습니다c:>php -i |findstr "soap" ;비누 모듈이 정상인지 확인하는 데 사용됩니다c :>php -r "new SoapClient ('http://localhost/wsdl/person.wsdl');"; 비누를 직접 실행하는 데 사용됩니다. 2. PHP 서버 파일(TestWebService.php)<span>php </span><span>class</span><span> TestWebService { </span><span>public</span><span>function</span><span> HelloWorld() { </span><span>return</span><span>array</span>("HelloWorldResult"=>"welcome to dongzi world"<span>); } </span><span>public</span><span>function</span> GetArray(<span>$args</span><span>) { </span><span>/*</span><span> 注意,Web Service的方法在声明时至多一个参数, 可是在调用该方法时就必须传value1,value2两个参数。 (这一点十分令人费解,我的理解是,在调用该方法时,系统把所有参数都放到一个<strong>对象</strong>里传过来的) </span><span>*/</span><span>$value1</span> = <span>$args</span>-><span>value1; </span><span>$value2</span> = <span>$args</span>->value2;<span>//</span><span>这两句是获取真正的参数 </span><span>$arry</span> = <span>array</span>(<span>$value1</span>,<span>$value2</span><span>); </span><span>//</span><span>返回值也很特别,不是直接返回$arry,而是把它放到一个<strong>对象</strong>里再返回。 </span><span>return</span><span>array</span>("GetArrayResult"=><span>$arry</span><span>); } } </span><span>//</span><span>创建WebSevice实例 </span><span>$server</span> = <span>new</span> SoapServer("TestWebService.wsdl"<span>); </span><span>//</span><span>指定类名 </span><span>$server</span>->setClass("TestWebService"<span>); </span><span>$server</span>-><span>handle(); </span>?>코드 보기
2.wsdl 파일 생성
원래는 제가 더 간단하게 만들고 싶었고 SoapDiscovery.class .php를 사용하고 싶었지만 항상 여러 가지 이상한 문제가 발생했습니다. Zend Studio 12.5를 설치해야 했지만 다른 이상한 문제가 여전히 발생했습니다. 마침내 vs2010을 사용하여 생성하는 것입니다. C# 호출에는 문제가 없습니다. 1. vs2010을 열고 새 프로젝트 만들기 - ASP.NET 빈 웹 애플리케이션 2. 솔루션리소스 관리자 - 프로젝트(오른쪽 클릭) - 추가 항목 - 웹 서비스, 이름이 TestWebService.asmx로 변경되고 코드는 다음과 같습니다.
<span>using</span><span> System; </span><span>using</span><span> System.Collections.Generic; </span><span>using</span><span> System.Linq; </span><span>using</span><span> System.Web; </span><span>using</span><span> System.Web.Services; </span><span>namespace</span><span> Web<strong>Application</strong>1 { </span><span>///</span><span><summary></summary></span><span>///</span><span> TestWebService 的摘要说明 </span><span>///</span><span></span> [WebService(Namespace = <span>"</span><span>http://tempuri.org/</span><span>"</span><span>)] [WebServiceBinding(ConformsTo </span>=<span> WsiProfiles.BasicProfile1_1)] [System.ComponentModel.ToolboxItem(</span><span>false</span><span>)] </span><span>//</span><span> 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。 </span><span>//</span><span> [System.Web.Script.Services.ScriptService]</span><span>public</span><span>class</span><span> TestWebService : System.Web.Services.WebService { [WebMethod] </span><span>public</span><span>string</span><span> HelloWorld() { </span><span>return</span><span>""</span><span>; } [WebMethod] </span><span>public</span><span>string</span>[] GetArray(<span>string</span> a,<span>string</span><span> b) { </span><span>return</span><span>null</span><span>; } } }</span>코드 보기HelloWorld() 및 GetArray()는 우리가 노출하려는 메서드입니다. 빈 메서드로 작성했습니다. 3. F5를 실행하고 팝업 IE 브라우저에서 실행 중인 URL 주소를 복사합니다. http://localhost:63463/TestWebService.asmx
열기 새 IE 브라우저 창에 를 붙여넣고 변경한 후 Enter를 누르면 wsdl 파일의 xml 형식 콘텐츠가 표시됩니다:
http://localhost:63463 /TestWebService.asmx?wsdl4. IE 브라우저의 파일-다른 이름으로 저장 메뉴를 클릭하여 TestWebService.wsdl 파일로 저장한 후 TestWebService.php와 같은 디렉터리에 넣습니다. 코드는 다음과 같습니다.<span></span><span>xml version="1.0" encoding="utf-8"</span><span>?></span><span><span>wsdl:definitions </span><span>xmlns:soap</span><span>="http://schemas.xmlsoap.org/wsdl/soap/"</span><span> xmlns:tm</span><span>="http://microsoft.com/wsdl/mime/textMatching/"</span><span> xmlns:soapenc</span><span>="http://schemas.xmlsoap.org/soap/encoding/"</span><span> xmlns:mime</span><span>="http://schemas.xmlsoap.org/wsdl/mime/"</span><span> xmlns:tns</span><span>="http://tempuri.org/"</span><span> xmlns:s</span><span>="http://www.w3.org/2001/XMLSchema"</span><span> xmlns:soap12</span><span>="http://schemas.xmlsoap.org/wsdl/soap12/"</span><span> xmlns:http</span><span>="http://schemas.xmlsoap.org/wsdl/http/"</span><span> targetNamespace</span><span>="http://tempuri.org/"</span><span> xmlns:wsdl</span><span>="http://schemas.xmlsoap.org/wsdl/"</span><span>></span><span><span>wsdl:types</span><span>></span><span><span>s:schema </span><span>elementFormDefault</span><span>="qualified"</span><span> targetNamespace</span><span>="http://tempuri.org/"</span><span>></span><span><span>s:element </span><span>name</span><span>="HelloWorld"</span><span>></span><span><span>s:complexType </span><span>/></span><span></span><span>s:element</span><span>></span><span><span>s:element </span><span>name</span><span>="HelloWorldResponse"</span><span>></span><span><span>s:complexType</span><span>></span><span><span>s:sequence</span><span>></span><span><span>s:element </span><span>minOccurs</span><span>="0"</span><span> maxOccurs</span><span>="1"</span><span> name</span><span>="HelloWorldResult"</span><span> type</span><span>="s:string"</span><span>/></span><span></span><span>s:sequence</span><span>></span><span></span><span>s:complexType</span><span>></span><span></span><span>s:element</span><span>></span><span><span>s:element </span><span>name</span><span>="GetArray"</span><span>></span><span><span>s:complexType</span><span>></span><span><span>s:sequence</span><span>></span><span><span>s:element </span><span>minOccurs</span><span>="0"</span><span> maxOccurs</span><span>="1"</span><span> name</span><span>="a"</span><span> type</span><span>="s:string"</span><span>/></span><span><span>s:element </span><span>minOccurs</span><span>="0"</span><span> maxOccurs</span><span>="1"</span><span> name</span><span>="b"</span><span> type</span><span>="s:string"</span><span>/></span><span></span><span>s:sequence</span><span>></span><span></span><span>s:complexType</span><span>></span><span></span><span>s:element</span><span>></span><span><span>s:element </span><span>name</span><span>="GetArrayResponse"</span><span>></span><span><span>s:complexType</span><span>></span><span><span>s:sequence</span><span>></span><span><span>s:element </span><span>minOccurs</span><span>="0"</span><span> maxOccurs</span><span>="1"</span><span> name</span><span>="GetArrayResult"</span><span> type</span><span>="tns:ArrayOfString"</span><span>/></span><span></span><span>s:sequence</span><span>></span><span></span><span>s:complexType</span><span>></span><span></span><span>s:element</span><span>></span><span><span>s:complexType </span><span>name</span><span>="ArrayOfString"</span><span>></span><span><span>s:sequence</span><span>></span><span><span>s:element </span><span>minOccurs</span><span>="0"</span><span> maxOccurs</span><span>="unbounded"</span><span> name</span><span>="string"</span><span> nillable</span><span>="true"</span><span> type</span><span>="s:string"</span><span>/></span><span></span><span>s:sequence</span><span>></span><span></span><span>s:complexType</span><span>></span><span></span><span>s:schema</span><span>></span><span></span><span>wsdl:types</span><span>></span><span><span>wsdl:message </span><span>name</span><span>="HelloWorldSoapIn"</span><span>></span><span><span>wsdl:part </span><span>name</span><span>="parameters"</span><span> element</span><span>="tns:HelloWorld"</span><span>/></span><span></span><span>wsdl:message</span><span>></span><span><span>wsdl:message </span><span>name</span><span>="HelloWorldSoapOut"</span><span>></span><span><span>wsdl:part </span><span>name</span><span>="parameters"</span><span> element</span><span>="tns:HelloWorldResponse"</span><span>/></span><span></span><span>wsdl:message</span><span>></span><span><span>wsdl:message </span><span>name</span><span>="GetArraySoapIn"</span><span>></span><span><span>wsdl:part </span><span>name</span><span>="parameters"</span><span> element</span><span>="tns:GetArray"</span><span>/></span><span></span><span>wsdl:message</span><span>></span><span><span>wsdl:message </span><span>name</span><span>="GetArraySoapOut"</span><span>></span><span><span>wsdl:part </span><span>name</span><span>="parameters"</span><span> element</span><span>="tns:GetArrayResponse"</span><span>/></span><span></span><span>wsdl:message</span><span>></span><span><span>wsdl:portType </span><span>name</span><span>="TestWebServiceSoap"</span><span>></span><span><span>wsdl:operation </span><span>name</span><span>="HelloWorld"</span><span>></span><span><span>wsdl:input </span><span>message</span><span>="tns:HelloWorldSoapIn"</span><span>/></span><span><span>wsdl:output </span><span>message</span><span>="tns:HelloWorldSoapOut"</span><span>/></span><span></span><span>wsdl:operation</span><span>></span><span><span>wsdl:operation </span><span>name</span><span>="GetArray"</span><span>></span><span><span>wsdl:input </span><span>message</span><span>="tns:GetArraySoapIn"</span><span>/></span><span><span>wsdl:output </span><span>message</span><span>="tns:GetArraySoapOut"</span><span>/></span><span></span><span>wsdl:operation</span><span>></span><span></span><span>wsdl:portType</span><span>></span><span><span>wsdl:binding </span><span>name</span><span>="TestWebServiceSoap"</span><span> type</span><span>="tns:TestWebServiceSoap"</span><span>></span><span><span>soap:binding </span><span>transport</span><span>="http://schemas.xmlsoap.org/soap/http"</span><span>/></span><span><span>wsdl:operation </span><span>name</span><span>="HelloWorld"</span><span>></span><span><span>soap:operation </span><span>soapAction</span><span>="http://tempuri.org/HelloWorld"</span><span> style</span><span>="document"</span><span>/></span><span><span>wsdl:input</span><span>></span><span><span>soap:body </span><span>use</span><span>="literal"</span><span>/></span><span></span><span>wsdl:input</span><span>></span><span><span>wsdl:output</span><span>></span><span><span>soap:body </span><span>use</span><span>="literal"</span><span>/></span><span></span><span>wsdl:output</span><span>></span><span></span><span>wsdl:operation</span><span>></span><span><span>wsdl:operation </span><span>name</span><span>="GetArray"</span><span>></span><span><span>soap:operation </span><span>soapAction</span><span>="http://tempuri.org/GetArray"</span><span> style</span><span>="document"</span><span>/></span><span><span>wsdl:input</span><span>></span><span><span>soap:body </span><span>use</span><span>="literal"</span><span>/></span><span></span><span>wsdl:input</span><span>></span><span><span>wsdl:output</span><span>></span><span><span>soap:body </span><span>use</span><span>="literal"</span><span>/></span><span></span><span>wsdl:output</span><span>></span><span></span><span>wsdl:operation</span><span>></span><span></span><span>wsdl:binding</span><span>></span><span><span>wsdl:binding </span><span>name</span><span>="TestWebServiceSoap12"</span><span> type</span><span>="tns:TestWebServiceSoap"</span><span>></span><span><span>soap12:binding </span><span>transport</span><span>="http://schemas.xmlsoap.org/soap/http"</span><span>/></span><span><span>wsdl:operation </span><span>name</span><span>="HelloWorld"</span><span>></span><span><span>soap12:operation </span><span>soapAction</span><span>="http://tempuri.org/HelloWorld"</span><span> style</span><span>="document"</span><span>/></span><span><span>wsdl:input</span><span>></span><span><span>soap12:body </span><span>use</span><span>="literal"</span><span>/></span><span></span><span>wsdl:input</span><span>></span><span><span>wsdl:output</span><span>></span><span><span>soap12:body </span><span>use</span><span>="literal"</span><span>/></span><span></span><span>wsdl:output</span><span>></span><span></span><span>wsdl:operation</span><span>></span><span><span>wsdl:operation </span><span>name</span><span>="GetArray"</span><span>></span><span><span>soap12:operation </span><span>soapAction</span><span>="http://tempuri.org/GetArray"</span><span> style</span><span>="document"</span><span>/></span><span><span>wsdl:input</span><span>></span><span><span>soap12:body </span><span>use</span><span>="literal"</span><span>/></span><span></span><span>wsdl:input</span><span>></span><span><span>wsdl:output</span><span>></span><span><span>soap12:body </span><span>use</span><span>="literal"</span><span>/></span><span></span><span>wsdl:output</span><span>></span><span></span><span>wsdl:operation</span><span>></span><span></span><span>wsdl:binding</span><span>></span><span><span>wsdl:service </span><span>name</span><span>="TestWebService"</span><span>></span><span><span>wsdl:port </span><span>name</span><span>="TestWebServiceSoap"</span><span> binding</span><span>="tns:TestWebServiceSoap"</span><span>></span><span><span>soap:address </span><span>location</span><span>="http://localhost:63463/TestWebService.asmx"</span><span>/></span><span></span><span>wsdl:port</span><span>></span><span></span><span>wsdl:service</span><span>></span><span></span><span>wsdl:definitions</span><span>></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span>코드 보기http://localhost를 입력하세요. :63463/ TestWebService.asmx를 최종적으로 방문하게 될 URL로 수정하세요. http://192.168.1.5/wsdl/006/TestWebService.php
3. c# php webservice 호출
1. 새 웹사이트 만들기 - ASP.NET 웹사이트 2. 솔루션리소스 관리자 - 프로젝트(오른쪽 클릭) - 추가 Web Reference , URL에 입력:
http://192.168.1.5/wsdl/006/TestWebService.php?wsdl 다음 창이 표시됩니다:[참조 추가]를 클릭합니다3. Default.aspx.cs 코드를
<span>using</span><span> System; </span><span>using</span><span> System.Collections.Generic; </span><span>using</span><span> System.Linq; </span><span>using</span><span> System.Web; </span><span>using</span><span> System.Web.UI; </span><span>using</span><span> System.Web.UI.WebControls; </span><span>public</span><span>partial</span><span>class</span><span> _Default : System.Web.UI.Page { </span><span>protected</span><span>void</span> Page_Load(<span>object</span><span> sender, EventArgs e) { </span><span>//</span><span>初始化WebService </span> WebReference.TestWebService srv = <span>new</span><span> WebReference.TestWebService(); </span><span>//</span><span>调第一个方法 </span><span>string</span> str =<span> srv.HelloWorld(); </span><span>//</span><span>调第二个方法 </span><span>string</span>[] arry = srv.GetArray(<span>"</span><span>string1</span><span>"</span>, <span>"</span><span>string2</span><span>"</span><span>); Response.Write(str); } }</span>으로 수정합니다. 코드 보기4. CTRL F5를 실행하면 상단에 "welcome to dongzi world"가 나타나는 것을 확인하세요. 이는 호출 성공을 의미합니다!
위 내용은 PHP로 웹서비스를 생성하고 C#을 통해 호출하는 실제 사례를 소개하며, 관련 내용도 포함되어 있어 PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되길 바랍니다.