使用NYT API访问JSON

时间:2016-03-29 09:18:41

标签: php json

我目前正在使用NYT News API搜索文章。我试图从JSON获取文章名称和URL。如何为JSON中的每个条目执行此操作?

$curl = curl_init();
$call = 'http://api.nytimes.com/svc/search/v2/articlesearch.json?    q=Denmark&begin_date=20040112&end_date=20041212&sort=oldest&api-key=mykey';

curl_setopt_array($curl, array(
    CURLOPT_RETURNTRANSFER => 1,
    CURLOPT_URL => $call,
    CURLOPT_USERAGENT => 'Codular Sample cURL Request'
));
// Send the request & save response to $resp
echo $resp = curl_exec($curl);
curl_close($curl);

//Decode Json
$y = json_decode($resp,true);

它返回一个大型JSON,我对检索weburl和标题字段感兴趣。我试过了:

$url = $y['response']['docs']['web_url'] 

$headline = $y['response']['docs']['headline']

但没有成功。有什么想法吗?

我还需要所有在JSON中都有web_url和标题的条目。

来自JSON响应的单篇文章:

{
    "response": {
        "meta": {
            "hits": 291,
            "time": 102,
            "offset": 0
        },
        "docs": [{
            "web_url": "http://www.nytimes.com/2005/01/14/arts/design/14anti.html",
            "snippet": "On Wednesday, Christie's New York is having what it claims is the first-ever auction devoted solely to Georg Jensen silver, with some 800 objects.",
            "lead_paragraph": "Georg Jensen silver from Denmark has a tactile quality all its own, probably because it is handmade. It gets only better with age. Michael von Essen, the founder and curator of the Georg Jensen Museum in Copenhagen, tried to explain its appeal: ''Once you have touched pieces of Jensen, you want to have them. The silver has a warmth to it, whether the style is 1900, Art Deco or modern.'' Last year, the company Georg Jensen founded celebrated its 100th anniversary. Jensen, who was not a gifted businessman, would probably have been surprised.",
            "abstract": "Wendy Moonan Antiques column profiles Danish silversmith Georg Jensen, whose company celebrated its centennial last year; Michael von Essen, founder and curator of Georg Jensen Museum in Copenhagen, is giving talk about Jensen at Christie's, comments; photo (M)",
            "print_page": "41",
            "blog": [],
            "source": "The New York Times",
            "multimedia": [],
            "headline": {
                "main": "From Denmark, Moonlight's Glow",
                "kicker": "Antiques"
            },
            "keywords": [{
                "name": "persons",
                "value": "JENSEN, GEORG"
            }, {
                "name": "persons",
                "value": "VON ESSEN, MICHAEL"
            }, {
                "name": "organizations",
                "value": "CHRISTIE'S"
            }, {
                "name": "subject",
                "value": "JEWELS AND JEWELRY"
            }, {
                "name": "subject",
                "value": "ANTIQUES"
            }, {
                "name": "subject",
                "value": "AUCTIONS"
            }, {
                "name": "subject",
                "value": "ART"
            }, {
                "name": "subject",
                "value": "SILVER"
            }],
            "pub_date": "2005-01-14T00:00:00Z",
            "document_type": "article",
            "news_desk": "Leisure/Weekend Desk",
            "section_name": "Arts",
            "subsection_name": null,
            "byline": {
                "person": [{
                    "organization": "",
                    "role": "reported",
                    "rank": 1,
                    "firstname": "Wendy",
                    "lastname": "Moonan"
                }],
                "original": "By Wendy Moonan"
            },
            "type_of_material": "News",
            "_id": "4fd2a5708eb7c8105d88d3af",
            "word_count": 1152,
            "slideshow_credits": null

        }]
    },
    "status": "OK",
    "copyright": "Copyright (c) 2013 The New York Times Company. All Rights Reserved."
}

1 个答案:

答案 0 :(得分:0)

可以使用以下行来访问JSON中的Json Weburl数据:$ y ['response'] ['docs'] [0] ['web_url']

可以使用索引变量(例如i)替换0以检索多个web_url条目,并将其置于for循环中。

for(i=0;i<3;i++)
{
$y['response']['docs'][i]['web_url']
}