download12749 2016-03-29 09:18
浏览 58
已采纳

使用NYT API访问JSON

I'm currently using the NYT News API to search for articles. I'm trying to get the Article Name, and URL from the JSON. How do I do this for each entry in the 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);

It returns a large JSON, I'm interested in retrieving the weburl, and headline field. I've tried:

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

and

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

but with no success. Any thoughts?

I also need all the entries that have a web_url and headline in the JSON.

A single article entry from the JSON response:

{
    "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条回答 默认 最新

  • douraoyw194498 2016-04-27 02:22
    关注

    The Json Weburl data in the JSON can be accessed using the line: $y['response']['docs'][0]['web_url']

    The 0 can be replaced with an index variable such as i to retrieve multiple web_url entries, and placed in a for loop.

    for(i=0;i<3;i++)
    {
    $y['response']['docs'][i]['web_url']
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮