1

I got this code

add_action( 'init', 'check_api_data' );

function check_api_data() {    

  if(isset($_GET['api']) ) {
  if ($_GET['api'] == 'json'){
           $args = array(  
                'post_type' => 'post'
           );
           $query = new WP_Query( $args ); // $query is the WP_Query Object
           $posts = $query->get_posts();   // $posts contains the post objects

           $output = array();
           foreach( $posts as $post ) {    // Pluck the id and title attributes
               $output[] = array( 
                'id' => $post->ID, 
                'title' => $post->post_title, 
                'content' => $post ->post_content,
                'imageurl' => wp_get_attachment_url( get_post_thumbnail_id($post->ID) )
                );
           }
           header("Content-type: application/json");
           echo json_encode( $output );
       }
   exit();
   }  
}

This is the output:

[
  {
    "id": 19,
    "title": "Early bird tickets",
    "content": "that is good",
    "imageurl": "http://localhost/PRACTISE/wp/wp-content/uploads/2016/10/news2.jpg"
  },
  {
    "id": 95,
    "title": "See you next year!",
    "content": "Lorem ipsum",
    "imageurl": "http://localhost/PRACTISE/wp/wp-content/uploads/2016/11/tak_for_i_aar.jpg"
  }
]

How can I use this data in a remote server, so that everytime I update my content of Server A it gets updated at Server B

1 Answer 1

2

On server B:

$result = wp_remote_post('http://serverA.com/?api=json', array(
    'method'      => 'POST',
    'redirection' => 1,
    'httpversion' => '1.0',
    'blocking'    => true,
    'headers'     => array(),
    'body'        => array(),
    'cookies'     => array()
));
if ( is_wp_error( $result ) ) {
    return 'bad connection!';
}
$json = $result['body'];
$posts = json_decode($json);

Now you have $posts as usual php array. var_dump($posts) will look like:

array(2) {
  [0]=>
  object(stdClass)#7918 (4) {
    ["id"]=>
    int(19)
    ["title"]=>
    string(18) "Early bird tickets"
    ["content"]=>
    string(12) "that is good"
    ["imageurl"]=>
    string(65) "http://localhost/PRACTISE/wp/wp-content/uploads/2016/10/news2.jpg"
  }
  [1]=>
  object(stdClass)#7919 (4) {
    ["id"]=>
    int(95)
    ["title"]=>
    string(18) "See you next year!"
    ["content"]=>
    string(11) "Lorem ipsum"
    ["imageurl"]=>
    string(73) "http://localhost/PRACTISE/wp/wp-content/uploads/2016/11/tak_for_i_aar.jpg"
  }
}
8
  • You solved my problem, thanks! Just one questions? Why is it that even though we have GOT posts from the remote server we have to use the POST method? instead of the GET?
    – Johnthomas
    Commented Feb 4, 2017 at 14:39
  • It works if I set the 'method' to GET though
    – Johnthomas
    Commented Feb 4, 2017 at 14:43
  • So the array is an optional one But Why did u overwrite the default values? (I dont know what the default values are though)
    – Johnthomas
    Commented Feb 4, 2017 at 15:02
  • You are welcome. Yes, we can use POST. My answer is an extraction from one of my plugins, so you can edit it. Maybe overwriting of defaults is overkill )) Commented Feb 4, 2017 at 15:25
  • Last question, what does the $json = $result['body']; do?
    – Johnthomas
    Commented Feb 4, 2017 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.