2017-11-16 1 views
0

Ich arbeite derzeit mit NewYorkTimes Nachrichten API. Ich erhalte diese JSON-Antwort. Und jetzt möchte ich es mit PHP durchlaufen und jeden Artikel (Bild, Titel und Auszug) bekommen.Holen Sie sich Daten von New York Times API JSON Antwort

{ 
    "status": "OK", 
    "copyright": "Copyright (c) 2017 The New York Times Company. All Rights Reserved.", 
    "response": { 
    "docs": [ 
     { 
     "web_url": "https://www.nytimes.com/reuters/2017/11/15/business/15reuters-square-bitcoin.html", 
     "snippet": "Payments company Square Inc said it has started allowing select customers to buy and sell bitcoins on its Cash app, as it looks to tap into a craze that has sent the cryptocurrency up nearly sevenfold this year.", 
     "blog": {}, 
     "source": "Reuters", 
     "multimedia": [], 
     "headline": { 
      "main": "Payments Company Square Tests Bitcoin Buying and Selling", 
      "print_headline": "Payments Company Square Tests Bitcoin Buying and Selling" 
     }, 
     "keywords": [], 
     "pub_date": "2017-11-15T18:24:36+0000", 
     "document_type": "article", 
     "new_desk": "None", 
     "byline": { 
      "original": "By REUTERS" 
     }, 
     "type_of_material": "News", 
     "_id": "5a0c866b7c459f246b6349f9", 
     "word_count": 388, 
     "score": 2.5538578, 
     "uri": "nyt://article/1dff4a88-4086-507c-b644-33e0d7d95e28" 
     }, 
     { 
     "web_url": "https://www.nytimes.com/reuters/2017/11/13/business/13reuters-investment-summit-fink-bitcoin.html", 
     "snippet": "Bitcoin, whose value has fluctuated significantly this month, remains a \"speculative\" investment that thrives because of the cryptocurrency's anonymous nature, BlackRock Inc Chief Executive Larry Fink said on Monday.", 
     "blog": {}, 
     "source": "Reuters", 
     "multimedia": [], 
     "headline": { 
      "main": "BlackRock's Fink Says Bitcoin Thrives on Its Anonymity", 
      "print_headline": "BlackRock's Fink Says Bitcoin Thrives on Its Anonymity" 
     }, 
     "keywords": [], 
     "pub_date": "2017-11-13T17:45:33+0000", 
     "document_type": "article", 
     "new_desk": "None", 
     "byline": { 
      "original": "By REUTERS" 
     }, 
     "type_of_material": "News", 
     "_id": "5a09da4a7c459f246b63428d", 
     "word_count": 322, 
     "score": 2.5538578, 
     "uri": "nyt://article/bb396871-c686-5afc-9e71-3f9f5af8dac1" 
     } 
    ], 
    "meta": { 
     "hits": 959, 
     "offset": 0, 
     "time": 7 
    } 
    } 
} 

Zum einen habe ich $arr = json_decode($result); meine Json zu entschlüsseln. Aber wenn ich eine Schleife zu machen versucht, tut es mir etwas zurückgeben

foreach($arr['docs'] as $article){ 
    echo $article['web_url']; 
} 

Bitte helfen Sie mir durch diese JSON Antwort auf Schleife und notwendigen Daten zu erhalten. Vielen Dank!

+1

Es ist kein Array, sondern ein Objekt. Verwenden Sie '$ arr = json_decode ($ result, true);', um ein Array zu erhalten. Und dann sollte es '$ arr ['Antwort'] ['Docs'] 'sein. Oder Schleife über '$ arr-> response-> docs' – jeroen

+0

Ich habe meinen Code geändert, aber es gibt mir immer noch nichts zurück ... –

+0

Sorry hat Ihre aktualisierte Antwort nicht überprüft. Es funktioniert! –

Antwort

1

versuchen diese

$json = json_decode($response, true); 
foreach($json['response']['docs'] as $key => $value) 
{ 
    if(!empty($value['web_url'])) 
    { 
    $WEB= $value['web_url']; 
    $WEB= addslashes($WEB); 
    $WEB= trim(preg_replace('/\s\s+/', ' ', $WEB)); 
    } 
    else 
    { 
    $WEB= ''; 
    } 
    echo 'WEB= '.$WEB.'</br>'; 
} 

nur die foreach sicher wahr ist, kann vielleicht helfen.

Verwandte Themen