0

Im setting up an credit system that checks if the user followed someone that is also on the website on Twitch. Now i've set up a base, what I think it should do the trick.

The thing now is, it checks everytime by cronjob every 10 minutes, but if i do it manually now, it just gives other responses, like the other time it says he followed the other, but if i refresh then it says he didnt follow him.

You think what's the point you only give out the credits once? Yes, that is true, but if it says that they don't follow we rip some credits from the user that unfollowed the other user on Twitch.

I don't know how to get the solid setup with the same results every refresh for example.

$get_streamer = $mysqli->query("SELECT `streamer`, `streamer_id` FROM `users` WHERE `streamer_id` != ''");

while($streamer = $get_streamer->fetch_object()) {

    //Check Affiliate
    $url1 = 'https://api.twitch.tv/helix/users?id='.$streamer->streamer_id;
    $gettwitch1 = json_decode(file_get_contents_curl($url1), true);

    $url2 = 'https://api.twitch.tv/helix/users/follows?from_id='.$gettwitch1['data'][0]['id'];
    $gettwitch2 = json_decode(file_get_contents_curl($url2), true);

    $get_streamer2 = $mysqli->query("SELECT `streamer`, `streamer_id` FROM `users` WHERE `streamer_id` != '".$streamer->streamer_id."' AND `streamer_id` != ''");
    while($streamer2 = $get_streamer2->fetch_object()) {
    $ii = 0;
    for ($i=0, $len=count($gettwitch2['data']); $i < $len; $i++){
        if ($gettwitch2['data'][$i]['to_id'] == $streamer2->streamer_id){
            $ii++;
        }
    }
    if ($ii == 0){
        echo $streamer->streamer." did not follow ".$streamer2->streamer."!<br />";
    } else {
        echo $streamer->streamer. " did follow ".$streamer2->streamer."!<br />";
    }
    }
    echo "<br />";
    echo "<br />";
}
Prizen
  • 11
  • 1
  • 3
  • One question based on above description - Are you adding a new entry in the db table each time a user follows/un-follows other user ? – Kishen Nagaraju Jan 02 '19 at 06:11
  • @KishenNagaraju Good question, they will be aded when the user followed someone, and when they unfollowed eachother the colomn 'followed' will change from 1 to 0. (if you have a better way just tell me.) – Prizen Jan 02 '19 at 06:24
  • No that is the normal way. I just asked because I can't think of any other reason as to why the data changes each time. Perhaps you can log the entries in the cron job as they run and verify whether you are getting the same issue. Just a suggestion from my end. – Kishen Nagaraju Jan 02 '19 at 06:33
  • Just check this and refresh (It isn't in the Database yet) http://viewershub.com/test The results are changing – Prizen Jan 02 '19 at 06:45
  • Yes its not in the database, but you are not comparing the follow logic in the database. If you look at the code above, you are comparing the users in the database with the api response. And there is no use of `$gettwitch1` in your code. That seems to be the problem. – Kishen Nagaraju Jan 02 '19 at 06:50
  • There is use because `$gettwitch1` is being used, because it includes `$url1` that get's info from `$streamer->streamer_id` – Prizen Jan 02 '19 at 06:54
  • Ok. Can you post a sample response from both the apis that you are getting and your database `users` table ? – Kishen Nagaraju Jan 02 '19 at 06:59
  • do ++$ii pre-increment and check again – Shibon Jan 02 '19 at 07:05
  • At where ? @Shibon – Prizen Jan 02 '19 at 07:07
  • $ii++; this to ++$ii; @Prizen – Shibon Jan 02 '19 at 07:09
  • That didn't work @Shibon – Prizen Jan 02 '19 at 07:12

0 Answers0