有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java从PHP调用Web服务

我一直在尝试从下到上调用用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服务吗

我想提到的是,我正在使用PHP5,并且我的php中已经启用了wamp soap扩展


共 (2) 个答案

  1. # 2 楼答案

    使用以下代码

     $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);