【发布时间】:2017-11-29 11:08:44
【问题描述】:
我有一个基于 symfony 的产品,我必须在其中公开一个 Soap API。我创建了 wsdl 文件,在输入用户名和密码后可从浏览器访问该文件以进行基本的 http 身份验证检查。
当我使用 SoapClient 调用该 API 并通过标头提供用户名和密码时,它能够访问 WSDL 并调用 SoapServer 端点。但是当 SoapServer() 调用该 wsdl 链接时,它会显示错误。
SoapClient():
$client = new \SoapClient('http://example.com/soaptest?wsdl', array(
"exceptions" => 0,
"trace" => 1,
'stream_context' => stream_context_create(array(
'http' => array(
'header' => "Authorization: Basic " . base64_encode("$username:$password")
),
)),
));
SoapServer():
ini_set("soap.wsdl_cache_enabled", "0");
$server = new SoapServer($wsdl); //Here SoapServer not able to access WSDl because of .htaccess
$server->setObject($this->get($class));
ob_start();
$server->handle();
$result = ob_get_clean();
return $result;
我也有一个 .htaccess 作为:
DirectoryIndex app.php
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{HTTP:Authorization} .
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^ %{ENV:BASE}/app.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
RedirectMatch 302 ^/$ /app.php/# RedirectTemp cannot be used instead
</IfModule>
</IfModule>
php_value date.timezone UTC
AuthType Basic
AuthName "Login"
AuthUserFile /etc/apache2/config/.htpasswd
Require expr %{REQUEST_URI} !~ m#^/(admin|login|soaptest)#
Require valid-user
#<RequireAll>
# Require ip 127.0.0.1
#</RequireAll>
所以这里控制转到 SoapServer() 调用,但是在尝试访问 SoapServer() 中的 wsdl 时,它显示:-
错误 {"500":"错误:SOAP-ERROR:解析 WSDL:无法从 'http://example.com/soaptest?wsdl' 加载:无法加载外部实体 "http://example.com/soaptest?wsdl""}
那么有没有办法在 SoapServer 调用中提供基本身份验证用户名和密码?因为我认为 SoapServer 在查找 WSDl 文件时无法访问该链接。 所有文件都在同一台服务器上。并且没有进行外部调用。
我也尝试过 SoapUI 并出现同样的错误。
.htaccess 中的最后 3 行在一定程度上有助于解决这个问题,但有没有更好的方法来解决这个问题?
.htaccess 对于我的项目是必需的,因此无法删除。
【问题讨论】:
-
提供基本身份验证用户+密码的最简单方法是在 url 中传递它;
user:pass@example.com/soaptest?wsdl -
对不起,我忘了提,但我也尝试过这种方式,但没有成功。
-
如果您的 WSDL 可以被您的浏览器加载,这是一件好事,但是您检查过您的 WSDL 是否正确吗?我使用 www.soapui.org 来检查 WSDL 如果您的 WSDL 是正确的,请尝试在没有登录名和密码的情况下简化您的测试,以检查 WSDL 是否可加载。之后,您可以添加登录名、密码、...
-
@vincent 如果我删除 htaccess,我的应用程序运行良好。这意味着 WSDL 很好,我在 SoapServer 调用中缺少一些选项,这些选项可以帮助我向 apache 提供登录详细信息。
标签: php web-services symfony soap wsdl