首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用PHP解码多个JSON URL

用PHP解码多个JSON URL
EN

Stack Overflow用户
提问于 2015-06-17 23:57:48
回答 1查看 896关注 0票数 0

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

代码语言:javascript
复制
$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());
}
}
EN

回答 1

Stack Overflow用户

发布于 2015-06-18 00:07:36

代码语言:javascript
复制
$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());
    }
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30896646

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档