为什么在我的 PHP WSDL (Zf2) 中没有返回成功
Why success is not being returned in my PHP WSDL (Zf2)
我使用这两个资源作为创建 WSDL 端点服务器的启动平台。
https://odan.github.io/2017/11/20/implementing-a-soap-api-with-php-7.html
https://www.youtube.com/watch?v=e_7jDqN2A-Y&t=799s
通过结合这两者,我能够想出一个可行的混合系统。我现在正在尝试解决的问题是从 api.php/endpoint 服务器获得响应。
在 odan git 示例中,它确实有效。但是一旦我更改了需要对象的代码。我开始收到错误。
PHP Notice: Trying to get property of non-object
这是服务器代码的一部分。
class wenoError
{
public $response = "Sucess";
public static function authenticate($header_params)
{
if($header_params->username == 'WEX' && $header_params->password == 'WEX1') return true;
else throw new SOAPFault('Wrong user/pass combination', 601);
}
/**
* @param string $payload
* @return string $delivery
*/
public function receivePayload($payload)
{
$xml = base64_decode($payload);
$fileName = 'message-'.rand().'.xml';
$file = file_put_contents('messages/'.$fileName, $xml);
$xml2json = simplexml_load_string($xml);
$jsonOut = json_encode($xml2json);
$arrayJson = json_decode($jsonOut, TRUE);
//$seeArray = print_r($arrayJson, true);
//file_put_contents('messages/converted-'.$fileName.'.json', $arrayJson['Header']['MessageID']);
$response = "Success";
return $response;
}
}
$serverUrl = "https://localhost/WenoErrors/api.php";
$options = [
'uri' => $serverUrl,
];
$server = new Zend\Soap\Server('wsdl', $options);
if (isset($_GET['wsdl'])) {
$soapAutoDiscover = new \Zend\Soap\AutoDiscover(new \Zend\Soap\Wsdl\ComplexTypeStrategy\ArrayOfTypeSequence());
$soapAutoDiscover->setBindingStyle(array('style' => 'document'));
$soapAutoDiscover->setOperationBodyStyle(array('use' => 'literal'));
$soapAutoDiscover->setClass('wenoError');
$soapAutoDiscover->setUri($serverUrl);
header("Content-Type: text/xml");
echo $soapAutoDiscover->generate()->toXml();
} else {
$soap = new \Zend\Soap\Server($serverUrl . '?wsdl');
$soap->setObject(new \Zend\Soap\Server\DocumentLiteralWrapper(new wenoError()));
$soap->handle();
}
我不明白的是 $response being a non-object 的错误信息。根据 PHP 手册 https://www.php.net/manual/en/language.oop5.properties.php
属性 已正确设置在 class 的顶部,声明了 属性 并设置了一个值。
出了什么问题?
更新:
添加客户端代码。
$client = new Zend\Soap\Client('https://localhost/WenoErrors/api.php?wsdl');
$delivery = $client->call('receivePayload',[['payload' => $message]]);
倾销客户收益:
C:\eRxGateway\www\apa\WenoErrors\clientapi.php:55:
object(client)[3]
public 'delivery' => null
更新:
最终对我有用的是这个改变。
第一个变化:
$server = new Zend\Soap\Server('wsdl', $options);
$server = new Zend\Soap\Server(null, $options);
你的代码对我来说似乎工作正常。不过,我得到的结果与您的结果不同,如下所示:
$client = new Zend\Soap\Client('http://localhost/test/api.php?wsdl');
$message = ' -> Hello World';
$delivery = $client->call('receivePayload',[['payload' => $message]]);
var_dump($delivery);
object(stdClass)#4 (1) {
["receivePayloadResult"]=>
string(7) "Success"
}
Step 1
请尝试从您的 /tmp 目录中删除所有“/tmp/wsdl-****”文件。您似乎在 windows,因此可能不是 /tmp,而是 C:\Windows\Temp。您可以通过进入 php.ini 文件并查找以下指令轻松找到哪个目录。
soap.wsdl_cache_dir="/tmp"
Step 2
此外,出于开发和测试目的,始终将以下 ini 指令放在客户端 php 文件的开头,在您的情况下为 clientapi.php 文件.
ini_set("soap.wsdl_cache_enabled", 0);
您不需要将此指令放在服务器 (api.php) 文件的开头,但如果以上仍然不适合您,您可以这样做。
我使用这两个资源作为创建 WSDL 端点服务器的启动平台。
https://odan.github.io/2017/11/20/implementing-a-soap-api-with-php-7.html
https://www.youtube.com/watch?v=e_7jDqN2A-Y&t=799s
通过结合这两者,我能够想出一个可行的混合系统。我现在正在尝试解决的问题是从 api.php/endpoint 服务器获得响应。
在 odan git 示例中,它确实有效。但是一旦我更改了需要对象的代码。我开始收到错误。
PHP Notice: Trying to get property of non-object
这是服务器代码的一部分。
class wenoError
{
public $response = "Sucess";
public static function authenticate($header_params)
{
if($header_params->username == 'WEX' && $header_params->password == 'WEX1') return true;
else throw new SOAPFault('Wrong user/pass combination', 601);
}
/**
* @param string $payload
* @return string $delivery
*/
public function receivePayload($payload)
{
$xml = base64_decode($payload);
$fileName = 'message-'.rand().'.xml';
$file = file_put_contents('messages/'.$fileName, $xml);
$xml2json = simplexml_load_string($xml);
$jsonOut = json_encode($xml2json);
$arrayJson = json_decode($jsonOut, TRUE);
//$seeArray = print_r($arrayJson, true);
//file_put_contents('messages/converted-'.$fileName.'.json', $arrayJson['Header']['MessageID']);
$response = "Success";
return $response;
}
}
$serverUrl = "https://localhost/WenoErrors/api.php";
$options = [
'uri' => $serverUrl,
];
$server = new Zend\Soap\Server('wsdl', $options);
if (isset($_GET['wsdl'])) {
$soapAutoDiscover = new \Zend\Soap\AutoDiscover(new \Zend\Soap\Wsdl\ComplexTypeStrategy\ArrayOfTypeSequence());
$soapAutoDiscover->setBindingStyle(array('style' => 'document'));
$soapAutoDiscover->setOperationBodyStyle(array('use' => 'literal'));
$soapAutoDiscover->setClass('wenoError');
$soapAutoDiscover->setUri($serverUrl);
header("Content-Type: text/xml");
echo $soapAutoDiscover->generate()->toXml();
} else {
$soap = new \Zend\Soap\Server($serverUrl . '?wsdl');
$soap->setObject(new \Zend\Soap\Server\DocumentLiteralWrapper(new wenoError()));
$soap->handle();
}
我不明白的是 $response being a non-object 的错误信息。根据 PHP 手册 https://www.php.net/manual/en/language.oop5.properties.php
属性 已正确设置在 class 的顶部,声明了 属性 并设置了一个值。
出了什么问题?
更新:
添加客户端代码。
$client = new Zend\Soap\Client('https://localhost/WenoErrors/api.php?wsdl');
$delivery = $client->call('receivePayload',[['payload' => $message]]);
倾销客户收益:
C:\eRxGateway\www\apa\WenoErrors\clientapi.php:55:
object(client)[3]
public 'delivery' => null
更新:
最终对我有用的是这个改变。
第一个变化:
$server = new Zend\Soap\Server('wsdl', $options);
$server = new Zend\Soap\Server(null, $options);
你的代码对我来说似乎工作正常。不过,我得到的结果与您的结果不同,如下所示:
$client = new Zend\Soap\Client('http://localhost/test/api.php?wsdl');
$message = ' -> Hello World';
$delivery = $client->call('receivePayload',[['payload' => $message]]);
var_dump($delivery);
object(stdClass)#4 (1) {
["receivePayloadResult"]=>
string(7) "Success"
}
Step 1
请尝试从您的 /tmp 目录中删除所有“/tmp/wsdl-****”文件。您似乎在 windows,因此可能不是 /tmp,而是 C:\Windows\Temp。您可以通过进入 php.ini 文件并查找以下指令轻松找到哪个目录。
soap.wsdl_cache_dir="/tmp"
Step 2
此外,出于开发和测试目的,始终将以下 ini 指令放在客户端 php 文件的开头,在您的情况下为 clientapi.php 文件.
ini_set("soap.wsdl_cache_enabled", 0);
您不需要将此指令放在服务器 (api.php) 文件的开头,但如果以上仍然不适合您,您可以这样做。