Bolandish / PHP-Instagram-Grabber

A workaround for the new Instagram policy to get images by hashtag and user id. No need for accesstoken :)
151 stars 39 forks source link

getMediaByHashtag returns null #41

Open cbenjamin opened 6 years ago

cbenjamin commented 6 years ago

Using the same example from the docs

Bolandish\Instagram::getMediaByHashtag("nofilter", 10);

Response is always null. Every other function works well except for getMediaByHashtag. Am I missing something?

scottyzen commented 6 years ago

@cbenjamin same problem. I think Instagram are after changing something on their side again. Anyone have a way around?

max-kk commented 6 years ago

Hi guys,

I see that https://www.instagram.com/explore/tags/best_photos/?__a=1 is working, so nonthig changed in Instagram API.

max-kk commented 6 years ago

My current code:

    /**
     * @param null $hashtag
     *
     * @return array|bool
     * @since 1.01 (CORE)
     */
    public static function getSimpleMediaByHashtag($hashtag = null)
    {
        // https://www.instagram.com/graphql/query/?query_id=17882293912014529&tag_name=best_photo&first=9&after=J0HWVISlQAAAF0HWVCuAwAAAFjwA

        if (empty($hashtag) || !is_string($hashtag)) {
            return false;
        }

        $random = self::generateRandomString();

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, sprintf("https://www.instagram.com/explore/tags/%s/?__a=1", $hashtag));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        $headers = array();
        $headers[] = "Cookie:  csrftoken=$random;";
        $headers[] = "X-Csrftoken: $random";
        $headers[] = "Referer: https://www.instagram.com/";
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $response = curl_exec($ch);
        curl_close($ch);

        //die();
        //var_dump($response);

        if ($response) {
            $media = json_decode($response, false);
        } else {
            return false;
        }

        if (!isset($media->tag->media->nodes)) {
            fv_log("Instagram :: Simple request nodes is empty!", $media);
            return false;
        }

        return array('all_media' => (array)$media->tag->media->nodes, 'page_info' => (object)array('has_next_page' => false, 'end_cursor' => '',));
    }
scottyzen commented 6 years ago

Thank you @max-kk. I got it working again.

Alexisgt01 commented 5 years ago

great thank you @max-kk ! On Laravel The function fv_log is not declared , it works with die and debug function but not fv_log ?

Alexisgt01 commented 5 years ago

`public static function getSimpleMediaByHashtag($hashtag = null) { if (empty($hashtag) || !is_string($hashtag)) { return false; }

       $random = self::generateRandomString();

       $ch = curl_init();
       curl_setopt($ch, CURLOPT_URL, sprintf("https://www.instagram.com/explore/tags/%s/?__a=1", $hashtag));
       curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
       $headers = array();
       $headers[] = "Cookie:  csrftoken=$random;";
       $headers[] = "X-Csrftoken: $random";
       $headers[] = "Referer: https://www.instagram.com/";
       curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
       $response = curl_exec($ch);
       curl_close($ch);

       //die();
       //var_dump($response);

       if ($response) {
           $media = json_decode($response, true);
       } else {
           return false;
       }

       return $media['graphql']['hashtag']['edge_hashtag_to_media']['edges'];
   }`

work for me