-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamazon-search.php
99 lines (94 loc) · 3.42 KB
/
amazon-search.php
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php
include('amazon_api.php');
/* Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. */
/* Licensed under the Apache License, Version 2.0. */
$serviceName="ProductAdvertisingAPI";
$region="eu-west-1";
$keywords = 'Bluetooth earphone'; //Enter your keywords.
$accessKey = "Enter your access key"; // Enter your access key
$secretKey = "Enter your secret key"; //Enter your secret key
$AssociateTag = "Enter your associate tag"; //Enter your associate tag
$payload="{"
." \"Keywords\": \"$keywords\","
." \"Resources\": ["
." \"Images.Primary.Large\","
. " \"ItemInfo.Features\","
." \"ItemInfo.Title\","
." \"Offers.Listings.Price\","
." \"Offers.Listings.Promotions\","
." \"Offers.Listings.SavingBasis\""
." ],"
." \"PartnerTag\": \"$AssociateTag\","
." \"PartnerType\": \"Associates\","
." \"Marketplace\": \"www.amazon.in\""
."}";
$host="webservices.amazon.in";
$uriPath="/paapi5/searchitems";
$awsv4 = new AwsV4 ($accessKey, $secretKey);
$awsv4->setRegionName($region);
$awsv4->setServiceName($serviceName);
$awsv4->setPath ($uriPath);
$awsv4->setPayload ($payload);
$awsv4->setRequestMethod ("POST");
$awsv4->addHeader ('content-encoding', 'amz-1.0');
$awsv4->addHeader ('content-type', 'application/json; charset=utf-8');
$awsv4->addHeader ('host', $host);
$awsv4->addHeader ('x-amz-target', 'com.amazon.paapi5.v1.ProductAdvertisingAPIv1.SearchItems');
$headers = $awsv4->getHeaders ();
$headerString = "";
foreach ( $headers as $key => $value ) {
$headerString .= $key . ': ' . $value . "\r\n";
}
$params = array (
'http' => array (
'header' => $headerString,
'method' => 'POST',
'content' => $payload
)
);
$stream = stream_context_create ( $params );
$fp = @fopen ( 'https://'.$host.$uriPath, 'rb', false, $stream );
if (! $fp) {
throw new Exception ( "Exception Occured" );
}
$response = @stream_get_contents ( $fp );
if ($response === false) {
throw new Exception ( "Exception Occured" );
}
//echo $response;
$details = json_decode($response, true);
$products = $details['SearchResult']['Items'];
foreach($products as $product){
if (isset($product)) {
$asin = $product['ASIN'];
$productUrl = $product['DetailPageURL'];
}
if (isset($product['ItemInfo'])) {
$title = $product['ItemInfo']['Title']['DisplayValue'];
$features = $product['ItemInfo']['Features']['DisplayValues'];
$description = '';
foreach ($features as $feature) {
$description .= "<li>". $feature ."</li>";
}
}
if (isset($product['Images'])) {
$image = $product['Images']['Primary']['Large']['URL'];
}
if (isset($product['Offers'])) {
$Sellprice = $product['Offers']['Listings']['0']['Price']['Amount'];
if(!$Sellprice){
echo "Sell Price is not present.";
}
}else{
$Sellprice = 0;
}
if(isset($product['Offers']['Listings']['0']['SavingBasis'])){
$Maxprice = $MaxPrice =$product['Offers']['Listings']['0']['SavingBasis']['Amount'];
}else{
$MaxPrice = 0;
}
$Seller = 'Amazon';
echo $title ."<img align='right' src=$image height='120' width='80' /><br>". "Description: " .$description ."<br>Product Link: ". $productUrl ."<br> Deal Price: ". $Sellprice ."<br> Real Price: ". $MaxPrice ."<hr />";
}
echo "<hr />" .$details['SearchResult']['SearchURL'];
?>