如何提高此PHP控制器的速度?
如下所示,有一个控制器结构可以从Instagram中获取各种数据,并在提取此数据后计算参与率。尽管该结构在小规模帐户中很快起作用,但在大帐户上,它的效率非常慢且效率低下。我试图通过尝试各种方法(例如使用收益率方法)来加快速度,但是由于我是PHP的新手,我什至不确定是否应该在此代码中使用屈服。您能帮我做什么吗?提前致谢。
这是我的控制器代码
<?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, ',', '.') . '%';
}
}
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, ',', '.') . '%';
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我已经使用 microtime 和 memory_get_usage 之后(或者如果可以的话/code> laravel log)脚本运行,您将能够在脚本完成后查看有关时间,内存和统计数据的一些统计数据,然后更新问题,我们可以考虑下一步
i've made a basic benchmark with microtime and memory_get_usage so after (or while if you can
tail
laravel log) script runs you will be able to see some stats about timings and memory and overal stats after script finish, then update question and we can think about next steps我相信这里的最大问题是您正在执行的HTTP请求的数量。我认为您必须增加每个请求的数量。它将减少HTTP请求,并可能加速您的代码。
您将其设置在
$ count = 12;
i重构您的代码并放置
$ MAXPERPAGE = 50
,并尝试减少一些不必要的计数,并呼吁Method$ response- &gt; json()
。I believe the big problem here is the number of HTTP requests you are doing. I think you must increase the number per request. It will decrease HTTP requests and probably speedup your code.
You set it on
$count = 12;
I refactor your code and put
$maxPerPage = 50
and tried to decrease some unnecessary counting and call for method$response->json()
.