Code Monkey home page Code Monkey logo

amazon's Introduction

#Amazon Product API -v4 ####Purpose The purpose of this library is to make use of Amazon's associates program and pull Amazon product information via rest quite easily.

####Notes The documentation for this API is not very straightforward, and there aren't too many easy to understand libraries out there that make use of API version 4 for Amazon's associates program. Furthermore, the documentation to authenticate URL's is painful to read through. Thus, I've created my own library in hopes to help others out there, and my library is VERY easy to understand, especially with my example framework provided. Extending this library is also easy and adding more attributes is a breeze! Here are the current attributes this library will handle:

  • title
  • description
  • url
  • numOffers
  • image
  • price
  • averagePrice

####References

####How To Use

  • In index.php, be sure to require Amazon.php: require_once('Amazon.php');
  • Instantiate your object: $amazon = new Amazon();
  • Set your Amazon configuration:
$amazon->config()
	->API_KEY('Your API')
	->SECRET_KEY('Your Secret Key')
	->associate_tag('associatetag-20')
	->locale('com')
	->maxResults($numResults);
  • Note: Another config option is requestDelay. Set this option to true if you want to add a one second delay to your request, which eliminates the api request limit. By default, this option is set to false (for example, don't bother setting it at all). You may also explicitly set this value to false.
  • Example: $amazon->config()->requestDelay(true) or $amazon->config()->requestDelay(false) or just don't set it like in the above example.
  • See [Documentation] (https://affiliate-program.amazon.com/gp/advertising/api/detail/faq.html) about this.
  • Search for a keyword: $amazon->search($kw);
  • Attributes can now easily be retrieved, and all attributes have a data type of an array. For example, if maxResults is set to 10, each attribute will return an array of 10 results:
  • $title = $amazon->get()->title(); 10 titles are returned and saved to the $title variable
  • $url = $amazon->get()->url(); 10 urls are returned and saved to the $url variable
  • With just some basic knowledge of PHP, if you know how to iterate through an array you can get creative from here and output values in whatever html markup you desire!
  • So, once you get your Amazon attributes (title, url, price, etc), I suggest that you set up a for loop like this:
for ($i = 0; $i < $numResults; $i++) {
        $result .= amazonLayout($i, $amazon);
    }
  • Notice the variable $amazon. $amazon is actually an object, and we can pass this object as a parameter into a new function with data type Amazon, like this:
function amazonLayout($i, Amazon $amazon) {
    //Get your amazon attributes here
    $title = $amazon->get()->title();
    $url = $amazon->get()->url();
    .
    .
    .

    //Your custom code and formatting here
    $newTitle = "<div>$title[$i]</div>";
    $newUrl = "<div>$url[$i]</div>";

    //..etc
    return $newTitle;
}
  • Let's say we wanted to print a list of 5 titles, the shell code to do that would like something like:
    $amazon = new Amazon();  //Instantiate Amazon object
    $kw = "product title";  //Set keyword
    $numResults = 5;  //Set amazon max results, up to 10
    
    //Set config options
    $amazon->config()
            ->API_KEY('Your API')
            ->SECRET_KEY('Your Secret Key')
            ->associate_tag('associatetag-20')
            ->locale('com')
            ->maxResults($numResults);

    //Search for keyword
    $amazon->search($kw);

    //Loop through array in for loop to save your Amazon results
    for ($i = 0; $i < $cnt; $i++) {
        $result .= amazonLayout($i, $amazon);
    }

    //Clear amazon object
    $amazon->clear();

    //Set and return results, in this case, 5 product titles
    echo $result;
  • Where the function amazonLayout($i, $amazon) is defined above.
  • Be sure you call the clear function after you are done retrieving results since the url string is cached in order to save on API hits, and speed up the request: $amazon->clear();
  • A full shell example is provided in index.php

####Real Example Usage

amazon's People

Contributors

chopin2256 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.