'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Accept-Language' => 'en-US', 'User-Agent' => 'Mozilla/5.0 (Windows NT 6.4; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2225.0 Safari/537.36', 'Connection' => 'close' ]; public function __construct($proxy = null, $timeout = null) { if ($proxy !== null) $this->proxy = $proxy; if ($timeout !== null) $this->timeout = $timeout; } public function getHeaders() { return $this->headers; } public function setHeader($key, $value) { $this->headers[$key] = $value; } public function removeHeader($key) { unset($this->headers[$key]); } public function getTimeout() { return $this->timeout; } public function setTimeout($timeout) { $this->timeout = $timeout; } public function getProxy() { return $this->proxy; } public function setProxy($proxy) { $this->proxy = $proxy; } public function getMediaFromHashtag($hashtag) { $response = $this->request('http://instagram.com/explore/tags/' . urlencode($hashtag) . '/'); $sharedData = $this->extractSharedData($response); if (isset($sharedData['entry_data']) && isset($sharedData['entry_data']['TagPage'])) { $photos = [ 'top' => $sharedData['entry_data']['TagPage'][0]['tag']['top_posts']['nodes'], 'recent' => $sharedData['entry_data']['TagPage'][0]['tag']['media']['nodes'] ]; return $photos; } else { file_put_contents('hello.txt', json_encode($http_response_header)); throw new Exception('Couldn\'t get media from #' . $hashtag); } return []; } public function getMedia($mediaCode) { $response = $this->request('https://instagram.com/p/' . $mediaCode . '/'); $sharedData = $this->extractSharedData($response); if (isset($sharedData['entry_data']) && isset($sharedData['entry_data']['PostPage'])) { $media = $sharedData['entry_data']['PostPage'][0]['media']; return $media; } else { throw new Exception('Couldn\'t get media ' . $mediaCode); } return []; } public function getProfile($username) { $response = $this->request('https://instagram.com/' . $username . '/'); $sharedData = $this->extractSharedData($response); if (isset($sharedData['entry_data']) && isset($sharedData['entry_data']['ProfilePage'])) { $profile = $sharedData['entry_data']['ProfilePage'][0]['user']; return $profile; } else { throw new Exception('Couldn\'t get profile ' . $username); } return []; } protected function extractSharedData($response) { $searchFor = '">window._sharedData = '; if (strpos($response, $searchFor) !== false) { $parts = explode($searchFor, $response, 2); if (count($parts) === 2) { $parts = explode(';', $parts[1], 2); if (!empty($parts[0])) { $sharedData = @json_decode($parts[0], true); if ($sharedData) { return $sharedData; } } } } return []; } protected function request($url, $method = 'GET', $data = null) { if ($data !== null) { $header['Content-Type'] = 'application/x-www-form-urlencoded'; } $headers = []; foreach ($this->headers as $key => $value) { $headers[] = $key . ':' . $value; } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_VERBOSE, false); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->timeout); curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout); if ($this->proxy !== null) { $proxyPath = $this->proxy; if (strpos($proxyPath, '@') !== false) { list($proxyAuth, $proxyPath) = explode('@', $proxyPath); curl_setopt($ch, CURLOPT_PROXYPWD, $proxyAuth); } curl_setopt($ch, CURLOPT_PROXY, $proxyPath); if (is_numeric($proxyPath[0])) curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5); } if (strtolower($method) === 'post') { curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); } curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); curl_close($ch); return $response; } } // Create a new scraper $scraper = new InstagramScraper(); try { // Get a list of photos from hashtag $photos = $scraper->getMediaFromHashtag('fashion'); // Get detailed information about the first photo $photo = $scraper->getMedia($photos['recent'][0]['code']); // Get uploader's profile $profile = $scraper->getProfile($photo['owner']['username']); // Show profile's biography echo 'Biography of ' . $profile['username'] . ': ' . $profile['biography']; } catch (Exception $e) { echo 'Error: ' . $e->getMessage(); }