Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: Jacob Mahto
Added: Jul 22, 2018 8:25 AM
Views: 3
Fetching images from amazon using curl php.
  1. <?php
  2.  
  3. function check() {
  4.     //create curl resource
  5.     $ch = curl_init();
  6.     $search_string = "pc video games";
  7.     $url = "https://www.amazon.com/s/field-keywords=$search_string";
  8.  
  9.     //set curl options
  10.     curl_setopt($ch, CURLOPT_COOKIE, true); //websites like https://www.amazon.com have robot check feature , which restricts the opening of their webpage,therefore this option has been used.
  11.     curl_setopt($ch, CURLOPT_URL, $url);
  12.  
  13.     //For accessing the https sites
  14.     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  15.  
  16.     //For saving the output in a variable (which we create during execution) , instead of displaying the webpage. To see that output , just echo it out.
  17.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  18.  
  19.  
  20.     //run curl (i.e. execute curl resource
  21.     $output = curl_exec($ch);
  22.  
  23.     preg_match_all("!https://images-na.ssl-images-amazon.com/images/I/[^\s]*?._AC_US218_.jpg!", $output, $matches);
  24.  
  25.     $images = array_values(array_unique($matches[0]));
  26.     for ($i = 0; $i < count($images); $i++) {
  27.         echo "<div style='float:left;margin:10 0 0 0;'>"
  28.         . "<img src='$images[$i]'><br></div>";
  29.     }
  30.  
  31.     //close curl resource
  32.     curl_close($ch);
  33. }
  34. ?>
  35.