WordPress.org

Ready to get started?Download WordPress

Forums

ELI's SHORTCURL Shortcodes to Fetch and Parse External Content
[resolved] How can I use this to fetch a post id remotely (from a non-WP app) and load it? (8 posts)

  1. oedipamaas
    Member
    Posted 9 months ago #

    I want to submit a remote get or post against a remote application that is not a wordpress app. That application returns a response of a post id and then i need to load it into my primary content area. Can I do that with your application?

    http://wordpress.org/extend/plugins/shortcurl/

  2. Eli
    Member
    Plugin Author

    Posted 9 months ago #

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

  3. oedipamaas
    Member
    Posted 9 months ago #

    Sure! It would be like this:

    curl -XGET http://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.

  4. Eli
    Member
    Plugin Author

    Posted 9 months ago #

    Well, you can certainly use my plugin to get and parse the first URL like this:
    [remote_get url="http://myremotenonWPserver/event/94303?radius=30"]

    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

  5. oedipamaas
    Member
    Posted 9 months ago #

    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        = 'http://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!

  6. Eli
    Member
    Plugin Author

    Posted 9 months ago #

    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

  7. oedipamaas
    Member
    Posted 9 months ago #

    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. :(

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

    Thanks for your feedback though!

  8. Eli
    Member
    Plugin Author

    Posted 9 months ago #

    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 = '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 $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

Reply

You must log in to post.

About this Plugin

About this Topic