Hãy chọn Truy cập mobile | Tiếp tục

Nét Đẹp Việt Nam

 Quên mật khẩu
 Register
Xem: 66|Trả lời: 1

Instagram Grabber PHP

[Lấy địa chỉ]
Đăng lúc 13-9-2017 14:37:17 | Xem tất |Chế độ đọc
1. List top images by hashtag

  1. <?php
  2. function scrape_insta_hash($tag) {
  3.         $insta_source = file_get_contents('https://www.instagram.com/explore/tags/'.$tag.'/'); // instagrame tag url
  4.         $shards = explode('window._sharedData = ', $insta_source);
  5.         $insta_json = explode(';</script>', $shards[1]);
  6.         $insta_array = json_decode($insta_json[0], TRUE);
  7.         return $insta_array; // this return a lot things print it and see what else you need
  8. }
  9. $tag = 'visitvietnam'; // tag for which ou want images
  10. $results_array = scrape_insta_hash($tag);
  11. $limit = 10; // provide the limit thats important because one page only give some images then load more have to be clicked
  12. $image_array= array(); // array to store images.
  13.         for ($i=0; $i < $limit; $i++) {
  14.                 $latest_array = $results_array['entry_data']['TagPage'][0]['tag']['media']['nodes'][$i];
  15.                  $image_data  = '<img src="'.$latest_array['thumbnail_src'].'">'; // thumbnail and same sizes
  16.                  //$image_data  = '<img src="'.$latest_array['display_src'].'">'; actual image and different sizes
  17.                 array_push($image_array, $image_data);
  18.         }
  19.         foreach ($image_array as $image) {
  20.                 echo $image;// this will echo the images wrap it in div or ul li what ever html structure
  21.         }
  22.         // for getting all images have to loop function for more pages
  23.         // for confirmation  you are getting correct images view
  24.         //https://www.instagram.com/explore/tags/your-tag-name/
  25. ?>

Sao chép mã

 Tác giả| Đăng lúc 13-9-2017 16:15:30 | Xem tất
Bulk Downloader from any public instagram users.
1.Login the instagram, export the cookies for your login session.
2. Create a script called: instagram_dl.php

  1. <?php
  2. include("includes/rollingcurlx.class.php");
  3. /**
  4. *   Crawls through user's page and downloads all avaliable images/videos
  5. */
  6. function download($RCX, $username, $max_id = 0) {
  7.         $id = '';
  8.         $lastId = '';
  9.         if ($max_id > 0) {
  10.                 $id = $max_id;
  11.         }
  12.         $userURL = "https://www.instagram.com/" . $username . "/media/?&max_id=" . $id;
  13.         $ch = curl_init();
  14.         $curl_options = array(
  15.                                                 CURLOPT_URL => $userURL,
  16.                                                 CURLOPT_REFERER => "https://www.instagram.com",
  17.                                                 CURLOPT_USERAGENT => "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6 (.NET CLR 3.5.30729)",
  18.                                                 CURLOPT_HEADER => 0,
  19.                                                 CURLOPT_RETURNTRANSFER => true,
  20.                                                 CURLOPT_TIMEOUT => 10,
  21.                                                 CURLOPT_HTTPHEADER => array('Content-type: application/json'),
  22.                                                 CURLOPT_COOKIEFILE => __DIR__ . "/cookies.txt",
  23.                                                 CURLOPT_SSL_VERIFYPEER => false
  24.                                         );
  25.         curl_setopt_array($ch, $curl_options);
  26.         $response = curl_exec($ch);
  27.        
  28.         if(empty($response)){
  29.                 die("API returned nothing\r\n");
  30.         }
  31.        
  32.         curl_close($ch);
  33.         $json = json_decode($response, true);
  34.        
  35.         if($json['status'] == "ok" && !empty($json['items'])) {       
  36.                 // Loop over json, get the filename, URL and timestamp
  37.                 foreach ($json['items'] as $data) {
  38.                         if($data['type'] == "video") {
  39.                                 $imageURL = $data['videos']['standard_resolution']['url'];
  40.                                 $name = explode("/", $imageURL);
  41.                                 $name = $name[count($name) - 1];
  42.                         } else {
  43.                                 $urlSplit = explode("/",
  44.                                                 $data['images']['standard_resolution']['url']);
  45.                                 $name = $urlSplit[count($urlSplit) - 1];
  46.                                
  47.                                 // Some images have URLs of different lengths
  48.                                 // "/s1080x1080/" ensures the image is the largest possible
  49.                                 if(count($urlSplit) == 6) {
  50.                                         $imageURL = $urlSplit[0] . "//" . $urlSplit[2]
  51.                                                         . "/" . $urlSplit[3] . "/" . $urlSplit[4] . "/"
  52.                                                         . $urlSplit[5];
  53.                                 } elseif(count($urlSplit) == 7) {
  54.                                         $imageURL = $urlSplit[0] . "//" . $urlSplit[2]
  55.                                                         . "/" . $urlSplit[3] . "/s1080x1080/" . $urlSplit[5]
  56.                                                         . "/" . $urlSplit[6];
  57.                                 } elseif(count($urlSplit) == 8) {
  58.                                         $imageURL = $urlSplit[0] . "//" . $urlSplit[2]
  59.                                                         . "/" . $urlSplit[3] . "/" . $urlSplit[4]
  60.                                                         . "/s1080x1080/" . $urlSplit[6] . "/" . $urlSplit[7];
  61.                                 } elseif(count($urlSplit) == 9) {
  62.                                         $imageURL = $urlSplit[0] . "//" . $urlSplit[2]
  63.                                                         . "/" . $urlSplit[3] . "/" . $urlSplit[4]
  64.                                                         . "/s1080x1080/" . $urlSplit[6] . "/" . $urlSplit[7]
  65.                                                         . "/" . $urlSplit[8];
  66.                                 } else {
  67.                                         $imageURL = $data['images']['standard_resolution']['url'];
  68.                                 }
  69.                         }
  70.                         // Add image to download queue
  71.                         $RCX->addRequest($imageURL, null, 'save', ['fileName' => $name, 'created_time' => $data['created_time'], 'username' => $username]);
  72.                         // Instagram only shows one page of images at a given time, saves the id of the last image
  73.                         $lastId = $data['id'];
  74.                 }
  75.         } else {
  76.                 die("Invalid username or private account.\r\n");
  77.         }
  78.         // Recurse if more images are avaliable
  79.         if($json['more_available'] == true){
  80.                 return download($RCX, $username, $lastId);
  81.         } else {
  82.                 $RCX->setOptions([array(
  83.                                                 CURLOPT_REFERER => "http://instagram.com",
  84.                                                 CURLOPT_USERAGENT => "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.6) Gecko/20091201 Firefox/3.5.6 (.NET CLR 3.5.30729)",
  85.                                                 CURLOPT_HEADER => 0,
  86.                                                 CURLOPT_RETURNTRANSFER => true,
  87.                                                 CURLOPT_TIMEOUT => 10,
  88.                                         )]);
  89.                 $RCX->execute();
  90.         }
  91. }
  92. function save($response, $url, $request_info, $user_data, $time) {
  93.         $saveto = "./" . $user_data['username'] . "/";
  94.         // Create user's folder
  95.         if(!file_exists($saveto)) {
  96.                 if (!mkdir($saveto, 0744, true)) {
  97.                         die(date("Y-m-d H:i:s") . " - Failed to create folder.\r\n");
  98.                 }
  99.         }
  100.        
  101.         $fileName = $user_data['fileName'];
  102.         $timestamp = $user_data['created_time'];
  103.        
  104.         // Instagram API sometimes gives weird file names
  105.         if(strpos($fileName, "ig_cache_key")) {
  106.                 $fileName = explode("?", $fileName)[0];
  107.         }
  108.        
  109.         $fileLocation = $saveto . $timestamp . "_" . $fileName;
  110.        
  111.         if(!file_exists($fileLocation)) {
  112.                 // Check error code
  113.                 if($request_info['http_code'] == "200") {
  114.                         echo("[" . $request_info['http_code'] . "] " .date("Y-m-d H:i:s") . " - saved " . $fileName . "\r\n");
  115.                 } else {
  116.                         echo("[" . $request_info['http_code'] . "] " .date("Y-m-d H:i:s") . " - Error downloading " . $fileName . " @ " . $url . "\r\n");
  117.                         return;
  118.                 }
  119.                
  120.                 $fp = fopen($fileLocation, 'w');
  121.                 fwrite($fp, $response);
  122.                 fclose($fp);
  123.         }
  124. }
  125. if(!isset($argv[1]) || empty($argv[1])) {
  126.         die("Usage: php " . $_SERVER["SCRIPT_FILENAME"] . " <username>\r\n");
  127. }
  128. if (!function_exists('curl_init')) {
  129.         die(date("Y-m-d H:i:s") . " - cURL is not installed.\r\n");
  130. }
  131. download(new RollingCurlX(10), $argv[1], 0);
  132. ?>
Sao chép mã

3. Create a scripted called:rollingcurlx.class.php put into the includes/ folder

  1. <?php
  2. /*
  3.         ---------- RollingCurlX 1.0.0 -----------
  4.         an easy to use curl_multi wrapper for php
  5.             Copyright (c) 2015 Marcus Leath
  6.                     License: MIT
  7.         https://github.com/marcushat/RollingCurlX
  8. */
  9. Class RollingCurlX {
  10.     private $_maxConcurrent = 0; //max. number of simultaneous connections allowed
  11.     private $_options = []; //shared cURL options
  12.     private $_headers = []; //shared cURL request headers
  13.     private $_callback = NULL; //default callback
  14.     private $_timeout = 5000; //all requests must be completed by this time
  15.     public $requests = []; //request_queue
  16.     function __construct($max_concurrent = 10) {
  17.         $this->setMaxConcurrent($max_concurrent);
  18.     }
  19.     public function setMaxConcurrent($max_requests) {
  20.         if($max_requests > 0) {
  21.             $this->_maxConcurrent = $max_requests;
  22.         }
  23.     }
  24.     public function setOptions(array $options) {
  25.         $this->_options = $options;
  26.     }
  27.     public function setHeaders(array $headers) {
  28.         if(is_array($headers) && count($headers)) {
  29.             $this->_headers = $headers;
  30.         }
  31.     }
  32.     public function setCallback(callable $callback) {
  33.         $this->_callback = $callback;
  34.     }
  35.     public function setTimeout($timeout) { //in milliseconds
  36.         if($timeout > 0) {
  37.             $this->_timeout = $timeout/1000; //to seconds
  38.         }
  39.     }
  40.     //Add a request to the request queue
  41.     public function addRequest(
  42.                         $url,
  43.                         $post_data = NULL,
  44.                         callable $callback = NULL, //individual callback
  45.                         $user_data = NULL,
  46.                         array $options = NULL, //individual cURL options
  47.                         array $headers = NULL //individual cURL request headers
  48.     ) { //Add to request queue
  49.         $this->requests[] = [
  50.             'url' => $url,
  51.             'post_data' => ($post_data) ? $post_data : NULL,
  52.             'callback' => ($callback) ? $callback : $this->_callback,
  53.             'user_data' => ($user_data) ? $user_data : NULL,
  54.             'options' => ($options) ? $options : NULL,
  55.             'headers' => ($headers) ? $headers : NULL
  56.         ];
  57.         return count($this->requests) - 1; //return request number/index
  58.     }
  59.     //Reset request queue
  60.     public function reset() {
  61.         $this->requests = [];
  62.     }
  63.     private function normalize_headers(array $headers) {
  64.         $normalized = [];
  65.         foreach($headers as $key => $header) {
  66.             if(is_string($key)) {
  67.                 $normal = "$key: $header";
  68.             } else {
  69.                 $header;
  70.             }
  71.             $normalized = [];
  72.         }
  73.     }
  74.     //Execute the request queue
  75.     public function execute() {
  76.         if(count($this->requests) < $this->_maxConcurrent) {
  77.             $this->_maxConcurrent = count($this->requests);
  78.         }
  79.         //the request map that maps the request queue to request curl handles
  80.         $requests_map = [];
  81.         $multi_handle = curl_multi_init();
  82.         //start processing the initial request queue
  83.         for($i = 0; $i < $this->_maxConcurrent; $i++) {
  84.             $this->init_request($i, $multi_handle, $requests_map);
  85.         }
  86.         do{
  87.             do{
  88.                 $mh_status = curl_multi_exec($multi_handle, $active);
  89.             } while($mh_status == CURLM_CALL_MULTI_PERFORM);
  90.             if($mh_status != CURLM_OK) {
  91.                 break;
  92.             }
  93.             //a request is just completed, find out which one
  94.             while($completed = curl_multi_info_read($multi_handle)) {
  95.                 $this->process_request($completed, $multi_handle, $requests_map);
  96.                 //add/start a new request to the request queue
  97.                 if($i < count($this->requests) && isset($this->requests[$i])) { //if requests left
  98.                     $this->init_request($i, $multi_handle, $requests_map);
  99.                     $i++;
  100.                 }
  101.             }
  102.             usleep(15); //save CPU cycles, prevent continuous checking
  103.         } while ($active || count($requests_map)); //End do-while
  104.         $this->reset();
  105.         curl_multi_close($multi_handle);
  106.     }
  107.     //Build individual cURL options for a request
  108.     private function buildOptions(array $request) {
  109.         $url = $request['url'];
  110.         $post_data = $request['post_data'];
  111.         $individual_opts = $request['options'];
  112.         $individual_headers = $request['headers'];
  113.         $options = ($individual_opts) ? $individual_opts + $this->_options : $this->_options; //merge shared and individual request options
  114.         $headers = ($individual_headers) ? $individual_headers + $this->_headers : $this->_headers; //merge shared and individual request headers
  115.         //the below will overide the corresponding default or individual options
  116.         $options[CURLOPT_RETURNTRANSFER] = true;
  117.         $options[CURLOPT_NOSIGNAL] = 1;
  118.         $options[CURLOPT_CONNECTTIMEOUT] = max(1, $this->_timeout/1000); //minimum of 1 second
  119.         $options[CURLOPT_TIMEOUT] = $this->_timeout/1000;
  120.         if($url) {
  121.             $options[CURLOPT_URL] = $url;
  122.         }
  123.         if($headers) {
  124.             $options[CURLOPT_HTTPHEADER] = $headers;
  125.         }
  126.         // enable POST method and set POST parameters
  127.         if($post_data) {
  128.             $options[CURLOPT_POST] = 1;
  129.             $options[CURLOPT_POSTFIELDS] = is_array($post_data)? http_build_query($post_data) : $post_data;
  130.         }
  131.         return $options;
  132.     }
  133.     private function init_request($request_num, $multi_handle, &$requests_map) {
  134.         $request =& $this->requests[$request_num];
  135.         $this->addTimer($request);
  136.         $ch = curl_init();
  137.         $opts_set = curl_setopt_array($ch, $this->buildOptions($request));
  138.         if(!$opts_set) {
  139.             echo 'options not set';
  140.             exit;
  141.         }
  142.         curl_multi_add_handle($multi_handle, $ch);
  143.         //add curl handle of a new request to the request map
  144.         $ch_hash = (string) $ch;
  145.         $requests_map[$ch_hash] = $request_num;
  146.     }
  147.     private function process_request($completed, $multi_handle, array &$requests_map) {
  148.         $ch = $completed['handle'];
  149.         $ch_hash = (string) $ch;
  150.         $request =& $this->requests[$requests_map[$ch_hash]]; //map handler to request index to get request info
  151.         $request_info = curl_getinfo($ch);
  152.         $request_info['curle'] = $completed['result'];
  153.         $request_info['curle_msg'] = $this->curle_msgs[$completed['result']];
  154.         $request_info['handle'] = $ch;
  155.         $request_info['time'] = $time = $this->stopTimer($request); //record request time
  156.         $request_info['url_raw'] = $url = $request['url'];
  157.         $request_info['user_data'] = $user_data = $request['user_data'];
  158.         if(curl_errno($ch) !== 0 || intval($request_info['http_code']) !== 200) { //if server responded with http error
  159.             $response = false;
  160.         } else { //sucessful response
  161.             $response = curl_multi_getcontent($ch);
  162.         }
  163.         //get request info
  164.         $callback = $request['callback'];
  165.         $options = $request['options'];
  166.         if($response && (isset($this->_options[CURLOPT_HEADER]) || isset($options[CURLOPT_HEADER]))) {
  167.             $k = intval($request_info['header_size']);
  168.             $request_info['response_header'] = substr($response, 0, $k);
  169.             $response = substr($response, $k);
  170.         }
  171.         //remove completed request and its curl handle
  172.         unset($requests_map[$ch_hash]);
  173.         curl_multi_remove_handle($multi_handle, $ch);
  174.         //call the callback function and pass request info and user data to it
  175.         if($callback) {
  176.             call_user_func($callback, $response, $url, $request_info, $user_data, $time);
  177.         }
  178.         $request = NULL; //free up memory now just incase response was large
  179.     }
  180.     private function check_for_timeouts($mh) {
  181.         $now = microtime($true);
  182.         $request_map = $this->_request_map;
  183.         $requests = $this->_request_map;
  184.         foreach($request_maps as $ch_hash => $request_num) {
  185.             $request = $requests[$request_num];
  186.             $timeout = $request->timeout;
  187.             $start_time = $request->start_time;
  188.             $ch = $request->handle;
  189.             if($now >=  $start_time + $timeout) {
  190.                 curl_multi_remove_handle($mh, $ch);
  191.             }
  192.         }
  193.     }
  194.     private function addTimer(array &$request) { //adds timer object to request
  195.         $request['timer'] = microtime(true);
  196.         $request['time'] = false; //default if not overridden by time later
  197.     }
  198.     private function stopTimer(array &$request) {
  199.         $elapsed = $request['time'] = microtime(true) - $request['timer'];
  200.         unset($request['timer']);
  201.         return $elapsed;
  202.     }
  203.     private $curle_msgs = [CURLE_OK => 'OK', CURLE_UNSUPPORTED_PROTOCOL => 'UNSUPPORTED_PROTOCOL', CURLE_FAILED_INIT => 'FAILED_INIT', CURLE_URL_MALFORMAT => 'URL_MALFORMAT', CURLE_URL_MALFORMAT_USER => 'URL_MALFORMAT_USER', CURLE_COULDNT_RESOLVE_PROXY => 'COULDNT_RESOLVE_PROXY', CURLE_COULDNT_RESOLVE_HOST => 'COULDNT_RESOLVE_HOST', CURLE_COULDNT_CONNECT => 'COULDNT_CONNECT', CURLE_FTP_WEIRD_SERVER_REPLY => 'FTP_WEIRD_SERVER_REPLY', CURLE_FTP_ACCESS_DENIED => 'FTP_ACCESS_DENIED', CURLE_FTP_USER_PASSWORD_INCORRECT => 'FTP_USER_PASSWORD_INCORRECT', CURLE_FTP_WEIRD_PASS_REPLY => 'FTP_WEIRD_PASS_REPLY', CURLE_FTP_WEIRD_USER_REPLY => 'FTP_WEIRD_USER_REPLY', CURLE_FTP_WEIRD_PASV_REPLY => 'FTP_WEIRD_PASV_REPLY', CURLE_FTP_WEIRD_227_FORMAT => 'FTP_WEIRD_227_FORMAT', CURLE_FTP_CANT_GET_HOST => 'FTP_CANT_GET_HOST', CURLE_FTP_CANT_RECONNECT => 'FTP_CANT_RECONNECT', CURLE_FTP_COULDNT_SET_BINARY => 'FTP_COULDNT_SET_BINARY', CURLE_PARTIAL_FILE => 'PARTIAL_FILE', CURLE_FTP_COULDNT_RETR_FILE => 'FTP_COULDNT_RETR_FILE', CURLE_FTP_WRITE_ERROR => 'FTP_WRITE_ERROR', CURLE_FTP_QUOTE_ERROR => 'FTP_QUOTE_ERROR', CURLE_HTTP_NOT_FOUND => 'HTTP_NOT_FOUND', CURLE_WRITE_ERROR => 'WRITE_ERROR', CURLE_MALFORMAT_USER => 'MALFORMAT_USER', CURLE_FTP_COULDNT_STOR_FILE => 'FTP_COULDNT_STOR_FILE', CURLE_READ_ERROR => 'READ_ERROR', CURLE_OUT_OF_MEMORY => 'OUT_OF_MEMORY', CURLE_OPERATION_TIMEOUTED => 'OPERATION_TIMEOUTED', CURLE_FTP_COULDNT_SET_ASCII => 'FTP_COULDNT_SET_ASCII', CURLE_FTP_PORT_FAILED => 'FTP_PORT_FAILED', CURLE_FTP_COULDNT_USE_REST => 'FTP_COULDNT_USE_REST', CURLE_FTP_COULDNT_GET_SIZE => 'FTP_COULDNT_GET_SIZE', CURLE_HTTP_RANGE_ERROR => 'HTTP_RANGE_ERROR', CURLE_HTTP_POST_ERROR => 'HTTP_POST_ERROR', CURLE_SSL_CONNECT_ERROR => 'SSL_CONNECT_ERROR', CURLE_FTP_BAD_DOWNLOAD_RESUME => 'FTP_BAD_DOWNLOAD_RESUME', CURLE_FILE_COULDNT_READ_FILE => 'FILE_COULDNT_READ_FILE', CURLE_LDAP_CANNOT_BIND => 'LDAP_CANNOT_BIND', CURLE_LDAP_SEARCH_FAILED => 'LDAP_SEARCH_FAILED', CURLE_LIBRARY_NOT_FOUND => 'LIBRARY_NOT_FOUND', CURLE_FUNCTION_NOT_FOUND => 'FUNCTION_NOT_FOUND', CURLE_ABORTED_BY_CALLBACK => 'ABORTED_BY_CALLBACK', CURLE_BAD_FUNCTION_ARGUMENT => 'BAD_FUNCTION_ARGUMENT', CURLE_BAD_CALLING_ORDER => 'BAD_CALLING_ORDER', CURLE_HTTP_PORT_FAILED => 'HTTP_PORT_FAILED', CURLE_BAD_PASSWORD_ENTERED => 'BAD_PASSWORD_ENTERED', CURLE_TOO_MANY_REDIRECTS => 'TOO_MANY_REDIRECTS', CURLE_UNKNOWN_TELNET_OPTION => 'UNKNOWN_TELNET_OPTION', CURLE_TELNET_OPTION_SYNTAX => 'TELNET_OPTION_SYNTAX', CURLE_OBSOLETE => 'OBSOLETE', CURLE_SSL_PEER_CERTIFICATE => 'SSL_PEER_CERTIFICATE', CURLE_GOT_NOTHING => 'GOT_NOTHING', CURLE_SSL_ENGINE_NOTFOUND => 'SSL_ENGINE_NOTFOUND', CURLE_SSL_ENGINE_SETFAILED => 'SSL_ENGINE_SETFAILED', CURLE_SEND_ERROR => 'SEND_ERROR', CURLE_RECV_ERROR => 'RECV_ERROR', CURLE_SHARE_IN_USE => 'SHARE_IN_USE', CURLE_SSL_CERTPROBLEM => 'SSL_CERTPROBLEM', CURLE_SSL_CIPHER => 'SSL_CIPHER', CURLE_SSL_CACERT => 'SSL_CACERT', CURLE_BAD_CONTENT_ENCODING => 'BAD_CONTENT_ENCODING', CURLE_LDAP_INVALID_URL => 'LDAP_INVALID_URL', CURLE_FILESIZE_EXCEEDED => 'FILESIZE_EXCEEDED', CURLE_FTP_SSL_FAILED => 'FTP_SSL_FAILED', CURLE_SSH => 'SSH'
  204.     ];
  205. }
  206. ?>
Sao chép mã

4. Upload to PHP supported hosting server.
Download with command:
php instagram_dl.php <username>
5. Backup your instagram photos to the cloud.
Bạn phải đăng nhập mới được đăng bài Đăng nhập | Register

Quy tắc điểm

Đóng

RecommendTrước /1 Sau

Phòng tối|iPhone|Archiver|G+|Youtube|Facebook|Twitter|Contact|Netdepviet.org

GMT+7, 23-9-2017 03:39 AM , Processed in 0.072992 second(s), 20 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

Trả lời nhanh Lên trên Trở lại danh sách