Ayuda Bot Amazon [PHP]

josd@

Lanero Reconocido
14 Mar 2008
5,240
Buenas senores.

Resulta que tengo que hacer un bot para amazon que en pocas palabras haga lo siguiente:

1. Ingresa a una lista de productos disponibles buscando por medio de un ASIN, ejemplo http://www.amazon.de/gp/offer-listing/B004XI22OM/ref=dp_olp_new?ie=UTF8
2. Haga click en agregar a carrito
3. Luego haga click en modificar numero de productos
4. Insertar 999 en el espacio de cantidad. En este punto Amazon devolvera un mensaje de alerta con la cantidad de stock para el producto solicitado

El primer y segundo punto ya lo tengo resuelto por medio de php cURL, el problema es que al enviar el formulario para agregar al carrito la pagina no hace nada, se queda en blanco y no devuelve respuesta por el lado de amazon.

El codigo basicamente lo que hace es una serie de pasos para instanciar los formularios de la pagina solicitada por ASIN en un objeto que pueda manipular despues, pero ahi me quede.

Sera que alguien tiene idea de lo que podria hacer?

aca dejo parte del codigo, me disculpan que este tan desordenado

Archivo principal

$asins = array();
array_push($asins, 'B004XI22OM');

$curl = new Curl();

foreach ($asins as $asin) {
$parametros_form = array();
$html = file_get_html(' ');

$forms = array();
foreach ($html->find('form') as $element) {
if (strpos($element->action, '/gp/item-dispatch') !== false) {
$action = explode('/', $element->action);
unset($action[4]);
$action = implode('/', $action);
$form = new Form("http://www.amazon.de" . $action);
array_push($forms, $form);
}
}

foreach ($forms as $form) {
foreach ($html->find('input') as $element) {
if ($element->name == "session-id")
$form->setSessionId($element->value);


if ($element->name == "qid")
$form->setQid($element->value);


if ($element->name == "sr")
$form->setSr($element->value);


if ($element->name == "signInToHUC")
$form->setSignInToHUC($element->value);


if (strrpos($element->name, "metric-asin") !== false)
$form->setMetric_asin($element->value, $asin);


if (strrpos($element->name, "registryItemID") !== false) {
$value = explode('.', $element->name);
$form->setRegistryItemID($element->value, $value[1]);
}

if (strrpos($element->name, "registryID") !== false) {
$value = explode('.', $element->name);
$form->setRegistryID($element->value, $value[1]);
}

if ($element->name == "itemCount")
$form->setItemCount($element->value);


if (strrpos($element->name, "offeringID") !== false) {
$value = explode('.', $element->name);
$form->setOfferingID($element->value, $value[1]);
}

if ($element->name == "isAddon") {
$form->setIsAddon($element->value);
break;
}
}
}
}
//Now let's make the form POST methods

$http_headers = array(
'Host: www.google.com',
'User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0.2) Gecko/20100101 Firefox/6.0.2',
'Accept: */*',
'Accept-Language: en-us,en;q=0.5',
'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7',
'Connection: keep-alive',
'Keep-Alive: 600'
);

$post = null;
$post .= $forms[0]->getSessionId()->getName() . '=' . urlencode($forms[0]->getSessionId()->getValue());
$post .= '&' . $forms[0]->getQid()->getName() . '=' . urlencode($forms[0]->getQid()->getValue());
$post .= '&' . $forms[0]->getSr()->getName() . '=' . urlencode($forms[0]->getSr()->getValue());
$post .= '&' . $forms[0]->getSignInToHUC()->getName() . '=' . urlencode($forms[0]->getSignInToHUC()->getValue());
$post .= '&' . $forms[0]->getMetric_asin()->getName() . '=' . urlencode($forms[0]->getMetric_asin()->getValue());
$post .= '&' . $forms[0]->getRegistryItemID()->getName() . '=' . urlencode($forms[0]->getRegistryItemID()->getValue());
$post .= '&' . $forms[0]->getRegistryID()->getName() . '=' . urlencode($forms[0]->getRegistryID()->getValue());
$post .= '&' . $forms[0]->getItemCount()->getName() . '=' . urlencode($forms[0]->getItemCount()->getValue());
$post .= '&' . $forms[0]->getOfferingID()->getName() . '=' . urlencode($forms[0]->getOfferingID()->getValue());
$post .= '&' . $forms[0]->getIsAddon()->getName() . '=' . urlencode($forms[0]->getIsAddon()->getValue());


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $forms[0]->getAction()->getValue());
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
curl_setopt($ch, CURLOPT_HTTPHEADER, $http_headers);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_COOKIEFILE, dirname(__FILE__) . '\cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEJAR, dirname(__FILE__) . '\cookie.txt');


try {
$result = curl_exec($ch);
var_dump($result);
} catch (Excaeption $e) {
echo $e->getMessage();
}

$error = curl_error($ch);

//Format: http://www.amazon.de/gp/offer-listing/ASIN/ref=dp_olp_new?ie=UTF8
form.php
require_once 'form_input.php';

class Form {

private $action;
private $sesion_id;
private $qid;
private $sr;
private $signInToHUC;
private $metric_asin;
private $registryItemID;
private $registryID;
private $itemCount;
private $offeringID;
private $isAddon;

public function __construct($action) {
$this->setAction($action);
}

public function getAction() {
return $this->action;
}

public function getQid() {
return $this->qid;
}

public function getSr() {
return $this->sr;
}

public function getSignInToHUC() {
return $this->signInToHUC;
}

public function getMetric_asin() {
return $this->metric_asin;
}

public function getRegistryItemID() {
return $this->registryItemID;
}

public function getRegistryID() {
return $this->registryID;
}

public function getItemCount() {
return $this->itemCount;
}

public function getOfferingID() {
return $this->offeringID;
}

public function getIsAddon() {
return $this->isAddon;
}

public function getSessionId() {
return $this->sesion_id;
}

public function setSessionId($sessionid) {
$this->sesion_id = new FormInput('session-id', null, $sessionid);
}

public function setAction($action) {
$this->action = new FormInput('action', null, $action);
}

public function setQid($qid = null) {
$this->qid = new FormInput('qid', null, $qid);
}

public function setSr($sr = null) {
$this->sr = new FormInput('sr', null, $sr);
}

public function setSignInToHUC($signInToHUC = null) {
$this->signInToHUC = new FormInput('signInToHUC', 'signInToHUC', $signInToHUC);
}

public function setMetric_asin($metric_asin = null, $asin) {
$this->metric_asin = new FormInput('metric-asin.' . $asin, null, $metric_asin);
}

public function setRegistryItemID($registryItemID = null, $id) {
$this->registryItemID = new FormInput('registryItemID'.$id, null, $registryItemID);
}

public function setRegistryID($registryID = null, $id) {
$this->registryID = new FormInput('registryID'.$id, null, $registryID);
}

public function setItemCount($itemCount = null) {
$this->itemCount = new FormInput('itemCount', null, $itemCount);
}

public function setOfferingID($offeringID = null, $id) {
$this->offeringID = new FormInput('offeringID'.$id, null, $offeringID);
}

public function setIsAddon($isAddon = null) {
$this->isAddon = new FormInput('isAddon',null,$isAddon);
}

}
archivo form_input.php
class FormInput {

private $name;
private $id;
private $value;

public function __construct($name = null, $id = null, $value = null) {
if (!is_null($name))
$this->setName($name);
if (!is_null($id))
$this->setId($id);
if (!is_null($value))
$this->setValue($value);
}

public function getName() {
return $this->name;
}

public function getId() {
return $this->id;
}

public function getValue() {
return $this->value;
}

public function setName($name) {
$this->name = $name;
}

public function setId($id) {
$this->id = $id;
}

public function setValue($value) {
$this->value = $value;
}

}
 
Alguno vez hice una prueba para agregar un producto en una pagina de compras, lo hice usando esta libreria: http://casperjs.org/, facilita muchas cosas y se puede hacer un seguimiento de la respuesta a cada evento a traves de pantallazos, de pronto le pueda servir.
 

Los últimos temas