Digital Guide: How to Update API

This modification contains modified API files for Nextpost incl. custom improvement and fixes.

Changelog in the bottom of the page.

Instruction

Suggestion: use Microsoft Visual Studio Code for code editing and profiling. All your syntax error will be highlighted.

Before we start making changes to the code, I strongly urge you to backup the source files so that you do not have any problems if something goes wrong. The list of files that we have to change:

/app/vendor/mgp25/instagram-php

Step 1

To begin with, you’ll have to check the Ubuntu, PHP and CURL versions that’s currently installed in your system by typing the following commands:

lsb_release -a
php -v
curl -V

Minimum server requirements:

  • Ubuntu 20.4
  • PHP v.7.4
  • CURL v.7.73

1.1. Ubuntu 20.04 is required for the correct work of Zstd compression in API.

IMPORTANT: Create a full server back-up before update!

Upgrade Ubuntu 18.04 to 20.04 LTS using command line:

https://www.cyberciti.biz/faq/upgrade-ubuntu-18-04-to-20-04-lts-using-command-line/

Notice that you cannot run both Apache and Nginx on the same port.

Don’t forget to disable old PHP versions and keep working only PHP 7.4. It’s important to not running more than one PHP version in the same moment.

You’ll need to disable Apache webserver using command line:

sudo systemctl disable --now apache2

1.2. Curl update.

Upgrade Curl to 7.73 using command line:

apt-get update
apt-get install build-essential libcurl4 openssl libssl-dev libssh-dev zlib1g-dev zlib1g libbrotli-dev brotli libkrb5-dev libldap2-dev librtmp-dev libpsl-dev libnghttp2-dev zstd libzstd-dev
wget https://curl.haxx.se/download/curl-7.73.0.tar.gz
tar -xvf curl-7.73.0.tar.gz && cd curl-7.73.0
./configure --with-libzstd --with-libssh2 --with-libmetalink --with-ngtcp2; make && make install
sudo ldconfig

1.3. Zstd extension for PHP.

Installation of Zstd Extension for PHP is required. Without that PHP extension API will not work correctly.

git clone --recursive --depth=1 https://github.com/kjdev/php-ext-zstd.git
cd php-ext-zstd
phpize
./configure
make
make install
cd modules
cp zstd.so /usr/local/

Open php.ini and add this line anywhere after “[PHP]”:

extension = "/usr/local/zstd.so"

Then just save the file and restart server using:

sudo service nginx restart
sudo service php7.4-fpm restart

php7.x-fpm – depends on which php you use.

To check if everything is working create a file called phpinfo.php into your root web directory and put these lines of code:

<?php phpinfo(); ?>

And then visit http://yoursite.com/phpinfo.php and search “zstd”.

If that is installed correctly, you will see the latest version is showing up.

1.4. API files update.

  • Unzip file api-files (Android only).zip, which attached to your order in Dashboard.
  • Replace /app/vendor/mgp25/instagram-php folder with the folder from api-files (Android only).zip.

Step 2. Compatibility with Nextpost Tag User Module (by Nextpass. io).

Open InstagramController.php file (/app/controllers/InstagramController.php) and find the string with:

if (!empty($tags)) {
    $metadata['usertags'] = $tags;
}

Change previous code to this:

if (!empty($tags)) {
    $metadata['usertags'] = ['in' => $tags];
}

Step 3. Hashtag and Location targeting issues in modules.

In every module change previous hashtag and location functions and parameters to the newest.

This functions most time located in

  • /module-name/module-name.php
  • /module-name/cron.php

Latest fixes, suggestions, and updates on our Telegram channel. Use tag #module_outdated_requests channel search.

Hashtag feed

As a pagination fix, I mean my previous fix for the scrolling process in modules. To find this fix use this keyword in search of the group “max id”, “pagination”.

https://www.facebook.com/notes/nextpost-developers-worldwide/updated-bugfix-auto-like-couldnt-find-the-new-media-to-like/1445462705585511/

Step 3.1. New hashtag feed request.

OLD #1 (with pagination)

$feed = $Instagram->hashtag->getFeed(str_replace("#", "", $target->id), $rank_token, $maxId);

NEW #1

$feed = $Instagram->hashtag->getSection(str_replace("#", "", $target->id), $rank_token, 'top', null, $maxId);

OLD #2 (without pagination)

$feed = $Instagram->hashtag->getFeed(str_replace("#", "", $target->id), $rank_token);

NEW #2

$feed = $Instagram->hashtag->getSection(str_replace("#", "", $target->id), $rank_token, 'top', null);

Step 3.2. New hashtag feed processing.

In the reason that hashtag feed changed to sections you should update your feed processing requests also.

Find that code:

$items = $feed->getItems();

or 

$items = array_merge($feed->getRankedItems(), $feed->getItems());

Replace him with that:

// Fix for hashtag processing (March, 2020)
$section_items = [];
$section_items_counter = 0;
$sections = $feed->getSections();
foreach ($sections as $section) {
    if ($section->getLayoutType() == "media_grid") {
        $medias = $section->getLayoutContent()->getMedias();
        foreach ($medias as $m) {
            array_push($section_items, $m->getMedia());
            $section_items_counter++;
        }
    }
}
$feed->setItems($section_items);
$feed->setNumResults($section_items_counter);

$items = $feed->getItems();

Step 3.3. New location feed request.

OLD #1 (with pagination)

$feed = $Instagram->location->getFeed($target->id, $rank_token, $maxId);

NEW #1

‘ranked’ – section tab for locations. Values: “ranked” and “recent”.

$feed = $Instagram->location->getFeed($target->id, $rank_token, 'ranked', null, null, $maxId);

OLD #2 (without pagination)

$feed = $Instagram->location->getFeed($target->id, $rank_token);

NEW #2

‘ranked’ – section tab for locations. Values: “ranked” and “recent”.

$feed = $Instagram->location->getFeed($target->id, $rank_token, 'ranked', null, null);

Step 3.4. New location feed processing.

In the reason that hashtag feed changed to sections you should update your feed processing requests also.

Find that code:

$items = $feed->getItems();

or 

$items = array_merge($feed->getRankedItems(), $feed->getItems());

Replace him with that:

// Fix for location processing (March, 2020)
$section_items = [];
$section_items_counter = 0;
$sections = $feed->getSections();
foreach ($sections as $section) {
    if ($section->getLayoutType() == "media_grid") {
        $medias = $section->getLayoutContent()->getMedias();
        foreach ($medias as $m) {
            array_push($section_items, $m->getMedia());
            $section_items_counter++;
        }
    }
}
$feed = null;
unset($feed);
$feed = new \InstagramAPI\Response\TagFeedResponse();
$feed->setItems($section_items);
$feed->setNumResults($section_items_counter);

$items = $feed->getItems();

Additional fixes

Boost module newest like request.

More requests you can found in the Telegram channel with tag #module_outdated_requests.

Step 4. 2FA support for Nextpost (optional)

This step will work only if you are installed before our mod Bugfix: Two-factor authentication (2FA) for Instagram accounts.

Step 4.1. Open file AccountController.php or PMAccountController.php (if you are using the Proxy Manager module) and find that code in  protected function handleChallengeException():

$challenge_resp = $Instagram->sendChallangeCode($api_path, $choice);

Replace with that:

$challenge_resp = $Instagram->sendChallangeCode($api_path, $choice);

if (is_string($challenge_resp)) {
    $challenge_resp = json_decode($challenge_resp);
}

Step 4.2. Find that code in protected function twofa().

} catch (\InstagramAPI\Exception\ChallengeRequiredException $e) {

Replace with that:

If you are NOT using Proxy Manager module:

} catch (\InstagramAPI\Exception\ChallengeRequiredException $e) {
            $this->username = $username;
            $this->password = $password;
            $this->handleChallengeException($Instagram, $e);
            // Output result
            $this->jsonecho();

If you are USING Proxy Manager module:

} catch (\InstagramAPI\Exception\ChallengeRequiredException $e) {
            $this->username = $username;
            $this->password = $password;
            $this->proxy = $proxy ? $proxy : null;
            $this->system_proxy = $system_proxy ? $system_proxy : null;
            $this->is_allocated = $is_allocated ? $is_allocated : null;
            $this->handleChallengeException($Instagram, $e);
            // Output result
            $this->jsonecho();

That’s all.

Thank you!