how to connect to a webservice and send xml in php

0

I have a project in codeigneiter, I am trying to make a connection by curl with a web service of ringing, I am using the code that they gave me in php, which is fine, only that when adding the variable $ ch = curl_init () my view stops working or it marks me an error, I do not know if that library should be imported as I already included in php, otherwise, help plis I am new working on this

function GenerarXml(){


<?php
$xml="";
   //Armando el Request para la peticiòn SOAP
   $xml  = "<?xml version=\"1.0\"?>\n";
   $xml .= "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\" xmlns:tes=\"http://schemas.datacontract.org/2004/07/TES.V33.CFDI.Negocios\">\n";  
  $xml .= "<soapenv:Header/>\n";
  $xml .= "<soapenv:Body>\n";
  $xml .= "    <tem:GenerarCFDI>\n";
  $xml .= "    <tem:credenciales>\n";
  $xml .= "    <tes:Cuenta>TES030201001</tes:Cuenta>\n";
  $xml .= "    <tes:Password>Pruebas2017$</tes:Password>\n";
  $xml .= "    <tes:Usuario>TES030201001</tes:Usuario>\n";
  $xml .= "    </tem:credenciales>\n";
  $xml .= "    <tem:cfdi>\n";
  $xml .= "    <tes:ClaveCFDI>FAC</tes:ClaveCFDI>\n";
  $xml .= "    <tes:Conceptos>\n";
  $xml .= "    <tes:ConceptoR>\n";
  $xml .= "    <tes:Cantidad>1.7</tes:Cantidad>\n";
  $xml .= "    <tes:ClaveProdServ>01010101</tes:ClaveProdServ>\n";
  $xml .= "    <tes:ClaveUnidad>F52</tes:ClaveUnidad>\n";
  $xml .= "    <tes:Descripcion>ZAMAC</tes:Descripcion>\n";
  $xml .= "    <tes:Importe>17000.00</tes:Importe>\n";
  $xml .= "    <tes:Impuestos>\n";
  $xml .= "    <tes:Traslados>\n";
  $xml .= "    <tes:TrasladoConceptoR>\n";
  $xml .= "    <tes:Base>17000</tes:Base>\n";
  $xml .= "    <tes:Importe>2720.00</tes:Importe>\n";
  $xml .= "    <tes:Impuesto>002</tes:Impuesto>\n";
  $xml .= "    <tes:TasaOCuota>0.160000</tes:TasaOCuota>\n";
  $xml .= "    <tes:TipoFactor>Tasa</tes:TipoFactor>\n";
  $xml .= "    </tes:TrasladoConceptoR>\n";
  $xml .= "    </tes:Traslados>\n";
  $xml .= "    </tes:Impuestos>\n";
  $xml .= "    <tes:NoIdentificacion>00003</tes:NoIdentificacion>\n";
  $xml .= "    <tes:Unidad>TONELADA</tes:Unidad>\n";
  $xml .= "    <tes:ValorUnitario>10000.00</tes:ValorUnitario>\n";
  $xml .= "    </tes:ConceptoR>\n";
  $xml .= "    </tes:Conceptos>\n";
  $xml .= "    <tes:CondicionesDePago>CONDICIONES</tes:CondicionesDePago>\n";
  $xml .= "    <tes:Emisor>\n";
  $xml .= "    <tes:Nombre>HORACIO LLANOS</tes:Nombre>\n";
  $xml .= "    <tes:RegimenFiscal>601</tes:RegimenFiscal>\n";
  $xml .= "    </tes:Emisor>\n";
  $xml .= "    <tes:FormaPago>01</tes:FormaPago>\n";
  $xml .= "    <tes:LugarExpedicion>45079</tes:LugarExpedicion>\n";
  $xml .= "    <tes:MetodoPago>PUE</tes:MetodoPago>\n";
  $xml .= "    <tes:Moneda>MXN</tes:Moneda>\n";  
  $xml .= "    <tes:Receptor>\n";   
  $xml .= "    <tes:Nombre>RAFAEL ALEJANDRO HERNÁNDEZ PALACIOS</tes:Nombre>\n";
  $xml .= "    <tes:Rfc>TEST010203001</tes:Rfc>\n";
  $xml .= "    <tes:UsoCFDI>G01</tes:UsoCFDI>\n";
  $xml .= "    </tes:Receptor>\n";
  $xml .= "    <tes:Referencia>0001</tes:Referencia>\n";
  $xml .= "    <tes:SubTotal>17000.00</tes:SubTotal>\n";
  $xml .= "    <tes:Total>19720.00</tes:Total>\n";
  $xml .= "    </tem:cfdi>\n";
  $xml .= "    </tem:GenerarCFDI>\n";
  $xml .= "    </soapenv:Body>\n";
  $xml .= "    </soapenv:Envelope>\n";

  //creo un archivo soap_reequest.xml e introduzco la cadena_xml
    $new_xml = fopen ("./soap-request.xml", "w");
    fwrite($new_xml,$xml);
    fclose($new_xml);
    //Esta parde es el Header de la peticion SOAP y en ella se incluye el contenido de la pagina del servicio
  $header = array(
    "POST /CR33Test/ConexionRemota.svc HTTP/1.1",
    "Host: www.fel.mx",
    "Content-Type: text/xml; charset=UTF-8",
    "Content-Length: ".strlen($xml),
    "SOAPAction: \"http://tempuri.org/IConexionRemota/GenerarCFDI\""
  );

  $ch=curl_init();
  curl_setopt($ch, CURLOPT_URL, "https://www.fel.mx/CR33Test/ConexionRemota.svc?WSDL");
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
  curl_setopt($ch, CURLOPT_TIMEOUT,        30);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  curl_setopt($ch, CURLOPT_POST,           true );
  curl_setopt($ch, CURLOPT_POSTFIELDS,     $xml);
  curl_setopt($ch, CURLOPT_HTTPHEADER,     $header);

// Respuesta del webservice
            $response = curl_exec($ch; 
            curl_close($ch;
            print $response;
//se guarda del web service (Tipo de Respuesta, XML Timbrado y CBB)         
    $new_xml = fopen ("./soap-response.xml", "w");
    fwrite($new_xml, $response);
    fclose($new_xml);

?>
var xml =  <?= json_encode($xml); ?>

var respuesta =  <?= json_encode($response); ?>

var newxml =  <?= json_encode($new_xml); ?>

console.log(xml);
console.log(respuesta);
console.log(newxml);

}

This is how I get the error and the line

function GenerarXml(){


<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><GenerarCFDIResponse xmlns="http://tempuri.org/"><GenerarCFDIResult xmlns:a="http://schemas.datacontract.org/2004/07/TES.V33.CFDI.Negocios" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><a:CBB i:nil="true"/><a:CodigoConfirmacion i:nil="true"/><a:ErrorDetallado>Código de error: </a:ErrorDetallado><a:ErrorGeneral>Error de autenticación de usuario, 1 intentos realizados.</a:ErrorGeneral><a:FechaGenerada i:nil="true"/><a:FolioGenerado i:nil="true"/><a:OperacionExitosa>false</a:OperacionExitosa><a:PDF i:nil="true"/><a:XML i:nil="true"/></GenerarCFDIResult></GenerarCFDIResponse></s:Body></s:Envelope>var xml =  "<?xml version=\"1.0\"?>\n<soapenv:Envelope xmlns:soapenv=\"http:\/\/schemas.xmlsoap.org\/soap\/envelope\/\" xmlns:tem=\"http:\/\/tempuri.org\/\" xmlns:tes=\"http:\/\/schemas.datacontract.org\/2004\/07\/TES.V33.CFDI.Negocios\">\n<soapenv:Header\/>\n<soapenv:Body>\n\t   <tem:GenerarCFDI>\n    <tem:credenciales>\n    <tes:Cuenta>TES030201001<\/tes:Cuenta>\n    <tes:Password>Pruebas2017$<\/tes:Password>\n    <tes:Usuario>TES030201001<\/tes:Usuario>\n    <\/tem:credenciales>\n    <tem:cfdi>\n    <tes:ClaveCFDI>FAC<\/tes:ClaveCFDI>\n    <tes:Conceptos>\n\t   <tes:ConceptoR>\n    <tes:Cantidad>1.7<\/tes:Cantidad>\n    <tes:ClaveProdServ>01010101<\/tes:ClaveProdServ>\n    <tes:ClaveUnidad>F52<\/tes:ClaveUnidad>\n    <tes:Descripcion>ZAMAC<\/tes:Descripcion>\n    <tes:Importe>17000.00<\/tes:Importe>\n    <tes:Impuestos>\n    <tes:Traslados>\n    <tes:TrasladoConceptoR>\n    <tes:Base>17000<\/tes:Base>\n    <tes:Importe>2720.00<\/tes:Importe>\n    <tes:Impuesto>002<\/tes:Impuesto>\n    <tes:TasaOCuota>0.160000<\/tes:TasaOCuota>\n    <tes:TipoFactor>Tasa<\/tes:TipoFactor>\n    <\/tes:TrasladoConceptoR>\n    <\/tes:Traslados>\n    <\/tes:Impuestos>\n    <tes:NoIdentificacion>00003<\/tes:NoIdentificacion>\n    <tes:Unidad>TONELADA<\/tes:Unidad>\n    <tes:ValorUnitario>10000.00<\/tes:ValorUnitario>\n    <\/tes:ConceptoR>\n    <\/tes:Conceptos>\n    <tes:CondicionesDePago>CONDICIONES<\/tes:CondicionesDePago>\n\t   <tes:Emisor>\n    <tes:Nombre>HORACIO LLANOS<\/tes:Nombre>\n    <tes:RegimenFiscal>601<\/tes:RegimenFiscal>\n    <\/tes:Emisor>\n    <tes:FormaPago>01<\/tes:FormaPago>\n    <tes:LugarExpedicion>45079<\/tes:LugarExpedicion>\n    <tes:MetodoPago>PUE<\/tes:MetodoPago>\n    <tes:Moneda>MXN<\/tes:Moneda>\n    <tes:Receptor>\n    <tes:Nombre>RAFAEL ALEJANDRO HERN\u00c1NDEZ PALACIOS<\/tes:Nombre>\n    <tes:Rfc>TEST010203001<\/tes:Rfc>\n    <tes:UsoCFDI>G01<\/tes:UsoCFDI>\n    <\/tes:Receptor>\n    <tes:Referencia>0001<\/tes:Referencia>\n    <tes:SubTotal>17000.00<\/tes:SubTotal>\n    <tes:Total>19720.00<\/tes:Total>\n    <\/tem:cfdi>\n    <\/tem:GenerarCFDI>\n    <\/soapenv:Body>\n    <\/soapenv:Envelope>\n"
var respuesta =  "<s:Envelope xmlns:s=\"http:\/\/schemas.xmlsoap.org\/soap\/envelope\/\"><s:Body><GenerarCFDIResponse xmlns=\"http:\/\/tempuri.org\/\"><GenerarCFDIResult xmlns:a=\"http:\/\/schemas.datacontract.org\/2004\/07\/TES.V33.CFDI.Negocios\" xmlns:i=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"><a:CBB i:nil=\"true\"\/><a:CodigoConfirmacion i:nil=\"true\"\/><a:ErrorDetallado>C\u00f3digo de error: <\/a:ErrorDetallado><a:ErrorGeneral>Error de autenticaci\u00f3n de usuario, 1 intentos realizados.<\/a:ErrorGeneral><a:FechaGenerada i:nil=\"true\"\/><a:FolioGenerado i:nil=\"true\"\/><a:OperacionExitosa>false<\/a:OperacionExitosa><a:PDF i:nil=\"true\"\/><a:XML i:nil=\"true\"\/><\/GenerarCFDIResult><\/GenerarCFDIResponse><\/s:Body><\/s:Envelope>"
var newxml =  
console.log(xml);
console.log(respuesta);
console.log(newxml);

}

So I'm left with the try catch, but I still mark that error that I mention, attach error image:

function GenerarXml(){


<?php
$xml="";
   //Armando el Request para la peticiòn SOAP
   $xml  = "<?xml version=\"1.0\"?>\n";
   $xml .= "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:tem=\"http://tempuri.org/\" xmlns:tes=\"http://schemas.datacontract.org/2004/07/TES.V33.CFDI.Negocios\">\n";  
  $xml .= "<soapenv:Header/>\n";
  $xml .= "<soapenv:Body>\n";
  $xml .= "    <tem:GenerarCFDI>\n";
  $xml .= "    <tem:credenciales>\n";
  $xml .= "    <tes:Cuenta>TES030201001</tes:Cuenta>\n";
  $xml .= "    <tes:Password>Pruebas2017$</tes:Password>\n";
  $xml .= "    <tes:Usuario>TES030201001</tes:Usuario>\n";
  $xml .= "    </tem:credenciales>\n";
  $xml .= "    <tem:cfdi>\n";
  $xml .= "    <tes:ClaveCFDI>FAC</tes:ClaveCFDI>\n";
  $xml .= "    <tes:Conceptos>\n";
  $xml .= "    <tes:ConceptoR>\n";
  $xml .= "    <tes:Cantidad>1.7</tes:Cantidad>\n";
  $xml .= "    <tes:ClaveProdServ>01010101</tes:ClaveProdServ>\n";
  $xml .= "    <tes:ClaveUnidad>F52</tes:ClaveUnidad>\n";
  $xml .= "    <tes:Descripcion>ZAMAC</tes:Descripcion>\n";
  $xml .= "    <tes:Importe>17000.00</tes:Importe>\n";
  $xml .= "    <tes:Impuestos>\n";
  $xml .= "    <tes:Traslados>\n";
  $xml .= "    <tes:TrasladoConceptoR>\n";
  $xml .= "    <tes:Base>17000</tes:Base>\n";
  $xml .= "    <tes:Importe>2720.00</tes:Importe>\n";
  $xml .= "    <tes:Impuesto>002</tes:Impuesto>\n";
  $xml .= "    <tes:TasaOCuota>0.160000</tes:TasaOCuota>\n";
  $xml .= "    <tes:TipoFactor>Tasa</tes:TipoFactor>\n";
  $xml .= "    </tes:TrasladoConceptoR>\n";
  $xml .= "    </tes:Traslados>\n";
  $xml .= "    </tes:Impuestos>\n";
  $xml .= "    <tes:NoIdentificacion>00003</tes:NoIdentificacion>\n";
  $xml .= "    <tes:Unidad>TONELADA</tes:Unidad>\n";
  $xml .= "    <tes:ValorUnitario>10000.00</tes:ValorUnitario>\n";
  $xml .= "    </tes:ConceptoR>\n";
  $xml .= "    </tes:Conceptos>\n";
  $xml .= "    <tes:CondicionesDePago>CONDICIONES</tes:CondicionesDePago>\n";
  $xml .= "    <tes:Emisor>\n";
  $xml .= "    <tes:Nombre>HORACIO LLANOS</tes:Nombre>\n";
  $xml .= "    <tes:RegimenFiscal>601</tes:RegimenFiscal>\n";
  $xml .= "    </tes:Emisor>\n";
  $xml .= "    <tes:FormaPago>01</tes:FormaPago>\n";
  $xml .= "    <tes:LugarExpedicion>45079</tes:LugarExpedicion>\n";
  $xml .= "    <tes:MetodoPago>PUE</tes:MetodoPago>\n";
  $xml .= "    <tes:Moneda>MXN</tes:Moneda>\n";  
  $xml .= "    <tes:Receptor>\n";   
  $xml .= "    <tes:Nombre>RAFAEL ALEJANDRO HERNÁNDEZ PALACIOS</tes:Nombre>\n";
  $xml .= "    <tes:Rfc>TEST010203001</tes:Rfc>\n";
  $xml .= "    <tes:UsoCFDI>G01</tes:UsoCFDI>\n";
  $xml .= "    </tes:Receptor>\n";
  $xml .= "    <tes:Referencia>0001</tes:Referencia>\n";
  $xml .= "    <tes:SubTotal>17000.00</tes:SubTotal>\n";
  $xml .= "    <tes:Total>19720.00</tes:Total>\n";
  $xml .= "    </tem:cfdi>\n";
  $xml .= "    </tem:GenerarCFDI>\n";
  $xml .= "    </soapenv:Body>\n";
  $xml .= "    </soapenv:Envelope>\n";

  //creo un archivo soap_reequest.xml e introduzco la cadena_xml
    $first_xml = fopen ("./soap-request.xml", "w");
    fwrite($first_xml,$xml);
    fclose($first_xml);
    //Esta parde es el Header de la peticion SOAP y en ella se incluye el contenido de la pagina del servicio
  $header = array(
    "POST /CR33Test/ConexionRemota.svc HTTP/1.1",
    "Host: www.fel.mx",
    "Content-Type: text/xml; charset=UTF-8",
    "Content-Length: ".strlen($xml),
    "SOAPAction: \"http://tempuri.org/IConexionRemota/GenerarCFDI\""
  );

  try{
    $ch = curl_init();
 curl_setopt($ch, CURLOPT_URL, "https://www.fel.mx/CR33Test/ConexionRemota.svc?WSDL");
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
  curl_setopt($ch, CURLOPT_TIMEOUT,        30);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true );
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  curl_setopt($ch, CURLOPT_POST,           true );
  curl_setopt($ch, CURLOPT_POSTFIELDS,     $xml);
  curl_setopt($ch, CURLOPT_HTTPHEADER,     $header);

}catch(Exception $e){
    print_r($e);
}



// Respuesta del webservice
            $response = curl_exec($ch); 
            curl_close($ch);
            print $response;
//se guarda del web service (Tipo de Respuesta, XML Timbrado y CBB)         
    $sec_xml = fopen ("./soap-response.xml", "w");
    fwrite($sec_xml, $response);
    fclose($sec_xml);

?>
var xml =  <?= json_encode($xml); ?>

var respuesta =  <?= json_encode($response); ?>

var newxml =  <?= json_encode($new_xml); ?>

console.log(xml);
console.log(respuesta);
console.log(newxml);

}

This is the error in sources: and this shows me in sources in the developer console:

  

function GenerarXml () {

     

Error code:   User authentication error,   1 attempts made.falsevar   xml="\ n \ n \ n \ n \ t   \ n \ n
  TES030201001 < / tees: Account > \ n
  Testing2017 $ < / tes: Password > \ n
  TES030201001 < / te: User > \ n < / tem: credentials > \ n
  \ n FAC < / te: ClaveCFDI > \ n
  \ n \ t \ n
  1.7 < / tees: Quantity > \ n
  01010101 < / te: ClaveProdServ > \ n
  F52 < / tes: ClaveUnity > \ n
  ZAMAC < / tes: Description > \ n
  17000.00 < / tees: Amount > \ n \ n
  \ n \ n
  17000 < / te: Base > \ n 2720.00 < / tees: Amount > \ n   002 < / tes: Tax > \ n
  0.160000 < / teas: RateOCuota > \ n
  Rate < / tes: TypeFactor > \ n
  < / te: TransferConceptR > \ n < / te: Transfers > \ n
  < / te: Taxes > \ n
  00003 < / tees: NoIdentification > \ n
  TON < / tes: Unity > \ n
  10000.00 < / tes: ValorUnitario > \ n
  < / tees: Concept > \ n < / tes: Concepts > \ n
  CONDITIONS < / tes: Payment Terms > \ n \ t
  \ n HORACIO LLANOS < / tes: Name > \ n
  601 < / te: RegimenFiscal > \ n < / tes: Issuer > \ n
  01 < / tes: FormaPago > \ n
  45079 < / tes: PlaceExpedition > \ n
  PUE < / te: Method of payment > \ n
  MXN < / tes: Currency > \ n \ n
  RAFAEL ALEJANDRO HERN \ u00c1NDEZ PALACIOS < / tes: Name > \ n
  TEST010203001 < / tes: Rfc > \ n
  G01 < / tes: UsoCFDI > \ n < / tes: Receptor > \ n
  0001 < / tes: Reference > \ n
  17000.00 < / te: SubTotal > \ n
  19720.00 < / te: Total > \ n < / tem: cfdi > \ n
  < / tem: GenerateCFDI > \ n < / soapenv: Body > \ n < / soapenv: Envelope > \ n "       var answer="Error code:   < / a: ErrorDefault > Authentication Error from   user, 1 attempts made. < / a: ErrorGeneral > false < / a: Successfull Operation < / GenerateCFDIResult < / GenerateCFDIResponse < / s: Body < / s: Envelope >       var newxml =
      console.log (xml);       console.log (answer);       console.log (newxml);

}
    
asked by debek mendoza 27.02.2018 в 19:31
source

1 answer

0

You can actually use SoapClient, but for the particular case, this is complicated due to how the wsdl is structured, you should send all the parameters in an array. Place the call curl_init on an exception block, and tell us what is your result, to make curl calls, you must also include the php-curl library, I recommend using the Soap-UI software, in which you can see what are the methods that exposes the service and the parameters that are required.

try{
$ch = curl_init();
.......
}catch(Exception $e){
print_r($e);
}

To enable the library, in your wamp installation folder for example, you should look for the file bin / php / php56 / php.ini, as in the bin / apache / apache2 / php.ini file and decompress the line

extension=php_curl.dll

once the files are edited, restart your wamp services

Example of calling with SoapClient, you must include the php-soap library

$client = new SoapClient("https://www.fel.mx/CR33Test/ConexionRemota.svc?WSDL");
$params = array('codigo' => $parametro);
$result = $client->miMetodo($params);
    
answered by 27.02.2018 / 20:12
source