Curl api on for loop doesn't work

Try this instead

<?php ini_set(‘display_errors’, ‘On’);

error_reporting(E_ALL); ?>

<html>

<head>

<script type=“text/javascript” src="./js/jquery-1.10.1.min.js"></script>

<script type=“text/javascript” src="./js/function.js"></script>

<script type=“text/javascript” src=“https://code.jquery.com/jquery-1.12.4.min.js”></script>

<script data-ad-client=“ca-pub-1504328409663488” async src=“https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js”></script>

<link rel=“stylesheet” href="./css/font-awesome/css/font-awesome.min.css">

<link rel=“stylesheet” href="./css/style.css">

</head>

<body>

<?php

include(“lista_canali_prova.php”);

// inizio a chiamare le API 

$all_channels = [];

foreach($channels as $chans){
    if (count($chans) > 0) {
        $callAPI = implode(’&user_login=’, $chans);

        $url=“https://api.twitch.tv/helix/streams?user_login=” . $callAPI;

        $ch = curl_init();

      

        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);

        curl_setopt($ch, CURLOPT_URL,$url);

        curl_setopt($ch, CURLOPT_HTTPHEADER, array(

            ‘Client-ID: c533u8ntdp29h42xxzwfe0pf5v66dm’

      

        ));

        

        $result = curl_exec($ch);

        $i = curl_getinfo($ch);

        curl_close($ch);

        // preparo le stamp per le API

        $str = json_decode($result, true);

        if ($i[‘http_code’] == 200) {

            // play print API
            foreach ($str['data'] as $live_chan) {
                $all_channels[] = $live_chan;
            }
            // do stuff with $str has chnanels

        } else {

            // non 200 do somethign with the error
                        // handle the error HERE
            // do stuff with $str has error message

        }

    // fine chiamata API
    }
}

// SNIP
// AND then iterate all_channels instead for your html
  • Check if channels has any channels to go load,
  • Do your curl loops merge all the response data into one array
  • then iterate that new array in the HTML display
1 Like