Displaying wordpress posts with curl

0

there are some sites where the installation of wordpress is in a subfolder called news and I need to display a list of posts in the index, searching the internet I saw that wordpress generated a JSON of the posts. So by giving one more lookup I get to display, however the method I'm using is via PHP's " file_get_contents ", there every time I have to log in to CPANEL and enable an option there, due to issues of security.

That's when a guy I had contact told me to use CURL , here comes the question, how do I get the pots and featured image?

Below I'm going for the code I'm currently using that is not recommended.

   
<?php

$posts = file_get_contents('http://www.hospitalpadreze.org.br/noticias/wp-json/wp/v2/posts?per_page=2');
$obj = json_decode($posts);
foreach ($obj as $post) {
    $id = ($post->id);
    echo '<div class="box">';
    $thumbnail = file_get_contents('http://www.hospitalpadreze.org.br/noticias/wp-json/wp/v2/media?parent=' . $id . '');
    $images = json_decode($thumbnail);
    foreach ($images as $image) {
        echo '<div class="imagem-destaque">';
        echo '<a href="' . $post->link . '">';
        echo '<img src="' . $image->media_details->sizes->index_blog->source_url . '"/>';
        echo '</a>';
        echo '</div>';
    }

    echo '<h2><a href="' . $post->link . '">' . $post->title->rendered . '</a></h2>';
    echo '</div>';
}
?>
   
    
asked by anonymous 07.02.2018 / 13:09

1 answer

0

Do this:

$ch = curl_init();  
$url = 'http://www.hospitalpadreze.org.br/noticias/wp-json/wp/v2/posts?per_page=2';
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
//  curl_setopt($ch,CURLOPT_HEADER, false); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET"); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$output=curl_exec($ch);
curl_close($ch);


$obj = json_decode($output);
foreach ($obj as $post) {
    $id = ($post->id);
    echo '<div class="box">';
    $thumbnail = file_get_contents('http://www.hospitalpadreze.org.br/noticias/wp-json/wp/v2/media?parent=' . $id . '');
    $images = json_decode($thumbnail);
    foreach ($images as $image) {
        echo '<div class="imagem-destaque">';
        echo '<a href="' . $post->link . '">';
        echo '<img src="' . $image->media_details->sizes->index_blog->source_url . '"/>';
        echo '</a>';
        echo '</div>';
    }

    echo '<h2><a href="' . $post->link . '">' . $post->title->rendered . '</a></h2>';
    echo '</div>';
}
    
27.04.2018 / 16:50