<?php

// $to = "https://vpfe-hab.dian.gov.co/WcfDianCustomerServices.svc";
// $action = "http://wcf.dian.colombia/IWcfDianCustomerServices/SendBillSync";

public function sendCurl($endpoint, $action, $soap){

	$curl = curl_init();
	$headers = [
        'Accept: application/xml',
        'Content-type: application/soap+xml',
        'Content-length: '.strlen($soap),
        'SOAPAction: '.$action 
    ];

	curl_setopt($curl, CURLOPT_URL, $endpoint);
	curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 180);
	curl_setopt($curl, CURLOPT_TIMEOUT, 180);
	curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
	curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
	curl_setopt($curl, CURLOPT_POST, true);
	curl_setopt($curl, CURLOPT_POSTFIELDS, $soap);
	curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

	$res 	= curl_exec($curl);
	$rq 	= curl_getinfo($curl);
	$err	= curl_error($curl);

	curl_close($curl);


	$response = new DOMDocument;
    $response->loadXML($res);
    print_r($response);//Para ver estructura de respuesta
    $code = $response->getElementsByTagName('StatusCode');
    $errors = $response->getElementsByTagName('ErrorMessage')[0];
    echo .'-----------*****---'.$code[0]->nodeValue.'----*********---------------';
    echo "<br>";
    if (!is_null($errors)) {
        echo $errors->nodeValue; 
        echo "<br>";
    }
	return $res;

}