1

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);
6
  • Also, I did try this approach too..which was textbook. Still nothing gets returned:
    – yoyodyne
    Commented Jul 16, 2013 at 3:42
  • 1
    You json_decode your $json_content to the variable $json but are still trying to use $json_content. Look at the last six lines of that callback. I don't think this has anything to do with WordPress.
    – s_ha_dum
    Commented Jul 16, 2013 at 4:14
  • Yeah, I caught that and fixed it. It's still failing. I'll update my question with the ammended code.
    – yoyodyne
    Commented Jul 16, 2013 at 6:01
  • Ok, ammended the $json_content reference to reflect the variable name for $json instead when it calls the elements of the array. Tested code in functions.php and still no joy.
    – yoyodyne
    Commented Jul 16, 2013 at 6:03
  • 1
    Your JSON string is malformed. There is a leading [ but there also appears to be two encoded objects separated by a comma. Either decodes correctly when run through json_decode individually but not when jammed together like that. Still not a WordPress issue.
    – s_ha_dum
    Commented Jul 16, 2013 at 13:46

1 Answer 1

0

Use print_r(); and into a variable to work further. Example:

$convertObj = json_decode($fetchJobs);
$jobArray = print_r($convertObj->data, true);

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.