ETLASW001.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. // Obtener conexión de la base de datos
  3. require_once 'conexionBD.php';
  4. // Llamar librería de soap
  5. require_once("soap/nusoap.php");
  6. // require_once("procesar_formularios.php");
  7. mysqli_query($conexion, "SET NAMES utf8");
  8. // Set timezone
  9. date_default_timezone_set('America/Monterrey');
  10. ini_set('upload_max_filesize', '10M');
  11. ini_set('post_max_size', '10M');
  12. ini_set('max_input_time', 300);
  13. ini_set('max_execution_time', 300);
  14. // Consultar las URL existentes en los productos
  15. $queryConsulta = "SELECT ID, EASYVENTURL FROM ITT_ALP_GLOBAL WHERE EASYVENTURL != ''";
  16. $resultado = mysqli_query($conexion, $queryConsulta) or die("Algo ha ido mal en la consulta a la base de datos");
  17. $arrURLEasyVent = array();
  18. while ($columna = mysqli_fetch_assoc($resultado)) {
  19. $arrURLEasyVent[] = array(
  20. "ID" => trim($columna['ID']),
  21. "URL" => trim($columna['EASYVENTURL']),
  22. );
  23. }
  24. // Los servidores se obtuvieron de la página http://proxydb.net/?country=MX ** Todos son de México
  25. $arrProxies = array(); // Lista de Proxies para realizar las peticiones
  26. $arrProxies[0]['proxyAndPuerto'] = '201.159.22.164:46809';
  27. $arrProxies[0]['tipoProxy'] = CURLPROXY_SOCKS4;
  28. $arrProxies[1]['proxyAndPuerto'] = '201.144.20.231:5678';
  29. $arrProxies[1]['tipoProxy'] = CURLPROXY_SOCKS4;
  30. $arrProxies[2]['proxyAndPuerto'] = '45.167.253.129:999';
  31. $arrProxies[2]['tipoProxy'] = CURLPROXY_HTTPS;
  32. // Iteración de las URL de easyvent
  33. foreach ($arrURLEasyVent as $dataEasyVent) {
  34. $exito = true;
  35. $totalProxies = count($arrProxies) - 1;
  36. // Iteración de las proxies guardadas
  37. for ($i = 0; $i <= $totalProxies; $i++) {
  38. $ch = curl_init();
  39. curl_setopt($ch, CURLOPT_URL, $dataEasyVent['URL']); // Dirección URL a capturar
  40. curl_setopt($ch, CURLOPT_PROXY, $arrProxies[$i]['proxyAndPuerto']); // El proxy HTTP para enviar peticiones a través de tunel.
  41. curl_setopt($ch, CURLOPT_PROXYTYPE, $arrProxies[$i]['tipoProxy']); // Puede ser CURLPROXY_HTTP (por defecto), CURLPROXY_SOCKS4, CURLPROXY_SOCKS5, CURLPROXY_SOCKS4A o CURLPROXY_SOCKS5_HOSTNAME
  42. curl_setopt($ch, CURLOPT_TIMEOUT, 60); // Número máximo de segundos permitido para ejectuar funciones cURL
  43. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60); // Número de segundos a esperar cuando se está intentado conectar. Use 0 para esperar indefinidamente
  44. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // "true" para seguir cualquier encabezado "Location: " que el servidor envíe como parte del encabezado HTTP (observe la recursividad, PHP seguirá tantos header "Location: " como se envíen, a no ser que la opción CURLOPT_MAXREDIRS esté establecida)
  45. //curl_setopt($ch, CURLOPT_MAXREDIRS, 10); // Número máximo de redirecciones HTTP a seguir. Use esta opción con CURLOPT_FOLLOWLOCATION
  46. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // "true" para devolver el resultado de la transferencia como string del valor de curl_exec() en lugar de mostrarlo directamente
  47. $data = curl_exec($ch); // Ejecuta la sesión cURL que se le pasa como parámetro
  48. $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); // Obtener información sobre una transferencia específica -> El último código de respuesta. A partir de PHP 5.5.0 y cURL 7.10.8, este es un alias heredado de CURLINFO_RESPONSE_CODE
  49. curl_close($ch); // Cierra la sesión cURL
  50. // Comprobación si se obtuvo los datos del proxy
  51. if (($httpcode >= 200) && ($httpcode < 300)) {
  52. $contenido = $data;
  53. // Identificar si se obtuvo la tabla
  54. if ( !empty($contenido) ) {
  55. // Contenido de la tabla
  56. $tablaHTML = "<div class='section section-grey'>";
  57. $seccionGris = strstr($contenido, "section section-grey");
  58. $removeFooter = strstr($seccionGris, "section cookie-warning", true);
  59. $tablaHTML .= ("<div class=\" " . $removeFooter . "\" >");
  60. $tablaHTML .= "</div>";
  61. // Cambiar las comillas dobles "" por comillas simples ''
  62. $tablaHTML = str_replace("'", '"', $tablaHTML);
  63. // Variable de fecha de modificación
  64. $fechaHoy = date("Y-m-d");
  65. $FUPD = date("Y-m-d H:i:s");
  66. // Consultar si existe en la tabla de datos de easyvent
  67. $queryConsulta = " SELECT IDPROD, FECHAMOD FROM ITT_ASW_EASYVENT WHERE IDPROD = '" . $dataEasyVent['ID'] . "' ";
  68. $resultado = mysqli_query($conexion, $queryConsulta) or die("Algo ha ido mal en la consulta a la base de datos");
  69. $arrEasy = array();
  70. while ($columna = mysqli_fetch_assoc($resultado)) {
  71. $arrEasy = array(
  72. "IDPROD" => trim($columna['IDPROD']),
  73. "FECHAMOD" => trim($columna['FECHAMOD'])
  74. );
  75. }
  76. $dateTimeEasyVent = date("Y-m-d", strtotime($arrEasy['FECHAMOD'] ));
  77. // Verificar si existe para modificar o ingresar el nuevo dato
  78. if ( empty($arrEasy) ) {
  79. $queryInsert = "INSERT INTO ITT_ASW_EASYVENT (IDPROD, DATATABLE, FECHAMOD) VALUES ('" . trim($dataEasyVent['ID']) . "', '" . trim($tablaHTML) . "', '" . trim($FUPD) . "');";
  80. if (!mysqli_query($conexion, $queryInsert)) {
  81. $exito = false;
  82. $errorDescripcion = 'SQL INSERT MEX|' . $dataEasyVent['ID'];
  83. $respuesta = conectarWebService($errorDescripcion, "sendEtlError");
  84. }else{
  85. goto next;
  86. }
  87. }else if( !empty($arrEasy) && $fechaHoy > $dateTimeEasyVent) {
  88. $queryUpdate = "UPDATE ITT_ASW_EASYVENT SET DATATABLE = '" . trim($tablaHTML) . "', FECHAMOD = '" . trim($FUPD) . "' WHERE ( IDPROD = '" . trim($dataEasyVent['ID']) . "' );";
  89. if (!mysqli_query($conexion, $queryUpdate)) {
  90. $exito = false;
  91. $errorDescripcion = 'SQL UPDATE MEX|' . $dataEasyVent['ID'];
  92. $respuesta = conectarWebService($errorDescripcion, "sendEtlError");
  93. }else{
  94. goto next;
  95. }
  96. }
  97. } else {
  98. $exito = false;
  99. $errorDescripcion = 'EMPTY TABLE MEX|' . $dataEasyVent['ID'];
  100. $respuesta = conectarWebService($errorDescripcion, "sendEtlError");
  101. }
  102. }else{
  103. $exito = false;
  104. $errorDescripcion = 'ERROR PROXY MEX|' . $dataEasyVent['ID'];
  105. $respuesta = conectarWebService($errorDescripcion, "sendEtlError");
  106. }
  107. }
  108. next:
  109. }
  110. fin_extraerTabla:
  111. if($exito == false){
  112. $respuesta = conectarWebService($errorDescripcion, "sendEtlError");
  113. }
  114. function conectarWebService($dataPlain, $function) {
  115. // URL del webservice
  116. $wsdl = "https://smart.solerpalau.mx/QA/WS001_ASW.php?wsdl";
  117. // Verificamos si esta disponible
  118. $headers = @get_headers($wsdl);
  119. if (preg_match('/^HTTP\/\d\.\d\s+(200|301|302)/', $headers[0])) {
  120. // Instanciando un nuevo objeto cliente para consumir el webservice
  121. $client = new nusoap_client($wsdl, 'wsdl');
  122. $param = array('datos' => $dataPlain);
  123. // Llamando al método y pasándole el array con los parámetros
  124. $resultado = $client->call($function, $param);
  125. return $resultado;
  126. }
  127. }