简体   繁体   English

如何使此脚本运行更快

[英]How do i make this script run faster

<?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));
    }

}
?>

This is the code that I used to update my streams list on the database. 这是我用来更新数据库上流列表的代码。 Problem is, the database is getting bigger and bigger, and this code is starting to run slow + take up my bandwidth. 问题是,数据库越来越大,此代码开始运行缓慢+占用了我的带宽。 This script automatically runs every 3 minutes, and sometimes it doesn't update. 该脚本每3分钟自动运行一次,有时不会更新。 So any suggestions to this code? 那么对此代码有什么建议吗? So it can run faster, and more efficiently? 这样它可以运行得更快,更高效吗?

I see 2 Problems : 我看到2个问题:

1 database access 1个数据库访问

I think you should use mysqli_multi_query instead of doing one query by loop. 我认为您应该使用mysqli_multi_query而不是一个循环地进行查询。

2 HTTP request -file_get_content- 2 HTTP请求-file_get_content-

You can make only one call using this 您只能使用此功能拨打一个电话

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

Like that you can get all the datas for all the ids at once. 这样,您可以一次获取所有id的所有数据。

then you parse the array into the loop. 然后将数组解析为循环。

try this --> https://api.twitch.tv/kraken/streams/?client_ids=2,3,4 Yes, it's working 试试这个-> 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 */}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM