• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
seegatesite header

Seegatesite.com

Seegatesite.com - Programming Tutorial , Sharing , How and Learn Together

  • TOOLS
    • Bootstrap Navbar Online Generator
    • Customize Sidebar Menu Bootstrap 3
    • Bootstrap Demo
  • ADVERTISE
  • CATEGORIES
    • Android
    • Blogging Tips
    • Database
    • CSS
    • Info Gadget
    • Javascript
    • Linux
    • PHP
    • Various
    • WordPress
  • Q&A
  • PHP
  • JAVASCRIPT
  • JQUERY
  • ANGULAR
  • WORDPRESS
  • SEO
  • REACT
🏠 » PHP » Easy Build Amazon ASIN Grabber with PHP and Curl

Easy Build Amazon ASIN Grabber with PHP and Curl

By Sigit Prasetya Nugroho ∙ July 23, 2014 ∙ PHP ∙ 1 Comment

Share : TwitterFacebookTelegramWhatsapp

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.

Table of Contents

  • 1 Why use PHP CURL to build the amazon ASIN grabber?
    • 1.1  The next step is filter string to get Amazon ASIN Product 
    • 1.2 What is syntax if ($result[4]==’dp’) { …. } for ?

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.

Related Articles :

  • How to Create Autocomplete Amazon Keyword Suggestion With Jquery
  • Easy Get XML Amazon API Data with My Amazon_Class
  • Easy Get ASIN with My Amazon Asin Grabber Class

Create CURL function as curl_url

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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

1
2
3
4
5
6
7
8
9
$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.

1
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

1
2
3
4
5
6
7
8
$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 .

Another PHP Related Post :

  • How To Replace String With Another String In PHP
  • Login Page – Tutorial CRUD Client and API Server Using JQuery And Lumen Part 2
  • Tutorial CRUD Client and API Server Using JQuery And Lumen Part 1
  • How To Solve Problems Illegal mix of collations (latin1_swedish_ci,IMPLICIT) In Laravel
  • How To Resolve No ‘Access-Control-Allow-Origin’ Header In Lumen
  • How To Create Custom Class In Laravel 5.5 For Beginners

Avatar for Sigit Prasetya Nugroho

About Sigit Prasetya Nugroho

This site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.

Reader Interactions

Comments

  1. Avatar for TuckTuck says

    July 31, 2016 at 9:19 am

    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?

    Reply
    • Avatar for Sigit Prasetya NugrohoSigit Prasetya Nugroho says

      August 1, 2016 at 2:45 pm

      Please show me your error or warning message..Maybe I can help you 🙂

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Welcome to my Home,

Avatar for Sigit Prasetya NugrohoThis site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.



Popular Articles

Checked checkbox AdminLTE Bootstrap in Jquery

November 4, 2014 By Sigit Prasetya Nugroho 7 Comments

Simple create date format validation with jqueryUI

December 21, 2014 By Sigit Prasetya Nugroho Leave a Comment

Create Simple Progress Bar for Fake Online Generator with Jquery

January 10, 2015 By Sigit Prasetya Nugroho Leave a Comment

22+ Coolest Free Jquery Plugin For Premium Theme

October 3, 2015 By Sigit Prasetya Nugroho Leave a Comment

Easy Build Your Anti Copy Paste Plugin

October 6, 2015 By Sigit Prasetya Nugroho Leave a Comment

Popular Tags

adminlte (15) adsense (13) adsense tips (4) affiliate amazon (13) amazon (12) Android (8) angular (16) angular 4 (12) angular 5 (4) asin grabber (3) Bootstrap (27) codeigniter (5) create wordpress theme (5) crud (8) css (6) free wordpress theme (7) google adsense (4) imacros (4) increase traffic (6) jquery (34) laravel (10) laravel 5 (5) learn android (5) modal dialog (5) mysql (6) nodeJs (4) optimize seo (4) pdo (6) php (30) plugin (53) pos (7) Publisher Tips (5) react (3) Reactjs (7) SEO (37) theme (17) tutorial angular (5) tutorial angular 4 (6) tutorial javascript (10) tutorial javascript beginners (4) twitter (3) widget (3) wordpress (18) wordpress plugin (13) XMLRPC (5)




  • About
  • Contact Us
  • Disclaimer
  • Privacy Policy
  • Terms and Conditions

©2021 Seegatesite.com