2016-11-11 33 views
6

To jest mój kod, którego użyłem do uzyskania obrazów hashtag bez API. Nie chcę używać żadnych referencji. Nie trzeba dodawać client_id ani dostępu do tokena. Ale mam tylko 15 zdjęć. Jak mogę uzyskać wszystkie obrazy?Jak uzyskać wszystkie zdjęcia hashtagu na Instagramie bez API?

<div> 

    <form action='#' method='post'> 
    <input type='input' name='txttag' /> 
    <input type='submit' value='Get Image' /> 
    </form> 

    </div> 


    <?php 
    function scrape_insta_hash($tag) { 
     $insta_source = file_get_contents('https://www.instagram.com/explore/tags/'.$tag.'/'); // instagrame tag url 
     $shards = explode('window._sharedData = ', $insta_source); 
     $insta_json = explode(';</script>', $shards[1]); 
     $insta_array = json_decode($insta_json[0], TRUE); 
     return $insta_array; // this return a lot things print it and see what else you need 
    } 

    if(isset($_POST['txttag'])) 
    { 
     $tag =$_POST['txttag']; // tag for which ou want images 
     $results_array = scrape_insta_hash($tag); 
     $limit = 15; // provide the limit thats important because one page only give some images then load more have to be clicked 
     $image_array= array(); // array to store images. 
      for ($i=0; $i < $limit; $i++) { 
       $latest_array = $results_array['entry_data']['TagPage'][0]['tag']['media']['nodes'][$i]; 
       $image_data = '<img src="'.$latest_array['thumbnail_src'].'">'; // thumbnail and same sizes 
       //$image_data = '<img src="'.$latest_array['display_src'].'">'; actual image and different sizes 
       array_push($image_array, $image_data); 
      } 
      foreach ($image_array as $image) { 
       echo $image;// this will echo the images wrap it in div or ul li what ever html structure 
      } 
      //https://www.instagram.com/explore/tags/your-tag-name/ 
    } 
    ?> 



    <style> 
    img { 
     height: 200px; 
     margin: 10px; 
    } 
    </style> 

Odpowiedz

38

Łatwo sposobem jest poprosić o ?__a=1 jak https://www.instagram.com/explore/tags/girls/?__a=1 i odbierać JSON bez parsowania HTML i window._sharedData =

W json widać page_info zakresie z end_cursor:

"page_info": { 
    "has_previous_page": false, 
    "start_cursor": "1381007800712523480", 
    "end_cursor": "J0HWCVx1AAAAF0HWCVxxQAAAFiYA", 
    "has_next_page": true 
}, 

użytku end_cursor, aby poprosić o następną porcję zdjęć:

https://www.instagram.com/explore/tags/girls/?__a=1&max_id=J0HWCVx1AAAAF0HWCVxxQAAAFiYA

UPD:

<?php 

$baseUrl = 'https://www.instagram.com/explore/tags/girls/?__a=1'; 
$url = $baseUrl; 

while(1) { 
    $json = json_decode(file_get_contents($url)); 
    print_r($json->tag->media->nodes); 
    if(!$json->tag->media->page_info->has_next_page) break; 
    $url = $baseUrl.'&max_id='.$json->tag->media->page_info->end_cursor; 
} 
+0

Czy możesz mi powiedzieć jak wydrukować więcej niż 20 zdjęć z kodem. –

+0

@jigsparmar zobacz aktualizację – ilyapt

+1

dziękuję bardzo .. –