繁体   English   中英

从PHP调用Web服务

[英]Calling a Webservice from PHP

我一直试图从底层到顶层的方法调用java创建的web服务,并希望通过我的PHP文件调用它,但它总是给我一个错误。

代码我尝试过:

$client = new SoapClient("http://localhost:8080/Indexer/services/IndexerService?wsdl");
$params = array(
      'anId' => 3,
      'action' => 'OMNOMNOMNOM',
      'parameters' => array(
                  'Param' => array(
                          array('Name' => 'in0', 'Value' => 'Skinnarlyngen startar Välkommen')
      )
));
$result = $client->__soapCall("getIds",$params);

print_r($result);

我试过的第二段代码:

$wsdl = 'http://localhost:8080/Indexer/services/IndexerService?wsdl';   
  $client = new SoapClient($wsdl );

  print($client->getIds("ibm"));

我的web服务在使用java客户端尝试时提供了完美的结果:

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<wsdl:definitions xmlns:soapenc12="http://www.w3.org/2003/05/soap-encoding" xmlns:tns="http://labs.aroha.com" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap11="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope" targetNamespace="http://labs.aroha.com">
<wsdl:types>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://labs.aroha.com">
<xsd:element name="example">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="in0" nillable="true" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="exampleResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element maxOccurs="1" minOccurs="1" name="out" nillable="true" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</wsdl:types>
<wsdl:message name="exampleRequest">
<wsdl:part name="parameters" element="tns:example"></wsdl:part>
</wsdl:message>
<wsdl:message name="exampleResponse">
<wsdl:part name="parameters" element="tns:exampleResponse"></wsdl:part>
</wsdl:message>
<wsdl:portType name="IndexerServicePortType">
<wsdl:operation name="example">
<wsdl:input name="exampleRequest" message="tns:exampleRequest"></wsdl:input>
<wsdl:output name="exampleResponse" message="tns:exampleResponse"></wsdl:output>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="IndexerServiceHttpBinding" type="tns:IndexerServicePortType">
<wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="example">
<wsdlsoap:operation soapAction=""/>
<wsdl:input name="exampleRequest">
<wsdlsoap:body use="literal"/>
</wsdl:input>
<wsdl:output name="exampleResponse">
<wsdlsoap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="IndexerService">
<wsdl:port name="IndexerServiceHttpPort" binding="tns:IndexerServiceHttpBinding">
<wsdlsoap:address location="http://localhost:8080/Indexer/services/IndexerService"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

在每种情况下,它都给了我以下错误:

Fatal error: SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://localhost:8080/Indexer/services/IndexerService?wsdl' : failed to load external entity 

任何人都可以帮助我,我怎么能在PHP代码中调用java编写的web服务。

我想提一下,我在php中使用php 5和wamp已经启用了soap扩展。

你可以使用的一个很好的工具是wsdl2php-interpreter ,它会创建四个类等等。

使用以下代码

 $Url = "http://localhost:8080/Indexer/services/IndexerService?wsdl";
    $params = array ("param1" =>"value","parm2"=>"value");

    $httpQuery = http_build_query ($params);
    $contextData = array (
                    "method" => "POST",
            "header" => "Connection: close\r\n".
                    "Content-Length: ".strlen($httpQuery)."\r\n",
                    "content"=> $httpQuery );               

    $context = stream_context_create (array ( "http" => $contextData )); 
    $result =  file_get_contents ($Url, false, $context); 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM