问题描述
我已经创建了 php 文件.它包含有关访问 (wsdl) xml 文件和函数的详细信息.我需要用参数调用一个函数,但我不能调用一个函数.在该函数中,我需要打印参数的值.$ve = $soap_client->sayHello($b);--->>>在执行此行时出现错误无法连接到主机(它直接传递给catch).请任何人指出我在该代码中犯了什么错误.我传递了 url 中的值 "http://xxx.xxx.x.xx/testcode.php?method=sayHello&name=shankar"在我的 php 文件中看起来像这样 testcode.php:
I have created php file. It contains details about to access (wsdl)xml file and function. I need to call a function with paramaters but i can't able to call a function. In that function i need to print the value of parameters.$ve = $soap_client->sayHello($b);--->>>while executing this line am getting error couldnot connect to host(it directly passed to the catch).Anyone plz point me what mistake i did in that code. I passed the value like in the url "http://xxx.xxx.x.xx/testcode.php?method=sayHello&name=shankar"In my php file look like this testcode.php:
<?php
try
{
$soap_client=new SoapClient("HelloWorld.wsdl");
$a = $_GET["method"];
$b = $_GET["name"];
echo $b;
$ve = $soap_client->sayHello($b);
function sayHello($b)
{
echo 'continue';
echo $b;
}
}
catch(SoapFault $exception)
{
echo $exception->getMessage();
}
?>
推荐答案
尝试设置 location 参数如果 wsdl 架构中尚不存在:
try set location param if not yet exist in wsdl schema:
WSDL 模式:
$client = new SoapClient('HelloWorld.wsdl',
array(
'location' => 'http://xxx.xxx.x.xx/testcode.php',
));
非 WSDL 模式:
$client = new SoapClient(null, array(
'location' => "http://xxx.xxx.x.xx/testcode.php",
'uri' => "urn://etc",
'trace' => 1 ));
echo $client->sayHello('foo');
非 WSDL 模式的服务器示例:
server example for non WSDL mode:
class Service
{
public function sayHello($name)
{
return 'Hello ' . $name;
}
}
$server = new SoapServer(null, array('uri' => "urn://etc/"));
$server->setClass("Service");
$server->handle();
和 wsdl 类似..
and similar for wsdl..
您也可以使用 php-wsdl-creator 生成新的 wsdl数据
also you can use php-wsdl-creator for generated new wsdl data
添加:
WSDL 模式的完整示例 - 分步说明:
Full sample for WSDL mode - step by step:
1) 创建简单的示例服务:
1) Create the simple example service:
class Service
{
/**
* @param string $name
* @return string
*/
public function sayHello($name)
{
return 'Hello ' . $name;
}
}
$server = new SoapServer('http://example.com/wsdl-gen/'); //our auto-gen. schema
$server->setClass("Service");
$server->handle();
2) 对于新生成的模式,我使用 php-wsdl-creator (如果你已经有了,请转到下一步)
2) For new generated schema, I use php-wsdl-creator (go to the next step if you already have)
require_once 'php-wsdl/class.phpwsdl.php';
$namespace = 'http://example.com/';
$location = 'http://example.com/server.php';
$wsdl = PhpWsdl::CreateInstance($namespace, $location, './cache',
array(
'../soap-server.php' // all classes for generator
),
null,
null,
null,
false,
false);
//$wsdl->RunServer(); // bonus :) generated full client with documentation
$wsdl->Optimize = false;
$wsdl = $wsdl->CreateWsdl();
3) WSDL-Schema 可以是:
3) WSDL-Schema can be:
<wsdl:definitions xmlns:tns="http://example.com/" targetNamespace="http://example.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">
<wsdl:message name="sayHelloSoapIn">
<wsdl:part name="name" type="s:string" />
</wsdl:message>
<wsdl:message name="sayHelloSoapOut">
<wsdl:part name="return" type="s:string" />
</wsdl:message>
<wsdl:portType name="ServiceSoap">
<wsdl:operation name="sayHello">
<wsdl:input message="tns:sayHelloSoapIn" />
<wsdl:output message="tns:sayHelloSoapOut" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="ServiceSoap" type="tns:ServiceSoap">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc" />
<wsdl:operation name="sayHello">
<soap:operation soapAction="http://example.com/sayHello" />
<wsdl:input>
<soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://example.com/" parts="name" />
</wsdl:input>
<wsdl:output>
<soap:body use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://example.com/" parts="return" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="Service">
<wsdl:port name="ServiceSoap" binding="tns:ServiceSoap">
<soap:address location="http://example.com/soap-server.php" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
4) 为当前方案创建示例客户端:
4) Create example client for current scheme:
$client = new SoapClient(
'http://example.com/wsdl-gen/', //schema
array(
'location' => 'http://test/server.php' //SOAP server addr
//we provide this option if the location param is not valid in scheme or not exist
));
5) 仅此而已,使用我们的服务方法的示例:
5) That's all, example working with our service methods:
$client->sayHello('reg'); //string 'Hello reg' (length=9)
文档和工具:
- PhpWsdl - PHP 的 WSDL 生成器.(大约 2 年前我在工作中使用过这个)
- PhpWsdl - WSDL generator for PHP. (I used this in my job ~2 year ago)
- Documentation
这篇关于如何在php中使用soap调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!