gpt4 book ai didi

php - 如何让这个脚本运行得更快

转载 作者:行者123 更新时间:2023-11-30 00:26:02 25 4
gpt4 key购买 nike

<?php
header('Content-Type: text/html; charset=UTF-8');
$con = mysqli_connect("localhost", "*******", "******", '*****');
$result = mysqli_query($con, "SELECT * FROM mybb_streams");
while ($row = mysqli_fetch_array($result)) {
$json_array = json_decode(file_get_contents('https://api.twitch.tv/kraken/streams/' . strtolower($row['channel']) . '?client_id=' . $clientId), true);
if ($json_array['stream'] != NULL) {
// turn them into variables to prevent outside SQL injection
$displayname = mysqli_real_escape_string($con, $json_array['stream']['channel']['display_name']);
$title = mysqli_real_escape_string($con, $json_array['stream']['channel']['status']);
$game = mysqli_real_escape_string($con, $json_array['stream']['channel']['game']);
$viewers = mysqli_real_escape_string($con, $json_array['stream']['viewers']);
$preview = mysqli_real_escape_string($con, $json_array['stream']['preview']['medium']);
$followers = mysqli_real_escape_string($con, $json_array['stream']['channel']['followers']);
mysqli_query($con, "SET NAMES utf8mb4");
mysqli_query($con, "UPDATE mybb_streams SET `online` = '1', `title` = '$title', `viewers` = '$viewers', `game` = '$game', `preview` = '$preview', `followers` = '$followers' WHERE `channel` = '" . strtolower($row['channel']) . "'") or die("A MySQL error has occurred.<br />Your Query: UPDATE `streams` SET `online` = `1`, `title` = `$title`, `viewers` = `$viewers`, `game` = `$game`, `preview` = `$preview` WHERE channel = '" . strtolower($row['channel']) . "'<br /> Error: (" . mysqli_errno($con) . ") " . mysqli_error($con));
} else {
mysqli_query($con, "UPDATE mybb_streams SET `online` = '0', `viewers` = '0' WHERE `channel` = '" . strtolower($row['channel']) . "'") or die("A MySQL error has occurred.<br />Your Query: UPDATE streams SET `online` = '0', `viewers` = '0' WHERE `channel` = '" . strtolower($row['channel']) . "'<br /> Error: (" . mysqli_errno($con) . ") " . mysqli_error($con));
}

}
?>

这是我用来更新数据库上的流列表的代码。问题是,数据库变得越来越大,这段代码开始运行缓慢+占用我的带宽。该脚本每 3 分钟自动运行一次,有时不会更新。那么对此代码有什么建议吗?那么它可以运行得更快、更高效吗?

最佳答案

我看到 2 个问题:

1 个数据库访问

我认为你应该使用 mysqli_multi_query 而不是通过循环执行一个查询。

2 HTTP 请求 -file_get_content-

您只能使用此功能调用一次电话

https://api.twitch.tv/kraken/streams/' . strtolower($row['channel']) . '?client_ids=' . implode(',',$array_clientId)

这样你就可以一次性获取所有id的所有数据。

然后将数组解析到循环中。

试试这个 --> https://api.twitch.tv/kraken/streams/?client_ids=2,3,4是的,它正在工作

$con    = mysqli_connect("localhost", "*******", "******", '*****');
$result = mysqli_query($con, "SELECT * FROM mybb_streams");
$array_ids;
while ($row = mysqli_fetch_array($result)) {
$array_ids[] = $row;
}
$json_array = json_decode(file_get_contents('https://api.twitch.tv/kraken/streams/' . strtolower($row['channel']) . '?client_ids=' . implode(',',$array_ids)), true);
foreach($json_array as $user){ /* do your stuff */}

关于php - 如何让这个脚本运行得更快,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22876391/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com