Site icon Hip-Hop Website Design and Development

Why is json_decode failing?

I’m trying to determine if this is related to my having the latest version of PHP on my server while using the latest version of WordPress. Or if I’m just doing it wrong:

Here’s my function that is correctly returning values (I can see them when I do an echo or a var dump):

    function my_Get_CURL (){
    $url        = 'http://someotherserver/event/94303?radius=30';
    //  Initiate curl
        $ch = curl_init();
    // Disable SSL verification
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    // Will return the response, if false it print the response
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    //header
    $headers = array(
        'Content-type: application/json'
    );
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    // Set the url
        curl_setopt($ch, CURLOPT_URL,$url);
    // Execute
        $json_content=curl_exec($ch);

    $json = json_decode($json_content, true);
   // echo $json_content;
        return array(
             'distance' => $json->distance,
            'data' => $json->data
        );
    }

    add_filter('the_content', 'my_Get_CURL', 1,3);

Here’s what var_dump or echo produces:

[{"distance": 0.0, "data": {"event_id": "1179", "post_id": "1564", "location_id": "19", "location_postcode": "94301"}}, {"distance": 2.162680661505968, "data": {"event_id": "1193", "post_id": "1578", "location_id": "19", "location_postcode": "94301"}}

Is it me or, as I’ve read some scattershot reports on json_decode in WordPress, is it the fact that I’m using php v5.3.2?

Should I be using wp_remote_retrieve_body instead? What’s the difference between what that does and the CURL options I’m using?

Also, I did try this approach too..which was textbook. Still nothing gets returned:

function my_wpRemote() {

    // Send GET request to remote API
    $api_url = 'http://remoteserver/event/94303?radius=30';
    $api_response = wp_remote_get( $api_url );

    // Get the JSON object
    $json = wp_remote_retrieve_body( $api_response );

    // Make sure the request was succesful or return false
    if( empty( $json ) )
        return false;

    // Decode the JSON object
    // Return an array
    $json = json_decode( $json );

    return array(
        'distance'  => $json->distance,
        'data' => $json->data
    );
}
add_filter('the_content', 'my_wpRemote', 1,3);