2016-03-29 16 views
0

Şu anda makaleleri aramak için NYT News API'yi kullanıyorum. JSON'un Makale Adı ve URL'sini almaya çalışıyorum. JSON'daki her giriş için bunu nasıl yapabilirim?NYT API'sı Kullanarak JSON'a Erişme

$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); 

Büyük bir JSON döndüren, ben weburl ve başlık alanı alınırken ilgileniyorum. Ben denedim:

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

ve

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

ancak başarılı

. Düşüncesi olan var mı?

JSON'da web_url ve manşetine sahip tüm girişlere de ihtiyacım var.

JSON yanıt gelen bir tek madde girişi:

{ 
    "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." 
} 
+0

Mesaj elde edilen dizi $ y, json – Mihai

+1

o [0] [ 'web_url değeri'] 've böylece en fazla 9 – Mihai

+0

için '$ y [ 'yanıtı'] [ 'dokümanlar'] olduğunu düşünüyorum @ steppermotor Jsonunuzu tek bir ürüne/girdiye indirdim ve bazı kod formatlarını ekledim. Takip ettiğiniz anahtarlar/değerler hala orada. – jDo

cevap

0
JSON Json Weburl veri hattı kullanılarak erişilebilir

: $ y [ 'yanıtı'] [ 'dokümanlar'] [0] [ 'web_url']

0, birden çok web_url girdisini almak ve bir for döngüsüne yerleştirmek için i gibi bir dizin değişkeni ile değiştirilebilir.

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