Skip to content

Commit 825a1c9

Browse files
committed
Merge pull request #25 from CPIGroup/master
Ready for next update version
2 parents df80acc + e9b7624 commit 825a1c9

File tree

10 files changed

+341
-10
lines changed

10 files changed

+341
-10
lines changed

INSTALL.md

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,52 @@
22
To install, simply add the library to your project. Composer is the default installation tool for this library.
33
If you do not use Composer for your project, you can still auto-load classes by including the file **includes/classes.php** in the page or function.
44

5-
Before you use any commands, you need to create a **amazon-config.php** file with your account credentials. Start by copying the template provided (*amazon-config.default.php*) and renaming the file.
5+
Before you use any commands, you need to create an **amazon-config.php** file with your account credentials. Start by copying the template provided (*amazon-config.default.php*) and renaming the file.
66

7-
Only the first section of the config file needs to be filled out. If you are operating outside of the United States, be sure to change the Amazon Service URL as well. Everything after that point is pertaining to Amazon's API. Be sure to keep these values up to date, as Amazon could change them in a new API version release.
7+
If you are operating outside of the United States, be sure to change the Amazon Service URL to the one matching your region.
88

9-
You can also link the built-in logging system to your own logging system by putting the function name in the *$logfunction* parameter.
9+
You can also link the built-in logging system to your own system by putting the logging function's name in the *$logfunction* parameter.
1010

11-
In the event that PHP does not have the correct permissions to create a file in the library's main directory, you will have to create the log file as "log.txt" and give all users permission to edit it.
11+
The default location for the built-in log file is in the library's main directory. In the event that PHP does not have the correct permissions to create a file in there, you will have to create the log file as "log.txt" and give PHP permission to edit it.
1212

13-
## Example Usage
13+
## Usage
14+
All of the technical details required by the API are handled behind the scenes,
15+
so users can easily build code for sending requests to Amazon
16+
without having to jump hurdles such as parameter URL formatting and token management.
1417
The general work flow for using one of the objects is this:
18+
1519
1. Create an object for the task you need to perform.
1620
2. Load it up with parameters, depending on the object, using *set____* methods.
1721
3. Submit the request to Amazon. The methods to do this are usually named *fetch____* or *submit____* and have no parameters.
1822
4. Reference the returned data, whether as single values or in bulk, using *get____* methods.
19-
5. Monitor the performance of the library using built-in logging system.
23+
5. Monitor the performance of the library using the built-in logging system.
2024

2125
Note that if you want to act on more than one Amazon store, you will need a separate object for each store.
2226

2327
Also note that the objects perform best when they are not treated as reusable. Otherwise, you may end up grabbing old response data if a new request fails.
28+
29+
## Examples
30+
Here is an example of a function used to get all warehouse-fulfilled orders from Amazon updated in the past 24 hours:
31+
```php
32+
function getAmazonOrders() {
33+
$amz = new AmazonOrderList("myStore"); //store name matches the array key in the config file
34+
$amz->setLimits('Modified', "- 24 hours");
35+
$amz->setFulfillmentChannelFilter("MFN"); //no Amazon-fulfilled orders
36+
$amz->setOrderStatusFilter(
37+
array("Unshipped", "PartiallyShipped", "Canceled", "Unfulfillable")
38+
); //no shipped or pending
39+
$amz->setUseToken(); //Amazon sends orders 100 at a time, but we want them all
40+
$amz->fetchOrders();
41+
return $amz->getList();
42+
}
43+
```
44+
This example shows a function used to send a previously-created XML feed to Amazon to update Inventory numbers:
45+
```php
46+
function sendInventoryFeed($feed) {
47+
$amz=new AmazonFeed("myStore"); //store name matches the array key in the config file
48+
$amz->setFeedType("_POST_INVENTORY_AVAILABILITY_DATA_"); //feed types listed in documentation
49+
$amz->setFeedContent($feed);
50+
$amz->submitFeed();
51+
return $amz->getResponse();
52+
}
53+
```

README.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,38 @@
11
phpAmazonMWS
22
============
33

4-
A library to connect to Amazon's MWS web services in an object-oriented manner, with a focus on intuitive usage.
4+
A library to connect to Amazon's Merchant Web Services (MWS) in an object-oriented manner, with a focus on intuitive usage.
5+
6+
This is __NOT__ for Amazon Web Services (AWS) - Cloud Computing Services.
7+
8+
9+
## Example Usage
10+
Here are a couple of examples of the library in use.
11+
All of the technical details required by the API are handled behind the scenes,
12+
so users can easily build code for sending requests to Amazon
13+
without having to jump hurdles such as parameter URL formatting and token management.
14+
15+
Here is an example of a function used to get all warehouse-fulfilled orders from Amazon updated in the past 24 hours:
16+
```php
17+
function getAmazonOrders() {
18+
$amz = new AmazonOrderList("myStore"); //store name matches the array key in the config file
19+
$amz->setLimits('Modified', "- 24 hours");
20+
$amz->setFulfillmentChannelFilter("MFN"); //no Amazon-fulfilled orders
21+
$amz->setOrderStatusFilter(
22+
array("Unshipped", "PartiallyShipped", "Canceled", "Unfulfillable")
23+
); //no shipped or pending
24+
$amz->setUseToken(); //Amazon sends orders 100 at a time, but we want them all
25+
$amz->fetchOrders();
26+
return $amz->getList();
27+
}
28+
```
29+
This example shows a function used to send a previously-created XML feed to Amazon to update Inventory numbers:
30+
```php
31+
function sendInventoryFeed($feed) {
32+
$amz=new AmazonFeed("myStore"); //store name matches the array key in the config file
33+
$amz->setFeedType("_POST_INVENTORY_AVAILABILITY_DATA_"); //feed types listed in documentation
34+
$amz->setFeedContent($feed);
35+
$amz->submitFeed();
36+
return $amz->getResponse();
37+
}
38+
```

environment.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
$AMAZON_VERSION_FEEDS = '2009-01-01';
3131
$AMAZON_VERSION_INBOUND = '2010-10-01';
3232
$AMAZON_VERSION_INVENTORY = '2010-10-01';
33-
$AMAZON_VERSION_ORDERS = '2011-01-01';
33+
$AMAZON_VERSION_ORDERS = '2013-09-01';
3434
$AMAZON_VERSION_OUTBOUND = '2010-10-01';
3535
$AMAZON_VERSION_PRODUCTS = '2011-10-01';
3636
$AMAZON_VERSION_REPORTS = '2009-01-01';

examples/feed_examples.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
die('This is just an example and will not work without proper store credentials.');
3+
4+
/*
5+
* This script retrieves a list of active feeds for the store "myStore" and display info on them.
6+
*/
7+
$list=getAmazonFeedStatus();
8+
if ($list) {
9+
echo 'Feed Status Report<hr>';
10+
foreach ($list as $feed) {
11+
//these are arrays
12+
echo '<b>Feed ID:</b> '.$feed['FeedSubmissionId'];
13+
echo '<br><b>Type:</b> '.$feed['FeedType'];
14+
echo '<br><b>Date Sent:</b> '.$feed['SubmittedDate'];
15+
echo '<br><b>Status:</b> '.$feed['FeedProcessingStatus'];
16+
echo '<br><br>';
17+
}
18+
}
19+
20+
/**
21+
* This function will retrieve a list of all items with quantity that was adjusted within the past 24 hours.
22+
* The entire list of items is returned, with each item contained in an array.
23+
* Note that this does not relay whether or not the feed had any errors.
24+
* To get this information, the feed's results must be retrieved.
25+
*/
26+
function getAmazonFeedStatus(){
27+
require('../includes/classes.php'); //autoload classes, not needed if composer is being used
28+
try {
29+
$amz=new AmazonFeedList("myStore");
30+
$amz->setTimeLimits('- 24 hours'); //limit time frame for feeds to any updated since the given time
31+
$amz->setFeedStatuses(array("_SUBMITTED_", "_IN_PROGRESS_", "_DONE_")); //exclude cancelled feeds
32+
$amz->fetchFeedSubmissions(); //this is what actually sends the request
33+
return $amz->getFeedList();
34+
} catch (Exception $ex) {
35+
echo 'There was a problem with the Amazon library. Error: '.$ex->getMessage();
36+
}
37+
}
38+
39+
/**
40+
* This function will send a provided Inventory feed to Amazon.
41+
* Amazon's response to the feed is returned as an array.
42+
* This function is not actively used on this example page as a safety precaution.
43+
*/
44+
function sendInventoryFeed($feed) {
45+
try {
46+
$amz=new AmazonFeed("myStore"); //store name matches the array key in the config file
47+
$amz->setFeedType("_POST_INVENTORY_AVAILABILITY_DATA_"); //feed types listed in documentation
48+
$amz->setFeedContent($feed); //can be either XML or CSV data; a file upload method is available as well
49+
$amz->submitFeed(); //this is what actually sends the request
50+
return $amz->getResponse();
51+
} catch (Exception $ex) {
52+
echo 'There was a problem with the Amazon library. Error: '.$ex->getMessage();
53+
}
54+
}
55+
56+
/**
57+
* This function will get the processing results of a feed previously sent to Amazon and give the data.
58+
* In order to do this, a feed ID is required. The response is in XML.
59+
*/
60+
function getFeedResult($feedId) {
61+
try {
62+
$amz=new AmazonFeedResult("myStore", $feedId); //feed ID can be quickly set by passing it to the constructor
63+
$amz->setFeedId($feedId); //otherwise, it must be set this way
64+
$amz->fetchFeedResult();
65+
return $amz->getRawFeed();
66+
} catch (Exception $ex) {
67+
echo 'There was a problem with the Amazon library. Error: '.$ex->getMessage();
68+
}
69+
}
70+
71+
72+
?>

examples/inventory_examples.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
die('This is just an example and will not work without proper store credentials.');
3+
4+
/*
5+
* This script retrieves a list of recently changed item supply info for the store "myStore" and display some of it.
6+
*/
7+
$list=getAmazonSupply();
8+
if ($list) {
9+
echo 'Amazon Inventory<hr>';
10+
foreach ($list as $item) {
11+
//these are arrays
12+
echo '<b>Item SKU:</b> '.$item['SellerSKU'];
13+
echo '<br><b>Condition:</b> '.$item['Condition'];
14+
echo '<br><b>In Stock:</b> '.$item['InStockSupplyQuantity'];
15+
echo '<br><br>';
16+
}
17+
}
18+
19+
/**
20+
* This function will retrieve a list of all items with quantity that was adjusted within the past 24 hours.
21+
* The entire list of items is returned, with each item contained in an array.
22+
*/
23+
function getAmazonSupply(){
24+
require('../includes/classes.php'); //autoload classes, not needed if composer is being used
25+
try {
26+
$obj = new AmazonInventoryList("myStore"); //store name matches the array key in the config file
27+
$obj->setUseToken(); //tells the object to automatically use tokens right away
28+
$obj->setStartTime("- 24 hours");
29+
$obj->fetchInventoryList(); //this is what actually sends the request
30+
return $obj->getSupply();
31+
} catch (Exception $ex) {
32+
echo 'There was a problem with the Amazon library. Error: '.$ex->getMessage();
33+
}
34+
}
35+
36+
37+
?>

examples/order_examples.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
die('This is just an example and will not work without proper store credentials.');
3+
4+
/*
5+
* This script retrieves a list of orders from the store "myStore" and displays various bits of their info.
6+
*/
7+
$list=getAmazonOrders();
8+
if ($list) {
9+
echo 'My Store Orders<hr>';
10+
foreach ($list as $order) {
11+
//these are AmazonOrder objects
12+
echo '<b>Order Number:</b> '.$order->getAmazonOrderId();
13+
echo '<br><b>Purchase Date:</b> '.$order->getPurchaseDate();
14+
echo '<br><b>Status:</b> '.$order->getOrderStatus();
15+
echo '<br><b>Customer:</b> '.$order->getBuyerName();
16+
$address=$order->getShippingAddress(); //address is an array
17+
echo '<br><b>City:</b> '.$address['City'];
18+
echo '<br><br>';
19+
}
20+
}
21+
22+
/**
23+
* This function will retrieve a list of all unshipped MFN orders made within the past 24 hours.
24+
* The entire list of orders is returned, with each order contained in an AmazonOrder object.
25+
* Note that the items in the order are not included in the data.
26+
* To get the order's items, the "fetchItems" method must be used by the specific order object.
27+
*/
28+
function getAmazonOrders() {
29+
require('../includes/classes.php'); //autoload classes, not needed if composer is being used
30+
try {
31+
$amz = new AmazonOrderList("myStore"); //store name matches the array key in the config file
32+
$amz->setLimits('Modified', "- 24 hours"); //accepts either specific timestamps or relative times
33+
$amz->setFulfillmentChannelFilter("MFN"); //no Amazon-fulfilled orders
34+
$amz->setOrderStatusFilter(
35+
array("Unshipped", "PartiallyShipped", "Canceled", "Unfulfillable")
36+
); //no shipped or pending orders
37+
$amz->setUseToken(); //tells the object to automatically use tokens right away
38+
$amz->fetchOrders(); //this is what actually sends the request
39+
return $amz->getList();
40+
} catch (Exception $ex) {
41+
echo 'There was a problem with the Amazon library. Error: '.$ex->getMessage();
42+
}
43+
}
44+
45+
?>

includes/classes/AmazonCore.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ abstract class AmazonCore{
105105
protected $mockIndex = 0;
106106
protected $logpath;
107107
protected $env;
108+
protected $rawResponses = array();
108109

109110
/**
110111
* AmazonCore constructor sets up key information used in all Amazon requests.
@@ -589,9 +590,49 @@ protected function sendRequest($url,$param){
589590
$response = $this->fetchURL($url,$param);
590591
}
591592

593+
$this->rawResponses[]=$response;
592594
return $response;
593595
}
594596

597+
/**
598+
* Gives the latest response data received from Amazon.
599+
* Response arrays contain the following keys:
600+
* <ul>
601+
* <li><b>head</b> - The raw HTTP head, including the response code and content length</li>
602+
* <li><b>body</b> - The raw HTTP body, which will almost always be in XML format</li>
603+
* <li><b>code</b> - The HTTP response code extracted from the head for convenience</li>
604+
* <li><b>answer</b> - The HTTP response message extracted from the head for convenience</li>
605+
* <li><b>ok</b> - Contains a <b>1</b> if the response was normal, or <b>0</b> if there was a problem</li>
606+
* <li><b>headarray</b> - An associative array of the head data, for convenience</li>
607+
* </ul>
608+
* @param int $i [optional] <p>If set, retrieves the specific response instead of the last one.
609+
* If the index for the response is not used, <b>FALSE</b> will be returned.</p>
610+
* @return array associative array of HTTP response or <b>FALSE</b> if not set yet
611+
*/
612+
public function getLastResponse($i=NULL) {
613+
if (!isset($i)) {
614+
$i=count($this->rawResponses)-1;
615+
}
616+
if ($i >= 0 && isset($this->rawResponses[$i])) {
617+
return $this->rawResponses[$i];
618+
} else {
619+
return false;
620+
}
621+
}
622+
623+
/**
624+
* Gives all response code received from Amazon.
625+
* @return array list of associative arrays of HTTP response or <b>FALSE</b> if not set yet
626+
* @see getLastResponse
627+
*/
628+
public function getRawResponses() {
629+
if (!empty($this->rawResponses)) {
630+
return $this->rawResponses;
631+
} else {
632+
return false;
633+
}
634+
}
635+
595636
/**
596637
* Sleeps for the throttle time and records to the log.
597638
*/

0 commit comments

Comments
 (0)