php - 如何访问JSON对象并插入数据库

标签 php mysql json

我想访问一种与我通常得到的不同的 JSON 格式。

通常我会得到如下所示的 JSON:

{
kind: "youtube#searchListResponse",
etag: ""stbstbsts"",
nextPageToken: "CAUQAA",
pageInfo: {
totalResults: 1000000,
resultsPerPage: 5
},
items: [
{
kind: "youtube#searchResult",
etag: ""pWoriJ5rlTq3315otdsHvMzLHR4"",
id: {
kind: "youtube#channel",
channelId: "UCbFnGyIcAhDrj-90OzvI7kw"
},
snippet: {
publishedAt: "2013-07-11T06:09:13.000Z",
channelId: "UCbFnGyIcAhDrj-90OzvI7kw",
title: "Oh My Disney",
description: "Videos for Disney lovers, by Disney lovers.",
thumbnails: {
default: {
url: "https://yt3.ggpht.com/-RpvvvIX2cL8/AAAAAAAAAAI/AAAAAAAAAAA/22QzVC_ihE4/s512-c-k-no/photo.jpg"
},
medium: {
url: "https://yt3.ggpht.com/-RpvvvIX2cL8/AAAAAAAAAAI/AAAAAAAAAAA/22QzVC_ihE4/s512-c-k-no/photo.jpg"
},
high: {
url: "https://yt3.ggpht.com/-RpvvvIX2cL8/AAAAAAAAAAI/AAAAAAAAAAA/22QzVC_ihE4/s512-c-k-no/photo.jpg"
}
},
channelTitle: "OhMyDisney",
liveBroadcastContent: "none"
}
},
{
kind: "youtube#searchResult",
etag: ""u4/3yClYSRlqGt3fuhXIOAwxFZWxB4"",
id: {
kind: "youtube#video",
videoId: "y8tDrJj-uyQ"
},
...

并使用

$arr = array();
foreach (json_decode($response) as $item){
  $post = $item->snippet->description;

  $arr[] = array(
           "post" => htmlspecialchars($post),
           );
/* script to insert database here */
}

从上面的 JSON 输出我可以访问它并将它插入到数据库中。 现在,我们来回答我的问题。我有这个代码。

我的代码:

$appear = "http://suggestqueries.google.com/complete/search?client=firefox&q=Disney";
function get_curl($url) {
    if(function_exists('curl_init')) {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 
        $output = curl_exec($ch);
        echo curl_error($ch);
        curl_close($ch);
        return $output;
    } else {
        return file_get_contents($url);
    }
}
$response = get_curl($appear);

输出:

[
  "disney",
  [
     "disney store",
     "disney",
     "disney world",
     "disneyland",
     "disney cruise",
     "disney movies",
     "disney channel",
     "disney games",
     "disneyland paris",
     "disney on ice"
  ]
]

目标:

"disney store" -> insert this word to the db
"disney"       -> insert this word to the db 
"disney world" -> insert this word to the db

怎么做?

最佳答案

这些是你要找的值,我留给你添加数据库INSERT/UPDATE代码

echo $response['disney']['disney store'];
echo $response['disney']['disney'];
echo $response['disney']['disney world'];

关于php - 如何访问JSON对象并插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35006376/

相关文章:

php - 如何使用 PHP 和 Javascript 将数据从一个页面传递到另一个页面?

javascript - 如何隐藏背景 HDR 贴图的显示,但保存 hdri 在 Three.js 中对光照所做的工作?

php - ajax在php中搜索多表连接

c# - 如何在同一行中找到重复的名称/值?可以算吗?

php - 从辅助类将输出写入控制台

php - GROUP BY 消息 MySQL

MySQL 将48小时转换为48 :00:00

ruby-on-rails - 在 Rails 中渲染 JSON 时包含关联模型

json - package.json 必须是实际的 JSON,而不仅仅是 JavaScript

java - 禁用每个 ObjectMapper 的字段序列化