Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
解码多个JSON URL';他正在使用PHP_Php_Mysql_Json_Youtube Api - Fatal编程技术网

解码多个JSON URL';他正在使用PHP

解码多个JSON URL';他正在使用PHP,php,mysql,json,youtube-api,Php,Mysql,Json,Youtube Api,我正在使用YouTube API尝试将来自多个URL的JSON数据放入mySQL数据库。我试图在多个json链接上使用json解码器,目前我的代码删除了第一个数组,并用第二个数组覆盖它。请注意,我不想使用array_merge函数,因为我想使用大约6-7个json URL。如果你可以非常具体,那也太好了,因为我对php非常陌生 $linkone = 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxR

我正在使用YouTube API尝试将来自多个URL的JSON数据放入mySQL数据库。我试图在多个json链接上使用json解码器,目前我的代码删除了第一个数组,并用第二个数组覆盖它。请注意,我不想使用array_merge函数,因为我想使用大约6-7个json URL。如果你可以非常具体,那也太好了,因为我对php非常陌生

$linkone = 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=25&playlistId={playlistID}&key={Key}';
$linktwo = 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=25&playlistId={PlaylistID}&key={Key}';


$content = file_get_contents($linkone);
$content2 = file_get_contents($linktwo);

$json = json_decode($content, true);


foreach($json['items'] as $row)
{
$title = $row['snippet']['title'];
$title = mysql_real_escape_string($title);
$description = $row['snippet']['description'];
$description = mysql_real_escape_string($description);

$publishedAt = $row['snippet']['publishedAt'];
$publishedAt = mysql_real_escape_string($publishedAt);
$high = $row['snippet']['thumbnails']['high']['url'];
$high = mysql_real_escape_string($high);

$sql = "INSERT INTO table(title, description, publishedAt, high) VALUES('".$title."','".$description."','".$publishedAt."','".$high."')";

if(!mysql_query($sql,$conn))
{
die('Error : ' . mysql_error());
}
}

如果可以,你应该这样做。它们不再得到维护,而是在使用。学习替代,并考虑使用PDO,为什么不把你的链接放到一个数组中,然后把你的JSON处理代码放到一个单独的函数中,并为每个URL调用它。“无法使用stdClass类型的对象作为数组。”我猜这与未包含“true”部分有关?是的,添加
true
作为json_decodeO中的第二个参数是的,对不起,我忘了。
$linkone = 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=25&playlistId={playlistID}&key={Key}';
$linktwo = 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=25&playlistId={PlaylistID}&key={Key}';

$jsonArray = array(
  json_decode(file_get_contents($linkone)),
  json_decode(file_get_contents($linktwo))
);

foreach($jsonArray as $json) {
  foreach($json['items'] as $row)
  {
    $title = $row['snippet']['title'];
    $title = mysql_real_escape_string($title);
    $description = $row['snippet']['description'];
    $description = mysql_real_escape_string($description);

    $publishedAt = $row['snippet']['publishedAt'];
    $publishedAt = mysql_real_escape_string($publishedAt);
    $high = $row['snippet']['thumbnails']['high']['url'];
    $high = mysql_real_escape_string($high);

    $sql = "INSERT INTO table(title, description, publishedAt, high)
      VALUES('".$title."','".$description."','".$publishedAt."','".$high."')";

    if(!mysql_query($sql,$conn))
    {
      die('Error : ' . mysql_error());
    }
  }
}