如何通过 Curl 和 PHP 发送 SOAP XML?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3006977/
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 08:24:25  来源:igfitidea点击:

How can I send SOAP XML via Curl and PHP?

phpxmlsoapcurl

提问by dan richardson

This has been bugging me for days; I'm trying to send a SOAP post via Curl but I just keep getting a "couldn't connect to host" error, but, I really can't see how.

这已经困扰我好几天了;我正在尝试通过 Curl 发送 SOAP 帖子,但我一直收到“无法连接到主机”的错误消息,但是,我真的不知道怎么做。

I have an ASP version which works fine with the same URL and data. I think it's just a PHP/Curl thing.

我有一个 ASP 版本,它可以很好地处理相同的 URL 和数据。我认为这只是一个 PHP/Curl 的事情。

I currently have the following code (the CURLOPT_POSTFIELDSdata is a valid SOAP envelope string):

我目前有以下代码(CURLOPT_POSTFIELDS数据是有效的 SOAP 信封字符串):

$soap_do = curl_init();
curl_setopt($soap_do, CURLOPT_URL,            "https://xxx.yyy.com:517/zzz.asmx" );
curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($soap_do, CURLOPT_TIMEOUT,        10);
curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true );
curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($soap_do, CURLOPT_POST,           true );            
curl_setopt($soap_do, CURLOPT_POSTFIELDS,     '<soap:Envelope>...</soap:Envelope>'); 
curl_setopt($soap_do, CURLOPT_HTTPHEADER,     array('Content-Type: text/xml; charset=utf-8', 'Content-Length: '.strlen('<soap:Envelope>...</soap:Envelope>') ));

if(curl_exec($soap_do) === false)
{                
    $err = 'Curl error: ' . curl_error($soap_do);
    curl_close($soap_do);
    return $err;
}
else
{
    curl_close($soap_do); 
    return 'Operation completed without any errors';
}

So any ideas why it just errors all the time?

那么有什么想法为什么它总是出错?

The ASP version works fine! The code is:

ASP 版本工作正常!代码是:

Set xmlhttp = server.Createobject("MSXML2.ServerXMLHTTP")
xmlhttp.Open "POST","https://xxx.yyy.com:517/zzz.asmx"
xmlhttp.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
xmlhttp.Send('<soap:Envelope>...</soap:Envelope>')

回答by Syn

Thanx a lot buddy, your code has worked for me.

谢谢很多朋友,你的代码对我有用。

Here is the code:

这是代码:

$soap_do = curl_init(); 
curl_setopt($soap_do, CURLOPT_URL,            $url );   
curl_setopt($soap_do, CURLOPT_CONNECTTIMEOUT, 10); 
curl_setopt($soap_do, CURLOPT_TIMEOUT,        10); 
curl_setopt($soap_do, CURLOPT_RETURNTRANSFER, true );
curl_setopt($soap_do, CURLOPT_SSL_VERIFYPEER, false);  
curl_setopt($soap_do, CURLOPT_SSL_VERIFYHOST, false); 
curl_setopt($soap_do, CURLOPT_POST,           true ); 
curl_setopt($soap_do, CURLOPT_POSTFIELDS,    $post_string); 
curl_setopt($soap_do, CURLOPT_HTTPHEADER,     array('Content-Type: text/xml; charset=utf-8', 'Content-Length: '.strlen($post_string) )); 
curl_setopt($soap_do, CURLOPT_USERPWD, $user . ":" . $password);

$result = curl_exec($soap_do);
$err = curl_error($soap_do);  

回答by Dom Hodgson

I had to use

我不得不使用

$headers = array(             
    "Content-type: text/xml;charset=\"utf-8\"", 
    "Accept: text/xml", 
    "Cache-Control: no-cache", 
    "Pragma: no-cache", 
    "SOAPAction: \"run\"", 
    "Content-length: ".strlen($xml),
); 

and

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 

回答by Tobias Fünke

For those finding this from Google, I ran into a similar problem, trying to interact with a .NET SOAP server from PHP, when the ASP method worked fine.

对于那些从 Google 发现这一点的人,我遇到了类似的问题,当 ASP 方法工作正常时,我试图从 PHP 与 .NET SOAP 服务器进行交互。

I used a packet sniffer to see what the ASP client was sending, exactly, and noticed it included cookies after the initial authentication request. So I enabled cookies in my cURL and it worked fine.

我使用数据包嗅探器查看 ASP 客户端发送的确切内容,并注意到它在初始身份验证请求后包含 cookie。所以我在我的 cURL 中启用了 cookie,它运行良好。

$cookiePath = tempnam('/tmp', 'cookie');
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiePath);

回答by dev7apps

This may be old, but this work for me. I originally had this function for an XML post and soap would not work until I change the HTTPHEADERto text/xmlinstead of application/xml:

这可能很旧,但这对我有用。我最初为 XML 帖子提供了此功能,并且在我将HTTPHEADERto更改为text/xml而不是之前,soap 将无法工作application/xml

function doXMLCurl($url,$postXML){
    $CURL = curl_init();

    curl_setopt($CURL, CURLOPT_URL, $url); 
    curl_setopt($CURL, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
    curl_setopt($CURL, CURLOPT_POST, 1); 
    curl_setopt($CURL, CURLOPT_POSTFIELDS, $postXML); 
    curl_setopt($CURL, CURLOPT_HEADER, false); 
    curl_setopt($CURL, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($CURL, CURLOPT_HTTPHEADER, array('Accept: text/xml','Content-Type: text/xml'));
    curl_setopt($CURL, CURLOPT_RETURNTRANSFER, true);
    $xmlResponse = curl_exec($CURL); 

    return $xmlResponse;
}

回答by Alistair

Try and set the port number using CURLOPT_PORT as perhaps it's not liking it as part of the URL?

尝试使用 CURLOPT_PORT 设置端口号,因为它可能不喜欢它作为 URL 的一部分?

回答by Adam

Your also using the wrong Content-type, application/xml is the correct type. But that shouldn't make any difference to the cURL request.

您还使用了错误的 Content-type,application/xml 是正确的类型。但这不会对 cURL 请求产生任何影响。