Search code examples
jsonregexwordpressregex-lookaroundswordpress-rest-api

Extract Image url from WordPress REST API JSON response


I'm new to WordPress REST API and I don't know how to extract the image URLs from the "content" key of the API (https://www.example.com/wp-json/wp/v2/posts) response which looks like this:

"content": {
"rendered": "\n<p>From munching on a casual pizza to romantic candlelit dinner in Kolkata with your better half, Valentine&#8217;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&#8217;s very best restaurants with your other half.</p>\n\n\n\n<p>Find your dream date night in our Valentine&#8217;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 &amp; 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."
}
  1. I tried this solution, but the "featured_media" is 0 in my case.
  2. Installed this plugin, but the field "better_featured_image" is showing as null in all the posts.
  3. This is the only solution that might solve the problem, but I don't know how to extract Image URLs using Regex.

Please help me with the Regex to extract the URLs. Or if there's another way which might help, please mention in the ans.


Solution

  • You can decode json string usgin json_decode() fucntion. then you can preg_match to match with '/src="([^"]*)"/' regex that extract your src. check below code.

    <?php 
    $image = '{"content": {"rendered": "\n<p>From munching on a casual pizza to romantic candlelit dinner in Kolkata with your better half, Valentine&#8217;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&#8217;s very best restaurants with your other half.</p>\n\n\n\n<p>Find your dream date night in our Valentine&#8217;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 &amp; 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;
    ?>