带有 BasicAuth 的 PHP SoapClient [英] PHP SoapClient with BasicAuth
问题描述
我有一个 PHP 脚本试图连接到 WSDL.我需要允许自签名并提供基本的身份验证详细信息.使用 SOAP UI,当我连接到 WSDL 时,系统会提示我输入用户名/密码.我开始工作了.
I have a PHP script trying to connect to a WSDL. I need to allow self signed AND give basic auth details. Using SOAP UI, when I connect to the WSDL I am prompted for username / password. I got this working.
我还发现每个请求也需要基本身份验证(因此在请求屏幕上,我必须选择身份验证,然后是基本身份,输入与我在提示中使用的凭据相同的凭据).
I also found out that each request also requires basic auth (so on the request screen, I have to select Auth, then basic, enter same credentials as I used on the prompt).
如何在 PHP 中执行此身份验证正如我所说,我可以连接,没问题,如果我尝试发出请求,我似乎会终止服务或超时
How to I do this auth in PHP As I said, I can connect, not a problem, I seem to kill the service or timeout if I try to make a request
<?php
$context = stream_context_create(array(
'ssl' => array(
'verify_peer' => false,
'verify_peer_name' => false,
'allow_self_signed' => true
)
));
$data = array(
'columnA' => 'dataA',
'columnB' => 'dataB',
'columnC' => 'dataC');
$url = 'https://111.111.111.111:1234/dir/file';
$login = 'username';
$pwd = 'password';
$client = new soapClient(null, array(
'location' => $url,
'uri' => '',
'login' => $login,
'password' => $pwd,
'stream_context' => $context
));
echo "\n\r---connected---\n\r";
$result = $client ->requestName($data);
print_r($result);
?>
我的输出是---连接---
My output is ---connected---
然后它似乎挂了.我试过用 try catch 包裹它,但结果相同.
Then it seems to hang. I have tried wrapping it round a try catch and I had the same result.
有什么建议吗??
推荐答案
来自 手动 soapclient 支持 http 基本身份验证.
From the Manual soapclient support the http basic auth.
对于 HTTP 身份验证,登录名和密码选项可用于提供凭据.用于通过代理建立 HTTP 连接服务器,选项 proxy_host、proxy_port、proxy_login 和proxy_password 也可用.对于 HTTPS 客户端证书身份验证使用 local_cert 和密码选项.一个可以在身份验证选项中提供身份验证.这身份验证方法可以是 SOAP_AUTHENTICATION_BASIC(默认)或 SOAP_AUTHENTICATION_DIGEST.
For HTTP authentication, the login and password options can be used to supply credentials. For making an HTTP connection through a proxy server, the options proxy_host, proxy_port, proxy_login and proxy_password are also available. For HTTPS client certificate authentication use local_cert and passphrase options. An authentication may be supplied in the authentication option. The authentication method may be either SOAP_AUTHENTICATION_BASIC (default) or SOAP_AUTHENTICATION_DIGEST.
$wsdl = "http://example/services/Service?wsdl";
$option = array(
"trace"=>1,
"login"=>"admin",
"password"=>"admin",
);
$client = new SoapClient($wsdl,$option);
但是当我启动soapclient时,它会抛出这个错误
But when I initiate the soapclient, it will throw this error
例外:未经授权
我也尝试将身份验证放在网址中,例如
I also have tried to put the auth in the url, like
$wsdl = "http://admin:admin@example/services/Service?wsdl";
但它也不起作用.
最后我通过在选项中添加 authentication
解决了这个问题.手册上说authentication
默认值是基本的auth,但只有我明确设置它才能工作.
Finally I solved it by add authentication
to the option. The manual says the authentication
default value is the basic auth, but only when I explicitly set it, it can work.
$option = array(
"trace"=>1,
"login"=>"admin",
"password"=>"admin",
"authentication"=>SOAP_AUTHENTICATION_BASIC
);
这篇关于带有 BasicAuth 的 PHP SoapClient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!