从 WordPress REST API JSON 响应中提取图像 url
Posted
技术标签:
【中文标题】从 WordPress REST API JSON 响应中提取图像 url【英文标题】:Extract Image url from WordPress REST API JSON response 【发布时间】:2021-03-19 20:14:22 【问题描述】:我是 WordPress REST API 的新手,我不知道如何从 API (https://www.example.com/wp-json/wp/v2/posts) 响应的“内容”键中提取图像 URL,如下所示:
"content":
"rendered": "\n<p>From munching on a casual pizza to romantic candlelit dinner in Kolkata with your better half, Valentine’s week is filled with all of these!</p>\n\n\n\n<p>The love week of February offers the perfect excuse to sample Kolkata’s very best restaurants with your other half.</p>\n\n\n\n<p>Find your dream date night in our Valentine’s Day special guide to the cutest restaurants in Kolkata.</p>\n\n\n\n<h2><i><center>Blue and Beyond</center></i></h2>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img src=\"https://kolkatasutra.com/wp-content/uploads/2021/02/blue-n-beyond.png\" alt=\"\" class=\"wp-image-919\" srcset=\"https://kolkatasutra.com/wp-content/uploads/2021/02/blue-n-beyond.png 540w, https://kolkatasutra.com/wp-content/uploads/2021/02/blue-n-beyond-300x197.png 300w, https://kolkatasutra.com/wp-content/uploads/2021/02/blue-n-beyond-150x99.png 150w\" sizes=\"(max-width: 540px) 100vw, 540px\" /><figcaption>Blue & Beyond, New Market</figcaption></figure></div>\n\n\n\n<p>Situated in the New Market area, this place boasts of beautiful rooftop open-air seating. It also has an indoor seating</p>\n\n\n\n<p>You can expect a feel of colonial Kolkata with Stuart Hogg Market below."
-
我尝试了this 解决方案,但在我的情况下“featured_media”为 0。
已安装this 插件,但“better_featured_image”字段在所有帖子中显示为空。
This 是唯一可能解决问题的解决方案,但我不知道如何使用正则表达式提取图像 URL。
请帮助我使用正则表达式来提取 URL。或者如果有其他可能有帮助的方法,请在答案中提及。
【问题讨论】:
【参考方案1】:你可以解码json字符串usginjson_decode()fucntion。然后你可以 preg_match 匹配 '/src="([^"]*)"/'
正则表达式来提取你的 src。检查下面的代码。
<?php
$image = '"content": "rendered": "\n<p>From munching on a casual pizza to romantic candlelit dinner in Kolkata with your better half, Valentine’s week is filled with all of these!</p>\n\n\n\n<p>The love week of February offers the perfect excuse to sample Kolkata’s very best restaurants with your other half.</p>\n\n\n\n<p>Find your dream date night in our Valentine’s Day special guide to the cutest restaurants in Kolkata.</p>\n\n\n\n<h2><i><center>Blue and Beyond</center></i></h2>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><img src=\"https://kolkatasutra.com/wp-content/uploads/2021/02/blue-n-beyond.png\" alt=\"\" class=\"wp-image-919\" srcset=\"https://kolkatasutra.com/wp-content/uploads/2021/02/blue-n-beyond.png 540w, https://kolkatasutra.com/wp-content/uploads/2021/02/blue-n-beyond-300x197.png 300w, https://kolkatasutra.com/wp-content/uploads/2021/02/blue-n-beyond-150x99.png 150w\" sizes=\"(max-width: 540px) 100vw, 540px\" /><figcaption>Blue & Beyond, New Market</figcaption></figure></div>\n\n\n\n<p>Situated in the New Market area, this place boasts of beautiful rooftop open-air seating. It also has an indoor seating</p>\n\n\n\n<p>You can expect a feel of colonial Kolkata with Stuart Hogg Market below."';
$image = json_decode($image, true);
preg_match('/src="([^"]*)"/',$image['content']['rendered'], $result);
echo $result[1]; die;
?>
【讨论】:
以上是关于从 WordPress REST API JSON 响应中提取图像 url的主要内容,如果未能解决你的问题,请参考以下文章
如何从外部应用程序安全地使用json web令牌,与wordpress rest api对话
Angular JSONP 调用 WordPress JSON REST API
从 WordPress REST API 获取原始 HTML 输出