Last active
May 17, 2024 12:13
-
-
Save EdEichman/cee00064fbd3964d5cc56974023030fc to your computer and use it in GitHub Desktop.
La documentación de Correos Express para seguimientoEnvios no tiene documentación para PHP. Este Gist explica como llamar a seguimientoEnvios en PHP.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//Correos Express SeguimientoEnvio | |
//necesitaras el documento "DC_SP_apiRestSeguimientoEnviosFechas_03.pdf" - contacta con integraciones de correso express | |
$id_shipment = {CAMPO numShip DE cex_history}; | |
$url = "https://www.correosexpress.com/wpsc/apiRestSeguimientoEnvios/rest/seguimientoEnvios"; | |
$input_xml = "<?xml version='1.0' encoding='UTF-8'?> | |
<SeguimientoEnviosRequest xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' | |
xsi:noNamespaceSchemaLocation='SeguimientoEnviosRequest.xsd'> | |
<Solicitante>{------ Código de cliente - el mismo q utilizas para el modulo -----}</Solicitante> | |
<Dato>$id_shipment</Dato> | |
</SeguimientoEnviosRequest>"; | |
$headers = array( | |
'Content-Type: application/xml', | |
'Cache-Control: no-cache', | |
'Accept: */*', | |
'Accept-Encoding: gzip, deflate, br', | |
'Connection: keep-alive', | |
'Authorization: Basic '. base64_encode("{--- Usuario - el mismo q utilizas para el modulo -----}:{--- Contraseña - el mismo q utilizas para el modulo -----}") | |
); | |
$ch = curl_init(); | |
$result = curl_setopt($ch, CURLOPT_URL, $url); | |
if ($result) $result = curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); | |
if ($result) $result = curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); | |
if ($result) $result = curl_setopt($ch, CURLOPT_POST, 1); | |
if ($result) $result = curl_setopt($ch, CURLOPT_POSTFIELDS, $input_xml); | |
if ($result) { | |
$data = curl_exec($ch); | |
} | |
curl_close($ch); | |
//extract the XML | |
$xml_start = strpos($data, '<?xml'); | |
$xml = substr($data, $xml_start); | |
$xml = simplexml_load_string($xml, "SimpleXMLElement", LIBXML_NOCDATA); | |
$json = json_encode($xml); | |
$result_array = json_decode($json,TRUE); | |
//Extract the delivery time | |
foreach ($result_array['EstadoEnvios'] as $key=>$value) { | |
if (12 /*ENTREGADO*/ == $value['CodEstado']) { | |
$year = substr($value['FechaEstado'], 4); | |
$month = substr($value['FechaEstado'], 2,2); | |
$day = substr($value['FechaEstado'], 0,2); | |
$hour = substr($value['HoraEstado'], 0,2); | |
$minute = substr($value['HoraEstado'], 2,2); | |
$second = substr($value['HoraEstado'], 4,2); | |
$date_time = date('Y-m-d H:i:s',mktime($hour, $minute, $second, $month, $day, $year)); | |
die($date_time); | |
} | |
} |
Por si no acertáis con el REST dejo uno con SOAP
https://github.com/mhonty/Seguimiento_Correos_Express.git
Buenas tardes, Ed ¿Tiene algún ejemplo de la generación de la etiqueta/envio con recogida con la API de Correos Express? Gracias !
Buenas @djboro1988 , este es el código para lo que pides, pasándole un JSON con los datos del envío:
`function grabarEnvio($json) {
//Preproducción
$url = 'https://www.test.cexpr.es/wspsc/apiRestGrabacionEnviok8s/json/grabacionEnvio';
//Producción
$url = 'https://www.cexpr.es/wspsc/apiRestGrabacionEnviok8s/json/grabacionEnvio';
global $user;
global $pass;
$headers = array(
'Content-Type: application/json',
'Cache-Control: no-cache',
'Accept: */*',
'Connection: keep-alive',
'Authorization: Basic '. base64_encode("{$user}:{$pass}")
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $json);
$response = curl_exec($curl);
curl_close($curl);
echo $response;
}`
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hola djboro1988,
No, lo siento. Pero cuando necesito algo así, normalmente mira como lo hacen en el módulo de PrestaShop, y imita su código.
Si eres cliente, el departamento de desarrollo de Correos Express deberían ayudarte.
Buena suerte!
Ed