Displaying wordpress posts with Curl

Asked

Viewed 169 times

0

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

That’s when a guy I had contact with said I should use CURL, Here comes the question, how do I get the pots and highlight image?

Below I will put the code I am using at the moment 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>';
}
?>
   

1 answer

0

Do so:

$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>';
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.