简体   繁体   English

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

[英]How can I send SOAP XML via Curl and PHP?

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. 我有一个ASP版本,可以在相同的URL和数据下正常工作。 I think it's just a PHP/Curl thing. 我认为这只是PHP / Curl的事情。

I currently have the following code (the CURLOPT_POSTFIELDS data 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! ASP版本工作正常! The code is: 代码是:

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>')

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);  

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); 

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. 我使用数据包嗅探器来确切地查看ASP客户端正在发送的内容,并注意到在初始身份验证请求之后它包含cookie。 So I enabled cookies in my cURL and it worked fine. 因此,我在cURL中启用了cookie,并且效果很好。

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

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 HTTPHEADER to text/xml instead of application/xml : 我最初具有用于XML发布的此功能,并且在将HTTPHEADER更改为text/xml而不是application/xml之前,soap无法正常工作:

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;
}

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

Your also using the wrong Content-type, application/xml is the correct type. 您还使用了错误的Content-type,application / xml是正确的类型。 But that shouldn't make any difference to the cURL request. 但这与cURL请求没有任何区别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM