Amazon sellercentral login through PHP and Curl

628 views Asked by At

I am working on web scraping after login to Amazon seller central account but while executing my code, it always return me an error

"There was a problem Enter your email or mobile phone number"

I have checked the cookie file and cookies are saving in it. Set headers as per requirement. Can anyone please let me know where i am lacking.

I have seen many posts which suggest to go with the PhantomJS + CasperJS, but i am not having much time to learn these 2 js. So please help me in resolving this problem.

    $email    = $username;

    // initial login page which redirects to correct sign in page, sets some cookies
    $URL = "https://sellercentral.amazon.com/gp/homepage.html?";

    $ch  = curl_init();

    curl_setopt($ch, CURLOPT_URL, $URL);
    curl_setopt($ch, CURLOPT_COOKIEJAR, '/var/www/html/amazoncookie.txt');
    curl_setopt($ch, CURLOPT_COOKIEFILE, '/var/www/html/amazoncookie.txt');
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 10);
    //curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLOPT_STDERR,  fopen('php://stdout', 'w'));
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

    $page = curl_exec($ch);

    // try to find the actual login form
    if (!preg_match('/<form .*?<\/form>/is', $page, $form)) {
        die('Failed to find log in form!');
    }

    $form = $form[0];

    // find the action of the login form
    if (!preg_match('/action=(?:\'|")?([^\s\'">]+)/i', $form, $action)) {
        die('Failed to find login form url');
    }

    $URL2 = $action[1]; // this is our new post url

    // find all hidden fields which we need to send with our login, this includes security tokens
    $count = preg_match_all('/<input type="hidden"\s*name="([^"]*)"\s*value="([^"]*)"/i', $form, $hiddenFields);

    $postFields = array();

    // turn the hidden fields into an array
    for ($i = 0; $i < $count; ++$i) {
        $postFields[$hiddenFields[1][$i]] = $hiddenFields[2][$i];
    }

    // add our login values
    $postFields['username'] = $email;
    $postFields['password'] = $password;

    $post = '';

    // convert to string, this won't work as an array, form will not accept multipart/form-data, only application/x-www-form-urlencoded
    foreach($postFields as $key => $value) {
        $post .= $key . '=' . urlencode($value) . '&';
    }

    $post = substr($post, 0, -1);

    // set additional curl options using our previous options
    curl_setopt($ch, CURLOPT_URL, $URL2);
    curl_setopt($ch, CURLOPT_REFERER, $URL);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);

    $page = curl_exec($ch); // make request
    print_r($page);
    return $ch;

Reference: "https://github.com/mindevolution/amazonSellerCentralLogin" Need to scrap product reviews and buyer email ids for the seller feedback after login.

0

There are 0 answers