Easy Build Amazon ASIN Grabber with PHP and Curl

Surely you already familiar with words AMAZON ASIN ? yup, Amazon ASIN is a code for the item on Amazon’s website. For the players amazon affiliate, Collecting ASIN code takes a lot of time to get a lot of product on the Amazon website. Many software products are sold to take ASIN code automatically ( Or usually netter called it ASIN GRABBER ) and quickly on the Amazon site, but with constrained costs, we can not afford to buy the software.

In my first article, will discuss about how to build the Software to grab the Amazon ASIN using PHP and CURL.

Why use PHP CURL to build the amazon ASIN grabber?

According to php.net, curl is “ PHP supports libcurl, a library created by Daniel Stenberg, that allows you to connect and communicate to many different types of servers with many different types of protocols. libcurl currently supports the http, https, ftp, gopher, telnet, dict, file, and ldap protocols. libcurl also supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading (this can also be done with PHP’s ftp extension), HTTP form based upload, proxies, cookies, and user+password authentication. These functions have been added in PHP 4.0.2. ”. By using curl, we can take the element-element of a site and process it, If you need to learn more about PHP CURL, visit php.net.

Ok, let’s try to use php curl to take elements from the amazon website. with the following script.

Create CURL function as curl_url

function curl_url($url,$ref="")
 {
   if(function_exists("curl_init"))
   {
     $ch_init = curl_init();
     $user_agent = "Mozilla/4.0 (compatible; MSIE 5.01; "."Windows NT 5.0)";
     $ch_init = curl_init();
     curl_setopt($ch_init, CURLOPT_USERAGENT, $user_agent);
     curl_setopt( $ch_init, CURLOPT_HTTPGET, 1 );
     curl_setopt( $ch_init, CURLOPT_RETURNTRANSFER, 1 );
     curl_setopt( $ch_init, CURLOPT_FOLLOWLOCATION , 1 );
     curl_setopt( $ch_init, CURLOPT_FOLLOWLOCATION , 1 );
     curl_setopt( $ch_init, CURLOPT_URL, $url );
     curl_setopt( $ch_init, CURLOPT_REFERER, $ref );
     curl_setopt ($ch_init, CURLOPT_COOKIEJAR, 'cookie.txt');
     $html = curl_exec($ch_init);
     curl_close($ch_init);
   }
  else
   {
     $hfile = fopen($url,"r");
     if($hfile)
     {
       while(!feof($hfile))
       {
         $html.=fgets($hfile,1024);
       }
     }
   }
  return $html;
 }

And use this script to call curl_url function

$url='http://www.amazon.com/s/ref=sr_nr_p_72_0?rh=n%3A172282%2Ck%3Aiphone+5s%2Cp_72%3A1248879011&keywords=iphone+5s&ie=UTF8&qid=1406078696&rnid=1248877011';
$getelement = curl_url($url);            
preg_match_all ("/a[\s]+[^>]*?href[\s]?=[\s\"\']+"."(.*?)[\"\']+.*?>"."([^<]+|.*?)?<\/a>/", $getelement, $matches,PREG_PATTERN_ORDER);    
$matches = $matches[1];
$list = array();
    foreach($matches as $var)
        {    
           print_r($var."\n");
        }

The result is :

asin grabber curl php result

With use preg_match_all ( Perform a global regular expression match ) function, We can filter the html element, fit our purpose.

preg_match_all ("/a[\s]+[^>]*?href[\s]?=[\s\"\']+"."(.*?)[\"\']+.*?>"."([^<]+|.*?)?<\/a>/", $getelement, $matches,PREG_PATTERN_ORDER);

That function is filter the <a href> html element from amazon url
http://www.amazon.com/s/ref=sr_nr_p_72_0?rh=n%3A172282%2Ck%3Aiphone+5s%2Cp_72%3A1248879011&keywords=iphone+5s&ie=UTF8&qid=1406078696&rnid=1248877011

 The next step is filter string to get Amazon ASIN Product 

Change print_r($var.”\n”); with


$result=explode("/",$var);
if (count($result)>=5)
{
  if ($result[4]=='dp')
  {
    print_r($result[5]."<br />");
  }
}

Voila!! We Grab ASIN from amazon.com 😀

ASIN grabber resultWhat is syntax if ($result[4]==’dp’) { …. } for ?

After we got <a href> element from curl_url function, check the url . ASIN code is locate before the word ‘dp’ . An example from result

http://www.amazon.com/Apple-iPhone-5s-Gold-Unlocked/dp/B00F3J4E5U/ref=sr_1_1/181-6054500-5986120?s=electronics&ie=UTF8&qid=1406083079&sr=1-1&keywords=iphone+5s .

Split url into array with explode function :  $result=explode(“/”,$var); . Word ‘dp’ contained in array number 4 and ASIN code in array number 5. Filter array number 4 and grab ASIN code in array number 5.

Example :

It`s easy right ?  🙂   You can modify this script to your needs. Thank you for visiting my site .

The following two tabs change content below.
This site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.

2 Comments

 Add your comment
  1. Hello Sigit.

    Thank you for sharing valuable tips on ASIN grabbing.
    I am having trouble producing successful result from the script given on this page, and I am glad if you could help me out.
    I simply copy and paste given script in the given order just like bellow. Do you see any false in it?

Leave a Comment

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

%d bloggers like this: