Php json вставить в MySQL данные и вставить контрольные пределы - PullRequest
0 голосов
/ 15 октября 2018

Эй, этот скрипт работает отлично, когда в json 8 или больше youtube#video, но когда он ниже, чем 8, как 6, я получаю ошибку.мне нужен вывод получить вставку от 0-8 не только 8.

$loop = mysqli_query($conn, "SELECT channelid FROM users ORDER BY id") or die (mysqli_error($conn));

while ($row = mysqli_fetch_array($loop)) {
    $channelid = $row['channelid'];
    $url = 'https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId='.$channelid .'&maxResults=10&key=KEY';
    $content = file_get_contents($url); 
    $json = json_decode($content, true);    

    if(!isset($json['items'])) continue; //skip if no items

    $videos = ['videoId'=>'','videoId1'=>'','videoId2'=>'','videoId3'=>'','videoId4'=>'','videoId5'=>'','videoId6'=>'','videoId7'=>''];
    $videossw = ['vidname'=>'','vidname1'=>'','vidname2'=>'','vidname3'=>'','vidname4'=>'','vidname5'=>'','vidname6'=>'','vidname7'=>''];
    $videoss = ['publishedAt'=>'','publishedAt1'=>''];

    $i = 0;
    //if(isset($videoList[$i]["id"]["videoId"])) {
    foreach($json['items'] as $items) {
        if ($items['id']['kind'] != 'youtube#playlist') {
            $keyy  = 0==$i ? 'publishedAt' : 'publishedAt'.$i;
            $videoss[$keyy] = $items['snippet']['publishedAt'];

            $keyyy  = 0==$i ? 'vidname' : 'vidname'.$i;
            $videossw[$keyyy] = $items['snippet']['title']; 

            $key  = 0==$i ? 'videoId' : 'videoId'.$i;
            $videos[$key] = $items['id']['videoId']; 
            ++$i;
        }
    }
    $qqq = 'INSERT INTO users(channelid, publishedAt, publishedAt1, videoId, videoId1, videoId2, videoId3, videoId4, videoId5, videoId6, videoId7, vidname, vidname1, vidname2, vidname3, vidname4, vidname5, vidname6, vidname7) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE channelid=?, publishedAt=?, publishedAt1=?, videoId=?, videoId1=?, videoId2=?, videoId3=?, videoId4=?, videoId5=?, videoId6=?, videoId7=?, vidname=?, vidname1=?, vidname2=?, vidname3=?, vidname4=?, vidname5=?, vidname6=?, vidname7=?';
    $stmt = $conn->prepare($qqq);
    $stmt->bind_param('ssssssssssssssssssssssssssssssssssssss', $channelid, $videoss['publishedAt'], $videoss['publishedAt1'], $videos['videoId'], $videos['videoId1'], $videos['videoId2'], $videos['videoId3'], $videos['videoId4'], $videos['videoId5'], $videos['videoId6'], $videos['videoId7'], $videossw['vidname'], $videossw['vidname1'], $videossw['vidname2'], $videossw['vidname3'], $videossw['vidname4'], $videossw['vidname5'], $videossw['vidname6'], $videossw['vidname7'], $channelid, $videoss['publishedAt'], $videoss['publishedAt1'], $videos['videoId'], $videos['videoId1'], $videos['videoId2'], $videos['videoId3'], $videos['videoId4'], $videos['videoId5'], $videos['videoId6'], $videos['videoId7'], $videossw['vidname'], $videossw['vidname1'], $videossw['vidname2'], $videossw['vidname3'], $videossw['vidname4'], $videossw['vidname5'], $videossw['vidname6'], $videossw['vidname7']);
    $stmt->execute();
}

var_dump

array(2) {
  ["kind"]=>
  string(13) "youtube#video"
  ["videoId"]=>
  string(11) "ElNBG63UQ0g"
}
array(2) {
  ["kind"]=>
  string(13) "youtube#video"
  ["videoId"]=>
  string(11) "e3PlqifTrS0"
}
<br />
<b>Notice</b>:  Undefined index: videoId in <b>/index.php</b> on line <b>63</b><br />
array(2) {
  ["kind"]=>
  string(15) "youtube#channel"
  ["channelId"]=>
  string(24) "UCynfZM0Edr9cA4pDymb2rEA"
}

ON LINE $videos[$key] = $items['id']['videoId'];

Я получаю ошибку:

Примечание: неопределенный индекс: videoId

...