如何在 PHP 中使用 SSL 证书发送 SOAP 请求?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11676392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:51:14  来源:igfitidea点击:

how to send SOAP request with SSL certificate in PHP?

phpweb-servicessoapsslssl-certificate

提问by BruneX

I'm trying to send a SOAP - PHP request with a DER certificate (that means the certificate don't have a privateKey) but no success.

我正在尝试发送带有 DER 证书的 SOAP - PHP 请求(这意味着该证书没有私钥)但没有成功。

$local_cert = FULL_PATH_TO_MY_CERT;

   $client = new SoapClient($wsdl, array(
                'local_cert' => $local_cert,
                'trace' => 1,
                'exceptions' => 1,
                'soap_version' => SOAP_1_1,
                'encoding' => 'ISO-8859-1',
                'compression' => (SOAP_COMPRESSION_ACCEPT | SOAP_COMPRESSION_GZIP),
                'location' => 'https://webserviceurl:port/ws/servlet/ws'
            ));

Only I recieve this errors:

只有我收到此错误:

Warning (2): SoapClient::SoapClient() [soapclient.soapclient]: Unable to set private key file `PATHTOMYLOCALCERT' [APP\Vendor\WebServices\MyWS.php, line 206]

警告 (2): SoapClient::SoapClient() [soapclient.soapclient]: 无法设置私钥文件`PATHTOMYLOCALCERT' [APP\Vendor\WebServices\MyWS.php, line 206]

Warning (2): SoapClient::SoapClient() [soapclient.soapclient]: failed to create an SSL handle [APP\Vendor\WebServices\MyWS.php, line 206]

警告 (2):SoapClient::SoapClient() [soapclient.soapclient]:未能创建 SSL 句柄 [APP\Vendor\WebServices\MyWS.php,第 206 行]

Warning (2): SoapClient::SoapClient() [soapclient.soapclient]: Failed to enable crypto [APP\Vendor\WebServices\MyWS.php, line 206]

警告 (2):SoapClient::SoapClient() [soapclient.soapclient]:无法启用加密 [APP\Vendor\WebServices\MyWS.php,第 206 行]

Warning (2): SoapClient::SoapClient(https://webserviceurl:port/ws/servlet/ws?wsdl) [soapclient.soapclient]: failed to open stream: operation failed [APP\Vendor\WebServices\MyWS.php, line 206]

警告(2):SoapClient::SoapClient(https://webserviceurl:port/ws/servlet/ws?wsdl) [soapclient.soapclient]: 无法打开流:操作失败 [APP\Vendor\WebServices\MyWS.php,第206行]

Warning (2): SoapClient::SoapClient() [soapclient.soapclient]: I/O warning : failed to load external entity "https://webserviceurl:port/ws/servlet/ws?wsdl" [APP\Vendor\WebServices\MyWS.php, line 206]

警告 (2):SoapClient::SoapClient() [soapclient.soapclient]:I/O 警告:无法加载外部实体“https://webserviceurl:port/ws/servlet/ws?wsdl”[APP\Vendor\WebServices \MyWS.php,第 206 行]

but I've found a little trick (in php.net) using the function file_get_contents($local_cert); the errors are gone.

但我发现了一个小技巧(在 php.net 中)使用函数 file_get_contents($local_cert); 错误消失了。

But a new error come from.

但是一个新的错误由此而来。

Result : string(773) "Error reading prefix:Action.Execute"

结果:字符串(773)“错误读取前缀:Action.Execute”

What I mean is... this error above... is comming from the WebService? because it cannot authenticate with my request?

我的意思是......上面的这个错误......来自WebService?因为它无法通过我的请求进行身份验证?

Thanks everybody. (appreciate your answers)

谢谢大家。(感谢您的回答)

回答by Vins

I'm using SSL certificate in my soap call.

我在我的肥皂呼叫中使用 SSL 证书。

In My case I'm giving absolute path on my server for wsdland for local_certI've already defined those in my class. Please note that I'm using my certificate in .pemformat.

在我的情况下,我在我的服务器上给出了绝对路径wsdl,因为local_cert我已经在我的班级中定义了这些路径。请注意,我正在使用我的证书.pem格式。

public $local_cert = "/var/www/.../webroot/cert.pem";
public $wsdl = "/var/www/.../webroot/my_wsdl.wsdl";

$this->client = new SoapClient($this->wsdl, array(
        "trace"         => 1, 
        "exceptions"    => true, 
        "local_cert"    => $this->local_cert, 
        "uri"           => "urn:xmethods-delayed-quotes",
        "style"         => SOAP_RPC,
        "use"           => SOAP_ENCODED,
        "soap_version"  => SOAP_1_2 ,
        "location"      => $this->location
    )
);

In my certificate there are 2 parts. Certificate and RSA Private Key.

在我的证书中有两部分。证书和 RSA 私钥。

(1)-----BEGIN CERTIFICATE-----
MIIFjzCC....
....
-----END CERTIFICATE-----
(2)-----BEGIN RSA PRIVATE KEY-----
MIIEpAI....
....
ww==
-----END RSA PRIVATE KEY----

And most important you should use httpslink for making a soap call. This is working fine for me.

最重要的是,您应该使用https链接进行肥皂呼叫。这对我来说很好用。

Hope this will help you.

希望这会帮助你。