-
-
Save silenoth/7b380e342686181f8a1cd6e9a2dfe8de to your computer and use it in GitHub Desktop.
<?php | |
/* | |
* Sencilla funcion para cotizar los precios de los paquetes enviados a traves de Starken (antes Turbus Cargo). | |
* No requiere el modulo allow_url_fopen. | |
*/ | |
include 'simple_html_dom.php'; | |
/** | |
* @param int $origen Código ciudad origen. | |
* @param int $destino Código ciudad destino. | |
* @param float $peso Peso (Kg.) | |
* @param array $dimensiones Dimensiones (largo, alto, ancho en cms). | |
* @return mixed Retorna false si encuentra un error | |
**/ | |
function starkenPrecios($origen,$destino,$peso,$dimensiones) | |
{ | |
$url = 'http://www.starken.cl/cotiza-tu-envio?'; | |
$cookie = dirname(__FILE__) . '\cookie.txt'; | |
$useragent = 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'; | |
$log = fopen('log.txt', 'w'); | |
/* | |
* Obtener CSRF y resolver captcha | |
*/ | |
$curl = curl_init($url); | |
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie); | |
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_USERAGENT, $useragent); | |
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10); | |
$str = curl_exec($curl); | |
curl_close($curl); | |
//var_dump(implode('', file($cookie))); | |
$html = str_get_html($str); | |
$val = $html->find('div[class=form-item form-item-verificacion form-type-textfield form-group]', 0); | |
for ($i=0; $i < count($val->find('img')); $i++) | |
{ | |
$img[$i] = str_replace('/sites/all/themes/starken/img/', '', $val->find('img', $i)->src); | |
$img[$i] = str_replace('.png', '', $img[$i]); | |
} | |
foreach ($html->find('input[name=form_build_id]') as $hash) | |
{ | |
$validate[1] = $hash->value; | |
} | |
$num1 = $img[0]; | |
$signo = $img[1]; | |
$num2 = $img[2]; | |
switch ($signo) { | |
case 11: | |
$validate[0] = $num1 + $num2; | |
break; | |
case 12: | |
$validate[0] = $num1 - $num2; | |
break; | |
case 13; | |
$validate[0] = $num1 * $num2; | |
break; | |
} | |
$post = http_build_query( | |
array( | |
'ciudadOrigen' => $origen, | |
'ciudadDestino' => $destino, | |
'alto' => $dimensiones[0], | |
'ancho' => $dimensiones[1], | |
'largo'=> $dimensiones[2], | |
'kilos' => $peso, | |
'verificacion' => trim($validate[0]), | |
'op' => 'Cotizar', | |
'form_build_id' => trim($validate[1]), | |
'form_id' => 'kiteknology_tarifa_xl_formulario' | |
) | |
); | |
$curl = curl_init(); | |
curl_setopt($curl, CURLOPT_URL, $url.$post); | |
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($curl, CURLOPT_COOKIE, $cookie); | |
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie); | |
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true); | |
curl_setopt($curl, CURLOPT_REFERER, $url); | |
curl_setopt($curl, CURLOPT_STDERR, $log); | |
curl_setopt($curl, CURLOPT_USERAGENT, $useragent); | |
curl_setopt($curl, CURLOPT_POSTFIELDS, array($url,$post)); | |
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10); | |
$str = curl_exec($curl); | |
curl_close($curl); | |
//verifica el contenido de la cookie. | |
//var_dump(implode('', file($cookie))); | |
$html = str_get_html($str); | |
if (is_numeric($origen) && is_numeric($destino) && is_numeric($peso) && is_numeric($dimensiones[0]) && is_numeric($dimensiones[1]) && is_numeric($dimensiones[2])) { | |
$theads = $html->find('table',0)->find('th'); | |
foreach ($theads as $thead) | |
{ | |
$datos[] = $thead->plaintext; | |
} | |
$tdatas = $html->find('table',0)->find('td'); | |
foreach ($tdatas as $tdata) | |
{ | |
$datos[] = $tdata->plaintext; | |
} | |
if (in_array('Normal', $datos) || in_array('Express', $datos)) | |
{ | |
$salida['Normal'] = [ | |
$datos[4] => [ | |
trim(str_replace('$','',str_replace('.','',$datos[9]))), | |
$datos[10] | |
], | |
$datos[5] => [ | |
trim(str_replace('$','',str_replace('.','',$datos[11]))), | |
$datos[12] | |
] | |
]; | |
$salida['Express'] = [ | |
$datos[7] => [ | |
trim(str_replace('$','',str_replace('.','',$datos[13]))), | |
$datos[14] | |
], | |
$datos[8] => [ | |
trim(str_replace('$','',str_replace('.','',$datos[15]))), | |
$datos[16] | |
] | |
]; | |
return $salida; | |
} else { | |
return false; | |
} | |
} | |
} | |
//Envio desde Antofagasta hasta Santiago. Paquete de 1kg x 10cms x 10cms x 10cms | |
echo "<pre>"; | |
var_dump(starkenPrecios(84,1,1,array(10,10,10))); |
master lo resolvi hicieron un cambio aqui form-item form-item-verificacion form-type-textfield form-group
osea la linea 40 debe ser modificada al dia de hoy $val = $html->find('div[class=form-type-textfield form-item-verificacion form-item form-group]', 0); saludos
ahora funciona perfecto.- gracias master.-
Perdón por la demora, gracias a ti por esa acotación, saludos
Lo volvieron a cambiar.
Línea 40 debe ser esta ahora:
$val = $html->find('div[class=form-item form-item-verificacion form-type-textfield form-group]', 0);
Gracias amigo
algo cambiaron en starken que ya no funciona en la misma linea el error
parece ser cookie.txt ni idea :(
error:
Fatal error: Uncaught Error: Call to a member function find() on boolean in /home/...../public_html/envios/starken/starken.php:40 Stack trace: #0 /home/...../public_html/envios/starken/starken.php(162): starkenPrecios(19, 1, 1, Array) #1 {main} thrown in /home/...../public_html/envios/starken/starken.php on line 40
esta funcionando?
master la etapa de captcha la sacaste de tal forma que se puede obtener la imagen para mantener el captcha original del form de starken? podrias orientarme un poco. porque me falla con este error:
Fatal error: Call to a member function find() on a non-object in ....../starken.php on line 42
y al parecer ese error es del captcha