2

I am using wordpress with JSON REST API plugin (http://wordpress.org/plugins/json-rest-api/).

I am able to create my custom endpoint. At this case I create endpoint for displaying 5 users.

This is my code:

class JSON_API_Pmbs_Controller
{
  public function user_search()
  {

    global $wpdb;
    $query = "SELECT * FROM pmbsn_users LIMIT 0,3";
    $blogusers = $wpdb->get_results($query);
    $return_array = array();
    foreach ($blogusers as $user)
    {
        $avatar_url = get_avatar_url ( get_the_author_meta($user->ID), $size = '50' ); 

        $current_array = array(
            'thumbnail' => $avatar_url,
            'display_name' => $user->display_name,
            'user_url' => site_url().'/members/'.$user->user_login
            );

        array_push($return_array, $current_array);
    }

    $echo_json = json_encode($return_array, JSON_PRETTY_PRINT);
    echo $echo_json;
  }
}

And this is the JSON result:

[
{
    "thumbnail": "http:\/\/0.gravatar.com\/avatar\/?d=identicon&s=50",
    "display_name": "PMBS Admin",
    "user_url": "http:\/\/localhost\/web_pmbsnetwork\/members\/pmbsadmin"
},
{
    "thumbnail": "http:\/\/0.gravatar.com\/avatar\/?d=identicon&s=50",
    "display_name": "Johan HS",
    "user_url": "http:\/\/localhost\/web_pmbsnetwork\/members\/johan"
},
{
    "thumbnail": "http:\/\/0.gravatar.com\/avatar\/?d=identicon&s=50",
    "display_name": "Seno Adi W",
    "user_url": "http:\/\/localhost\/web_pmbsnetwork\/members\/seno"
}
]null

You guys may notice that there is a strange 'null' text in the end of json result.

After debug, I found out that was from global $wpdb.

I have been doing:

  1. Include wp-load.php (http://wordpress.org/support/topic/wpdb-returning-null)
  2. Include all (maybe required) files (Using WPDB in standalone script?)
  3. Set global $wpdb = "foo" ($wpdb is null even after 'global $wpdb)

But no success. How can I remove the null text in the end of my JSON result?

Thanks.

Community
  • 1
  • 1
mokalovesoulmate
  • 271
  • 2
  • 6
  • 18

2 Answers2

0

I think problem is your non-utf8 data.

  1. Try to put this code before your SELECT query.

    $wpdb->query('SET CHARACTER SET utf8');
    
  2. If your problem isn't solved, follow steps in this answer: https://stackoverflow.com/a/1972468/2718799

Community
  • 1
  • 1
mht
  • 80
  • 2
  • 7
  • Sorry, both solution are not worked. The error I encounter is not the json, but the $wpdb. If I comment out the `echo $echo_json;`, it only display `null` without the echoed json. Thanks. – mokalovesoulmate Jun 06 '14 at 08:21
  • I'm not sure, but could you please try `JSON_UNESCAPED_UNICODE` instead of `JSON_PRETTY_PRINT` if you have php 5.4+? – mht Jun 06 '14 at 11:12
0

So finally I do a very dirty way;

  • uninstall JSON REST API plugin
  • make new file (example: wordpress-root/api/get_user.php)
  • move scripts inside controller's method to the get_user.php
  • replace global $wpdb with include('../wp-load.php');

Then the strange null text from $wpdb is gone.

For those who experienced this problem I strongly not recommend my answer, this is just quick dirty fix.

mokalovesoulmate
  • 271
  • 2
  • 6
  • 18