简体   繁体   English

如何从PHP中的知识图api中仅提取文章正文?

[英]How to extract only article body from knowledge graph api in php?

I am trying to get only the article body from json-ld in php but i can't understand how. 我正在尝试仅从php中的json-ld获取文章正文,但我不知道如何。

I'm not too familiar with encoding and decoding json from php, so nothing seems to work. 我对从php编码和解码json不太熟悉,因此似乎没有任何效果。

  "@context": {
    "@vocab": "http://schema.org/",
    "goog": "http://schema.googleapis.com/",
    "resultScore": "goog:resultScore",
    "detailedDescription": "goog:detailedDescription",
    "EntitySearchResult": "goog:EntitySearchResult",
    "kg": "http://g.co/kg"
  },
  "@type": "ItemList",
  "itemListElement": [
    {
      "@type": "EntitySearchResult",
      "result": {
        "@id": "kg:/m/0dl567",
        "name": "Taylor Swift",
        "@type": [
          "Thing",
          "Person"
        ],
        "description": "Singer-songwriter",
        "image": {
          "contentUrl": "https://t1.gstatic.com/images?q=tbn:ANd9GcQmVDAhjhWnN2OWys2ZMO3PGAhupp5tN2LwF_BJmiHgi19hf8Ku",
          "url": "https://en.wikipedia.org/wiki/Taylor_Swift",
          "license": "http://creativecommons.org/licenses/by-sa/2.0"
        },
        "detailedDescription": {
          "articleBody": "Taylor Alison Swift is an American singer-songwriter and actress. Raised in Wyomissing, Pennsylvania, she moved to Nashville, Tennessee, at the age of 14 to pursue a career in country music. ",
          "url": "http://en.wikipedia.org/wiki/Taylor_Swift",
          "license": "https://en.wikipedia.org/wiki/Wikipedia:Text_of_Creative_Commons_Attribution-ShareAlike_3.0_Unported_License"
        },
        "url": "http://taylorswift.com/"
      },
      "resultScore": 896.576599
    }
  ]
}

I only need the article body which is "Taylor Alison Swift is an American singer-songwriter and actress...". 我只需要这样的文章正文:“泰勒·艾莉森·斯威夫特(Taylor Alison Swift)是美国歌手兼作曲家和女演员……”。 How do i achieve this? 我该如何实现?

You have to decode this string with json_decode then it's just picking up your needs from an array. 您必须使用json_decode对该字符串进行解码,然后才从数组中获取所需的内容。 Eg 例如

$j = '{"@context": {"@vocab": "http://schema.org/", "goog": "http://schema.googleapis.com/", "resultScore": "goog:resultScore" }}';

$arr = json_decode($j, true);

echo $arr['@context']['goog'];

For articleBody it should be: 对于articleBody ,应为:

$arr['itemListElement'][0]['result']['detailedDescription']['articleBody']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM