Guest User

Untitled

a guest
Nov 18th, 2016
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. <?php
  2. //Find new movie titles
  3. include '../../config/db.php';
  4. //include("simple_html_dom.php");
  5. // Turn off all error reporting
  6. error_reporting(0);
  7.  
  8. // Create connection
  9. $connection = new mysqli($servername, $username, $password, $dbname);
  10. // Check connection
  11. if ($connection->connect_error) {
  12. die("Connection failed: " . $connection->connect_error);
  13. }
  14.  
  15. // include simple html dom parser class
  16. require_once 'simple_html_dom.php';
  17.  
  18. // If secret pass is correct run the jobs below
  19. $sql = "SELECT * FROM pages";
  20. $results = $connection->query($sql);
  21. //Loop through and echo all the records
  22. while ($row = $results->fetch_assoc()){
  23. //Loop with style is started
  24. $pages = $row["pages"];
  25.  
  26. /*
  27. * Amazon Product Search class
  28. */
  29.  
  30. class Amazon_Product_Search
  31. {
  32. protected $url;
  33. public function __construct($related_h)
  34. {
  35. $this->url = "https://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Daps&pages='{$related_h}'&field-keywords=";
  36. }
  37.  
  38.  
  39.  
  40.  
  41. //class Amazon_Product_Search{
  42. //protected $url = "https://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Daps&field-keywords=";
  43. protected $keyword='';
  44.  
  45. public function __construct($keyword){
  46. $this->url = $this->url.$keyword;
  47. }
  48.  
  49. public function get(){
  50. $products = array();
  51. // Prices array
  52. $ps = array();
  53. try{
  54. $webPageContent = $this->getWebPageContent();
  55. if ( !empty( $webPageContent ) ){
  56. $html = str_get_html($webPageContent);
  57.  
  58. foreach($html->find('li.s-result-item') as $key => $li) {
  59. $li = $this->appendTags($li);
  60. foreach($li->find('img.s-access-image') as $element){
  61. $products[$key]['images'] = $element->src;
  62. }
  63.  
  64.  
  65. //Test
  66.  
  67.  
  68.  
  69.  
  70. //End test
  71.  
  72.  
  73.  
  74. foreach($li->find('a.h2.s-access-title') as $element){
  75. $products[$key]['titles'] = $element->plaintext;
  76. }
  77.  
  78. foreach($li->find('a.span.s-price') as $element){
  79. $price = !empty($element->plaintext)?$element->plaintext:'0';
  80. $products[$key]['prices'] = $price;
  81. }
  82. }
  83. // Sort products on the bases of prices
  84. foreach ($products as $key => $row){
  85. $row['prices'] = !empty($row['prices'])?$row['prices']:0;
  86. $ps[$key] = str_replace('$','',$row['prices']);
  87. }
  88. array_multisort($ps, SORT_DESC, $products);
  89. }
  90.  
  91.  
  92. return $products;
  93. }catch (\Exception $e){
  94. return $e->getMessage();
  95. }
  96. }
  97. /*
  98. * Fetch the content from resource url
  99. */
  100. protected function getWebPageContent() {
  101. if (function_exists('curl_init')) {
  102. $ch = curl_init();
  103. curl_setopt($ch, CURLOPT_URL, $this->url);
  104. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  105. set_time_limit(0);
  106. $content = curl_exec($ch);
  107. curl_close($ch);
  108. return $content;
  109. }else{
  110. return "";
  111. }
  112. }
  113. /*
  114. * Utility funtion to append html tags
  115. */
  116. public function appendTags($html){
  117. return str_get_html('<html>'.$html.'</html>');
  118. }
  119. /*
  120. * Print the array results in readable form
  121. */
  122. public function pre($args){
  123. echo '<pre>';
  124. print_r($args);
  125. echo '</pre>';
  126. }
  127.  
  128. }
  129.  
  130. //End loop
  131. }
  132.  
  133. if (empty($_GET)) {
  134. //if(isset($_GET['id'])){
  135. //Load keywords from database
  136. $search_word = $keywords;
  137. $aps = new Amazon_Product_Search($search_word);
  138. $result = $aps->get();
  139. }
  140. // Buffer the output
  141. ob_start();
  142. ?>
  143. <!DOCTYPE html>
  144. <html lang="en-US">
  145. <head>
  146. <style>
  147. .city {
  148. float: left;
  149. margin: 5px;
  150. padding: 15px;
  151. width: 300px;
  152. height: 300px;
  153. border: 1px solid black;
  154. }
  155. </style>
  156. </head>
  157. <body>
  158. <?php
  159. if(!empty($result)){
  160. echo '<h1>Amazon Products</h1>';
  161. foreach($result as $key => $products) {
  162. ?>
  163. <?php echo $products['titles'];?><br>
  164. <?php echo $show_url;?><br>
  165. <img src="<?php echo $products['images'];?>" /><br>
  166. <?php echo !empty($products['prices'])?$products['prices']:0;?><br>
  167.  
  168.  
  169. <?php
  170. }
  171. }else{
  172. echo 'No products found';
  173. }
  174. ?>
  175. </body>
  176. </html>
  177. <?php
  178. $content = ob_get_contents();
  179. ob_clean();
  180. echo $content;
  181. die();
  182.  
  183.  
  184.  
  185. ?>
Advertisement
Add Comment
Please, Sign In to add comment