<?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. So any suggestions to this code? So it can run faster, and more efficiently?
I see 2 Problems :
1 database access
I think you should use mysqli_multi_query instead of doing one query by loop.
2 HTTP request -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.
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
$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 */}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.