How can I send SOAP XML via Curl and PHP?

前端 未结 6 422
暖寄归人
暖寄归人 2020-12-01 02:15

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.

相关标签:
6条回答
  • 2020-12-01 02:28

    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:

    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;
    }
    
    0 讨论(0)
  • 2020-12-01 02:31

    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.

    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.

    $cookiePath = tempnam('/tmp', 'cookie');
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiePath);
    
    0 讨论(0)
  • 2020-12-01 02:35

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

    0 讨论(0)
  • 2020-12-01 02:37

    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); 
    
    0 讨论(0)
  • 2020-12-01 02:49

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

    0 讨论(0)
  • 2020-12-01 02:53

    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);  
    
    0 讨论(0)
提交回复
热议问题