Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Eli

    (@scheeeli)

    can you give me an example of the URL that you are wanting to fetch?

    Thread Starter oedipamaas

    (@oedipamaas)

    Sure! It would be like this:

    curl -XGET https://myremotenonWPserver/event/94303?radius=30

    which returns to me something like:

    {“0.0”: {“event_id”: “1179”, “post_id”: “1564”}}

    So basically I’m trying to submit a radius search, this remote app crunches the numbers and gives me back a list with WP post ids in it. Then I want to load those post ids.

    Plugin Author Eli

    (@scheeeli)

    Well, you can certainly use my plugin to get and parse the first URL like this:
    [remote_get url=”https://myremotenonWPserver/event/94303?radius=30″%5D

    But then I’m not sure I understand how you want to use the data returned.

    You then want to display and internal WP post based on the IDs returned or you want to fetch another URL with the returned post IDs?

    It might help if you provided the real URL to your radius search. You can email me directly if you don’t want to post it on the forum: wordpress at ieonly dot com

    Thread Starter oedipamaas

    (@oedipamaas)

    I can’t share the url at all. I work for a large corp and it would be a huge no no. But I can break it down for you a bit better.

    What I’m trying to acheive, is to simply talk to this really weird AI system that ALSO holds post ids in its system. When I have a visitor, I send their IP address to this AI system, and the AI system then knows what post id to send back (based on their location). So I know that it’s really weird to have a seperate system also containing post ids when it’s not WP, but there you go. It’s what I’m dealing with.

    When I get the JSON back, it looks like this:

    {"event_id": "1179", "post_id": "1564", "location_id": "19", "location_postcode": "94301"}

    Let’s just call this AI system Skynet. So my application or plugin or addition to a template (not sure which yet) will push a curl get (is there a better way? wp_remote?) to this Skynet-like thing, Skynet responds with the above and I capture that response (again, can I do that with your plugin you think?) and then I json_decode, grab the key–>values and then submit the ids to something like get_post_by_id or whatever is out there. I figured a loop even sort of like:

    function myGet_CURL (){
    $url        = 'https://remoteNONWPserver/event/94303?radius=30';
    
    $ch = curl_init();
    curl_setopt ($ch, CURLOPT_URL, $url);
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, 0);
    $result = curl_exec($ch);
    curl_close($ch);
    
    // Prepare the data:
    $content = trim( wp_remote_retrieve_body( $result ) );
    // Convert output to JSON
    if (
    strstr(
        wp_remote_retrieve_header( $result, 'content-type' )
        ,'json'
    ) )
    {
        $content = json_decode( $content );
        $post_id = _json_decode_object_helper('post_id');
        //now I need to parse the decoded content.  But I need to see what I'm doing in the loop
        //as I parse and return
    
    }
        return $content;
    }
    
    function myDecodeGetPost(){
    
        $validPosts = array();
        $this_post = array();
        $id_post = array();
        $i = 0;
    
    //so once i pull the post id, it gets passed here into a variable that will act as the id
        $my_query = new WP_Query( $id_post );
    
        if($my_query->have_posts()) {
            while($i < $my_query->post_count) :
                $post = $my_query->posts;
    
                if(!in_array($post[$i]->ID, $id_post)){
                    $this_post['id'] = $post[$i]->ID;
                    $this_post['post_content'] = $post[$i]->post_content;
                    $this_post['post_title'] = $post[$i]->post_title;
                    $this_post['guid'] = $post[$i]->guid;
    
                    $id_post[] = $post[$i]->ID;
                    array_push($validPosts, $this_post);
    
                }
    
                $post = '';
                $i++;
    
            endwhile;
    //figure out how to display the posts you put in the array onto a page now.  Put all this into a template?
        }
    }

    So is the above easier to achieve with your plugin you think? Thanks!

    Plugin Author Eli

    (@scheeeli)

    Actually I think your custom code would be easier to get the results you are looking for, and it looks like you almost have it working.

    My plugin is meant to fetch some external content using a shortcode on you page or post. I think it could be used to produce near the same result as what you have but it would be more difficult, less efficient, and less flexible than your approach.

    It looks like you just need to pass your array of Post IDs to a WP Query that would replace The Loop. You could model that loop off of the archive.php or catagory.php file from your theme.

    Aloha, Eli

    Thread Starter oedipamaas

    (@oedipamaas)

    Thanks for the encouragement Eli. I’m almost there, as you said. Now if only I could figure out why json_decode fails in WP. ??

    https://wordpress.stackexchange.com/questions/106634/why-is-json-decode-failing

    Thanks for your feedback though!

    Plugin Author Eli

    (@scheeeli)

    no problem,

    By the way, I looked at that code you posted on stackexchange.
    When you use:
    add_filter('the_content', 'my_Get_CURL', 1, 3);
    the 3 is the number of arguments you are passing (should be 1), and the my_Get_CURL function should then receive the content variable.

    Also, when using the ‘the_content’ filter you should always return a string. Try something like this (just for testing purposes):

    function my_wpRemote($content) {
    
        // Send GET request to remote API
        $api_url = 'https://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 $content.print_r(array(
            'distance'  => $json->distance,
            'data' => $json->data
        ),1);
    }
    add_filter('the_content', 'my_wpRemote', 1, 1);

    that should concat your json array to the end of the content.

    Good luck, Eli

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How can I use this to fetch a post id remotely (from a non-WP app) and load it?’ is closed to new replies.