Curl api on for loop doesn't work


#1

From my online list:

include("channels_list.php");

// play call API 

for($i=1; $i <= $q; $i++){
$forse = 'channels' . $i;
if($forse != NULL){
$callAPI = implode('&user_login=', ${$forse});
$url="https://api.twitch.tv/helix/streams?user_login=" . $callAPI;
$ch = curl_init();
  
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  
    'Accept: application/vnd.twitchtv.v5+json',
    'Client-ID: c533u8ntdp29h42xxzwfe0pf5v66dm'
  
));

$forse2 = "result" .$i;
${$forse2} = curl_exec($ch);
curl_close($ch);

// play print API
$str = json_decode(${$forse2}, true);
} else {
    echo 'nothing';
}
// end call API
}

From channels_list.php

$channels1 = array();
$channels2 = array();
$channels3 = array();
$channels4 = array();
$channels5 = array();

  $canale = "SELECT * FROM `ttp_tsp` ORDER BY id ASC LIMIT 0, 100";
  $result = mysqli_query($connString, $canale)or die(mysqli_error());
  if (mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_assoc($result)){
  $channels1[] = $row['canale'];
}
  }

  $canale = "SELECT * FROM `ttp_tsp` ORDER BY id ASC LIMIT 100, 100";
  $result = mysqli_query($connString, $canale)or die(mysqli_error());
  if (mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_assoc($result)){
 $channels2[] = $row['canale'];
}
  }

$canale = "SELECT * FROM `ttp_tsp` ORDER BY id ASC LIMIT 200, 100";
$result = mysqli_query($connString, $canale)or die(mysqli_error());
if (mysqli_num_rows($result) > 0){
    while($row = mysqli_fetch_assoc($result)){
 $channels3[] = $row['canale'];
}
  }

$canale = "SELECT * FROM `ttp_tsp` ORDER BY id ASC LIMIT 300, 100";
$result = mysqli_query($connString, $canale)or die(mysqli_error());
if (mysqli_num_rows($result) > 0){
    while($row = mysqli_fetch_assoc($result)){
 $channels4[] = $row['canale'];
}
  }

$canale = "SELECT * FROM `ttp_tsp` ORDER BY id ASC LIMIT 400, 100";
$result = mysqli_query($connString, $canale)or die(mysqli_error());
if (mysqli_num_rows($result) > 0){
    while($row = mysqli_fetch_assoc($result)){
 $channels5[] = $row['canale'];
}
  }

if($channels5 != NULL){
$q = 5;
}elseif($channels4 != NULL){
$q = 4;
}elseif($channels3 != NULL){
$q = 3;
}elseif($channels2 != NULL){
$q = 2;
}elseif($channels1 != NULL){
$q = 1;
}

The problem is the curl, it should repeat five times but it call only last $channels if $channels is not null.
What should i do for repeat five time curl calling five different channels array?


#2

Your PHP is bad for want of better wording.

Try something more akin to

include("channels_list.php");

// play call API 

foreach ($channels as $chans) {
    $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);

  $str = json_decode($result, true);
  if ($i['http_code'] == 200) {
    // play print API
    // do stuff with $str has chnanels
  } else {
    // non 200 do somethign with the error
    // do stuff with $str has error message
  }
}
// end call API

$channels = array(
  array(),
  array(),
  array(),
  array(),
  array()
);

  $canale = "SELECT * FROM `ttp_tsp` ORDER BY id ASC LIMIT 0, 100";
  $result = mysqli_query($connString, $canale)or die(mysqli_error());
  if (mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_assoc($result)){
  $channels[0][] = $row['canale'];
}
  }

  $canale = "SELECT * FROM `ttp_tsp` ORDER BY id ASC LIMIT 100, 100";
  $result = mysqli_query($connString, $canale)or die(mysqli_error());
  if (mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_assoc($result)){
 $channels[1][] = $row['canale'];
}
  }

$canale = "SELECT * FROM `ttp_tsp` ORDER BY id ASC LIMIT 200, 100";
$result = mysqli_query($connString, $canale)or die(mysqli_error());
if (mysqli_num_rows($result) > 0){
    while($row = mysqli_fetch_assoc($result)){
 $channels[2][] = $row['canale'];
}
  }

$canale = "SELECT * FROM `ttp_tsp` ORDER BY id ASC LIMIT 300, 100";
$result = mysqli_query($connString, $canale)or die(mysqli_error());
if (mysqli_num_rows($result) > 0){
    while($row = mysqli_fetch_assoc($result)){
 $channels[3][] = $row['canale'];
}
  }

$canale = "SELECT * FROM `ttp_tsp` ORDER BY id ASC LIMIT 400, 100";
$result = mysqli_query($connString, $canale)or die(mysqli_error());
if (mysqli_num_rows($result) > 0){
    while($row = mysqli_fetch_assoc($result)){
 $channels[4][] = $row['canale'];
}
  }

Also removed the accept header thats not needed with Helix.and added a curl 200 check.


#3

Thanks for your reply.
channels_list.php works great!
But index for channels make a http error 500


#4

Inspect the body of the response for additional information.

Check the URL that is being constructed is the correct/expected/valid URL

I modified your code off the top of my head and didn’t test it myself, since I don’t have your DB structure.


#5

Ok, now work perfectly, but now print all five array if any are null too, this generate error on undefined index for str[‚Äėdata‚Äô].
If i add channel name on all array curl print only last array and not all five.


#6

I can tell that English is not your first language. I don’t quite understand what you have written


#7

yup sorry ahahah i’m italian.
if i have $channels[1] without channels it generate error ‚Äúundefined index‚ÄĚ because curl print only latest $channels ($channels[4] in this case).


#8

Show your code please. But this is erring towards general PHP problems rather than a TwitchAPI problem


#9

channels_list.php

<?php

¬†¬†include_once(‚Äúconnection.php‚ÄĚ);

  $db = new dbObj();

  $connString =  $db->getConnstring();

  

    $channels = array(

      array(),

      array(),

      array(),

      array(),

      array()

    );

¬†¬†$canale¬†=¬†‚ÄúSELECT¬†*¬†FROM¬†ttp_tsp¬†ORDER¬†BY¬†id¬†ASC¬†LIMIT¬†0,¬†100‚ÄĚ;

  $result = mysqli_query($connString, $canale)or die(mysqli_error());

  if (mysqli_num_rows($result) > 0){

    while($row = mysqli_fetch_assoc($result)){

¬†¬†¬†¬†¬†¬†$channels[0][]¬†=¬†$row[‚Äėcanale‚Äô];

    }

  }

¬†¬†$canale¬†=¬†‚ÄúSELECT¬†*¬†FROM¬†ttp_tsp¬†ORDER¬†BY¬†id¬†ASC¬†LIMIT¬†100,¬†100‚ÄĚ;

  $result = mysqli_query($connString, $canale)or die(mysqli_error());

  if (mysqli_num_rows($result) > 0){

    while($row = mysqli_fetch_assoc($result)){

¬†¬†¬†¬†¬†$channels[1][]¬†=¬†$row[‚Äėcanale‚Äô];

    }

  }

¬†¬†¬†¬†$canale¬†=¬†‚ÄúSELECT¬†*¬†FROM¬†ttp_tsp¬†ORDER¬†BY¬†id¬†ASC¬†LIMIT¬†200,¬†100‚ÄĚ;

    $result = mysqli_query($connString, $canale)or die(mysqli_error());

    if (mysqli_num_rows($result) > 0){

        while($row = mysqli_fetch_assoc($result)){

¬†¬†¬†¬†¬†$channels[2][]¬†=¬†$row[‚Äėcanale‚Äô];

    }

  }

¬†¬†¬†¬†$canale¬†=¬†‚ÄúSELECT¬†*¬†FROM¬†ttp_tsp¬†ORDER¬†BY¬†id¬†ASC¬†LIMIT¬†300,¬†100‚ÄĚ;

    $result = mysqli_query($connString, $canale)or die(mysqli_error());

    if (mysqli_num_rows($result) > 0){

        while($row = mysqli_fetch_assoc($result)){

¬†¬†¬†¬†¬†$channels[3][]¬†=¬†$row[‚Äėcanale‚Äô];

    }

  }

¬†¬†¬†¬†$canale¬†=¬†‚ÄúSELECT¬†*¬†FROM¬†ttp_tsp¬†ORDER¬†BY¬†id¬†ASC¬†LIMIT¬†400,¬†100‚ÄĚ;

    $result = mysqli_query($connString, $canale)or die(mysqli_error());

    if (mysqli_num_rows($result) > 0){

        while($row = mysqli_fetch_assoc($result)){

¬†¬†¬†¬†¬†$channels[4][]¬†=¬†$row[‚Äėcanale‚Äô];

    }

  }

?>

connection.php

<?php

Class dbObj{

    / Database connection start /

¬†¬†¬†¬†var¬†$servername¬†=¬†‚Äúlocalhost‚ÄĚ;

¬†¬†¬†¬†var¬†$username¬†=¬†‚Äúusername‚ÄĚ;

¬†¬†¬†¬†var¬†$password¬†=¬†‚Äúpassword‚ÄĚ;

¬†¬†¬†¬†var¬†$dbname¬†=¬†‚Äúdbname‚ÄĚ;

    var $conn;

    function getConnstring() {

        $con = mysqli_connect($this->servername, $this->username, $this->password, $this->dbname) or die("Connection failed: " . mysqli_connect_error());

        / check connection /

        if (mysqli_connect_errno()) {

¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†printf(‚ÄúConnection failed:¬†%s\n‚ÄĚ,¬†mysqli_connect_error());

            exit();

        } else {

            $this->conn = $con;

        }

        return $this->conn;

    }

}

?>

index.php

<?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 

foreach($channels as $chans){

    $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

        // do stuff with $str has chnanels

    } else {

        // non 200 do somethign with the error

        // do stuff with $str has error message

    }

    // fine chiamata API

 // Print test, lo uso solo per testare degli script

  print_r($url);

¬†¬†¬†echo¬†$str[‚Äėdata‚Äô];

}

?>

<div¬†class=‚Äúcenter‚ÄĚ>

¬†¬†¬†¬†<div><center><img¬†src="./img/TPS-logo.webp"¬†alt=‚ÄúLogo¬†TSP‚Ä̬†height=‚Äú150‚Ä̬†width=‚Äú150‚Ä̬†style=‚Äúpadding:¬†13px;‚ÄĚ></center></div>

<?php¬†¬†if(is_array($str[‚Äėdata‚Äô])){¬†?>

¬†¬†¬†¬†<div¬†class=‚Äútwitch-online‚Ä̬†align=‚Äúcenter‚ÄĚ>

    </div>

<?php 

¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†$TopRaid¬†=¬†‚ÄúSELECT¬†canale¬†FROM¬†ttp_tsp¬†WHERE¬†raid=‚ÄėSi‚Äô‚ÄĚ;

                                        $risraid = mysqli_query($connString, $TopRaid)or die(mysqli_error()); 

                                        while($row = mysqli_fetch_array($risraid)){

¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†$canaleraid¬†=¬†$row[‚Äėcanale‚Äô];

                                        }

?>

¬†¬†¬†¬†¬†¬†¬†¬†<div¬†class=‚ÄúdivTable‚ÄĚ>

¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†<div¬†class=‚ÄúdivTableBody‚ÄĚ>

¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†<div¬†class=‚ÄúdivTableRow‚ÄĚ>

¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†<div¬†class=‚ÄúdivTableCell¬†cella1‚Ä̬†style=‚Äúbackground-color:red;‚ÄĚ><h2><b>RAID¬†IN¬†EVIDENZA¬†¬†¬†¬†&emsp;</b>¬†¬†<p¬†hidden¬†id=‚ÄúTopHOST‚ÄĚ>/host¬†<?php¬†echo¬†$canaleraid;¬†?></p><button¬†onclick=‚ÄúcopyToClipboard(‚ÄėTopHOST‚Äô)‚Ä̬†class=‚Äúbutton¬†button_primary‚ÄĚ>Fai¬†da¬†HOST</button>&emsp;<p¬†hidden¬†id=‚ÄúTopRAID‚ÄĚ>/raid¬†<?php¬†echo¬†$canaleraid;¬†?></p><button¬†onclick=‚ÄúcopyToClipboard(‚ÄėTopRAID‚Äô)‚Ä̬†class=‚Äúbutton¬†button_primary‚ÄĚ>Fai¬†il¬†RAID</button></h2></div>

                </div>

¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†<div¬†class=‚ÄúdivTableRow‚ÄĚ>

¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†<div¬†class=‚ÄúdivTableCell‚Ä̬†style=‚Äúwidth:100%;‚ÄĚ>

¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†<div¬†id=‚Äútwitch-embed‚ÄĚ></div>

¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†<script¬†src=‚Äúhttps://embed.twitch.tv/embed/v1.js‚ÄĚ></script>

¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†<script¬†type=‚Äútext/javascript‚ÄĚ>

¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†var¬†embed¬†=¬†new¬†Twitch.Embed(‚Äútwitch-embed‚ÄĚ,¬†{

                                        width: 500,

                                        height: 400,

¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†channel:¬†‚Äú<?php¬†echo¬†$canaleraid;¬†?>‚ÄĚ,

¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†layout:¬†‚Äúvideo‚ÄĚ,

                                        autoplay: false

                                    });

                                      

                                    embed.addEventListener(Twitch.Embed.VIDEO_READY, () => {

                                        var player = embed.getPlayer();

                                        player.play();

                                    });

                                </script> 

                    </div>

                </div>

            </div>

        </div>

    <br><br>

¬†¬†¬†¬†<div¬†id=‚Äúupdate‚ÄĚ>

¬†¬†¬†¬†<div¬†class=‚Äútwitch-online‚Ä̬†align=‚Äúcenter‚ÄĚ>¬†¬†¬†¬†¬†

¬†¬†¬†¬†<h2>CHANNELS LIVE NOW:¬†<?php¬†¬†$online¬†=¬†count($str[‚Äėdata‚Äô]);¬†echo¬†$online;¬†?><br><br></h2>¬†

    </div>

¬†¬†¬†¬†¬†¬†¬†¬†<div¬†class=‚ÄúdivTable‚ÄĚ>

¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†<div¬†class=‚ÄúdivTableBody‚ÄĚ>

¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†<div¬†class=‚ÄúdivTableRow‚ÄĚ>

¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†<div¬†class=‚ÄúdivTableCell¬†cella1‚ÄĚ><h2><b>CHANNEL NAME</b><h2></div>

¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†<div¬†class=‚ÄúdivTableCell¬†cella2‚ÄĚ><h2><b><i¬†class=‚Äúfa¬†fa-users‚Ä̬†aria-hidden=‚Äútrue‚ÄĚ></i></b><h2></div>

¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†<div¬†class=‚ÄúdivTableCell¬†cella3‚ÄĚ><h2><b>ACTIONS</b><h2></div>

                </div>

<?php

foreach($str[‚Äėdata‚Äô]¬†as¬†$mydata){

if($mydata != null) {

¬†¬†$name¬†¬†¬†¬†¬†¬†=¬†$mydata[‚Äėuser_name‚Äô];

¬†¬†$views¬†¬†¬†¬†¬†=¬†$mydata[‚Äėviewer_count‚Äô];

  ?>

¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†<div¬†class=‚ÄúdivTableRow¬†table-row‚ÄĚ>

¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†<div¬†class=‚ÄúdivTableCell‚ÄĚ><?php¬†echo¬†‚Äú<h3>‚Ä̬†.¬†$name¬†."</h3>";¬†?></div>

¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†<div¬†class=‚ÄúdivTableCell‚ÄĚ><?php¬†echo¬†‚Äú‚Ä̬†.¬†$views¬†."";¬†?></div>

¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†¬†<div¬†class=‚ÄúdivTableCell‚ÄĚ><?php¬†echo¬†‚Äú<a¬†href='https://twitch.tv/‚Ä̬†¬†.¬†$name¬†.¬†‚Äú‚Äô¬†target=‚Äô_blank‚Äô¬†><button¬†class=‚Äėbutton¬†button_primary‚Äô>WATCH</button></a>‚ÄĚ;¬†?>&nbsp;&nbsp;<p¬†hidden¬†id="<?php¬†echo¬†$name;¬†?>">/raid¬†<?php¬†echo¬†$name;¬†?></p><button¬†onclick=‚ÄúcopyToClipboard(‚Äô<?php¬†echo¬†$name;¬†?>‚Äô)‚Ä̬†class=‚Äúbutton¬†button_primary‚ÄĚ>RAID</button></div>

                </div>

<?php

}else{

¬†¬†echo¬†‚Äú<p>test</p>‚ÄĚ;

  } } } 

?>

            </div>

        </div>

    </div>

<?php

if($str[‚Äėdata‚Äô]¬†==¬†null¬†or¬†$str[‚Äėdata‚Äô]¬†==¬†‚Äú0‚ÄĚ){

?>

<div¬†id=‚Äútwitch-embed‚Ä̬†class=‚Äútwitch-embed‚ÄĚ>

<?php

echo¬†¬†¬†¬†‚Äú<p><h2>No one in live.</h2></p>‚ÄĚ;

}

?>

</div>

</body>

</html>


#10

This is the error:

https://api.twitch.tv/helix/streams?user_login=
Notice : Undefined index: data in /var/www/prova_curl.php on line 53


#11

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

#12

Okay i’ve applied your corrections.
Continues undaunted not to work the loop :frowning_face:


#13

Only last $channels show


#14

Thats what this bit does here. Which I didn’t change/fix

This block of code uses the data from the last $str that was generate. You havn’t added a iterator for the $all_channels that we added


#15

OH GOD THAT WORK! Thanks :heart_eyes: