As you can see below, there is a controller structure that pulls various data from Instagram and calculates the engagement rate after pulling this data. Although this structure works fast in small-scale accounts, it becomes very slow and inefficient when it comes to large accounts. I tried to speed it up by trying various things, such as using the yield method, but since I'm new to php, I'm not even sure if I should use yield in this code. Could you please help me on what to do? Thanks in advance.
this is my controller code
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Http;
class splenperAPIController extends Controller
{
public function splenperAPI()
{
$maxId = '';
$response = Http::withHeaders([
'cookie' => 'sessionid=1796659686%3A4Ojj1py72bZKql%3A7; csrftoken=fPYrPRD1vHB7LdS0DjKzOK4kGo4uYK9f; ds_user_id=1796659686; ig_did=25114CCD-7A9D-4971-88F1-1E04796D9F14; ig_nrcb=1; mid=YiqZZQALAAEhkff6N5T2oovGOBkz; rur=01f73b7ee19feca3df296fc45ee75179c49dd54efb93233307b58f19bf707fea5d458fe7; shbid=01f7998eadf528233f0e7331b327e5a107134084c64e229af2cc6577b8a2ce862e3798da; shbts=01f7caf607a6877ca8407e9cb85cd175736a87f8f75e9076d5bddb32cbcbac6d5da45e89',
'x-ig-app-id' => '936619743392459',
'Content-Type' => 'application/json',
])->get('https://www.instagram.com/elmaligroup/?__a=1');
$response = $response->json();
$userId = $response['graphql']['user']['id'];
$followersCount = $response['graphql']['user']['edge_followed_by']['count'];
$count = 12;
$index = 0;
$isMoreAvailable = true;
$totalLikeCount = 0;
$totalCommentCount = 0;
while ($index < $count && $isMoreAvailable) {
$variables = json_encode([
'id' => $userId,
"after" => $maxId,
"first" => $count,
]);
$variables = urlencode($variables);
$response = Http::withHeaders([
'cookie' => 'sessionid=1796659686%3A4Ojj1py72bZKql%3A7; csrftoken=fPYrPRD1vHB7LdS0DjKzOK4kGo4uYK9f; ds_user_id=1796659686; ig_did=25114CCD-7A9D-4971-88F1-1E04796D9F14; ig_nrcb=1; mid=YiqZZQALAAEhkff6N5T2oovGOBkz; rur=01f73b7ee19feca3df296fc45ee75179c49dd54efb93233307b58f19bf707fea5d458fe7; shbid=01f7998eadf528233f0e7331b327e5a107134084c64e229af2cc6577b8a2ce862e3798da; shbts=01f7caf607a6877ca8407e9cb85cd175736a87f8f75e9076d5bddb32cbcbac6d5da45e89',
'x-ig-app-id' => '936619743392459',
'Content-Type' => 'application/json',
])->get('https://www.instagram.com/graphql/query/?query_hash=e769aa130647d2354c40ea6a439bfc08&variables=' . $variables);
for ($i = 0; $i < $count; $i++) {
if ($i == count($response->json()['data']['user']['edge_owner_to_timeline_media']['edges'])) {
break;
}
$totalCommentCount += $response->json()['data']['user']['edge_owner_to_timeline_media']['edges'][$i]['node']['edge_media_to_comment']['count'];
$totalLikeCount += $response->json()['data']['user']['edge_owner_to_timeline_media']['edges'][$i]['node']['edge_media_preview_like']['count'];
$userName = $response->json()['data']['user']['edge_owner_to_timeline_media']['edges'][$i]['node']['owner']['username'];
$index++;
}
$maxId = $response->json()['data']['user']['edge_owner_to_timeline_media']['page_info']['end_cursor'];
$isMoreAvailable = $response->json()['data']['user']['edge_owner_to_timeline_media']['page_info']['has_next_page'];
if ($isMoreAvailable) {
$index = 0;
}
}
$mediaCount = $response->json()['data']['user']['edge_owner_to_timeline_media']['count'];
echo $followersCount . '<br>';
echo $totalLikeCount . '<br>';
echo $totalCommentCount . '<br>';
echo ($totalLikeCount + $totalCommentCount) . '<br>';
echo $userName . '<br>';
echo $userId . '<br>';
echo $mediaCount . '<br>';
$percent = ($totalLikeCount + $totalCommentCount) / $followersCount * 100;
echo "engagementRate: " . number_format($percent, 2, ',', '.') . '%';
}
}