0

I want to integrate an API for automatically import products from a 3rd party.

I have the API for that . API documentation says as follows.,

We update inventory every 24 hours!

The API request must be in the following form:**

http://boatexportusa.com/wp-content/plugins/api/api.php?apikey=myAPIkeyHere

The API response is a JSON string containing all the listings (>55.000)

You can set the optional variable debug=1 when you are testing, this will force that the response from the API contain only 50 listings and you have unlimited daily requests.

The only request method available is GET

Example: API Request (with CURL)

$ch = curl_init('http://boatexportusa.com/wp-content/plugins/api/api.php?apikey=myAPIkeyHere')
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);

Example: API Response (JSON)

{
    "status":"SUCCESS",
    "message":"Results have been extracted",
    "results":[{
                "post_title":"1984 AQUASPORT 222 FT W96 YAMAHA",
                "post_name":"1984-aquasport-22-2-ft-w96-ya",
                "post_content":"Nice boat 1984 aquasport 22.2 ft c.c 1996 yamaha 200 hp runs good.",
                "post_category":"By Owner",
                "price":"52000",
                "location":"Miami, FL",
                "stock":"952481",
                "year":"2007",
                "images":[
                          "http:\/\/boatexportusa.com\/wp-content\/plugins\/api\/get-image.php?src=fakesrc\/00o0o_7TGyKJXFMp9_600x450.jpg",
                          "http:\/\/boatexportusa.com\/wp-content\/plugins\/api\/get-image.php?src=fakesrc\/00o0o_7TGyKJXFMp9_600x450.jpg"
                         ],
                "cond":"new",
                "size":"27'",
                "make":"SeaRay",
                "model":"S300",
                "hours":"330",
                "propulsion":"Power",
                "addedtime":"2015-03-23"
                }]
}

I am blind stuck on how to proceed

How should i integrate this API with the Wocommerce Products to automatically get synced

UPDATE :

Image is not getting stored , I use the following code

'post_images' => $data[ 'images' ],

closed as off-topic by Pieter Goosen, TheDeadMedic, Howdy_McGee, cybmeta, kaiser Jan 3 '16 at 21:56

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

3

There is a similar problem to create posts from an API and programmatically generating posts.

Essentially just parse the JSON into an array, grab the fields you need, and create the post/product. You most likely need to pull the images as well.

You should also consider if the post already exists in your database and how/where you want the extra metadata to live like; price, stock, year, hours, etc...

JSON PARSE

$response = '{"status":"SUCCESS","message":"Results have been extracted","results":[{"post_title":"1984 AQUASPORT 222 FT W96 YAMAHA","post_name":"1984-aquasport-22-2-ft-w96-ya","post_content":"Nice boat 1984 aquasport 22.2 ft c.c 1996 yamaha 200 hp runs good.","post_category":"By Owner","price":"52000","location":"Miami, FL","stock":"952481","year":"2007","images":["http:\/\/boatexportusa.com\/wp-content\/plugins\/api\/get-image.php?src=fakesrc\/00o0o_7TGyKJXFMp9_600x450.jpg","http:\/\/boatexportusa.com\/wp-content\/plugins\/api\/get-image.php?src=fakesrc\/00o0o_7TGyKJXFMp9_600x450.jpg"],"cond":"new","size":"27\'","make":"SeaRay","model":"S300","hours":"330","propulsion":"Power","addedtime":"2015-03-23"}]}';

// decode the json

$json = json_decode($response, true);

// loop through the results

if( ! empty($json[ 'results' ])) {
    foreach($json[ 'results' ] as $data) {

        // gather data you need, make sure to sanitize it

        $post_data = array(
            'post_title'   => $data[ 'post_title' ],
            'post_name'    => $data[ 'post_name' ],
            'post_content' => $data[ 'post_content' ],
            'post_date'    => date('Y-m-d H:i:s'),
            'post_status'  => 'publish', 
            'post_author'  => 1,
        );

        // create product or post and return the new ID

        $post_id = wp_insert_post($post_data, $wp_error);
    }
}

UPDATE

I would recommend putting this into a plugin. That way you can turn it on or off and keep it separate from your main theme. Use something like WPPB.me to generate a basic plugin.

When the plugin activates, schedule a WP Cron task to run daily.

register_activation_hook(__FILE__, 'my_activation');

function my_activation() {
    wp_schedule_event(time(), 'daily', 'my_daily_event');
}

add_action('my_daily_event', 'do_this_daily');

function do_this_daily() {
    // pull api data every day
}

Since the WP Cron is a fake cron it might be best to hook your API pull in the shutdown hook or avoid cron and trigger it from an admin option page. Either way TechCrunch WP Asynchronous Tasks could help offload the processing power to another task.

Once you've pulled the new data you'll want to figure out a way to uniquely identify the product so you don't duplicate it in the database. Check to see if it exists and if it does then update it. If not, make a new one.

update_post_meta to set it, WP_Meta_Query to see if it exists. Or check by title:

if (null == get_page_by_title($title) && empty(get_posts(array('name' => $slug)))) 

If it helps, make a custom post type to capture the product data and generate them on a custom page template using a custom query.

Everything outside of what I answered you could google, try it out, and ask new questions when you run into issues.

  • First of all thanks @jgraup . Can you explain me step by step like which file i should edit in which location and what code . Sorry for looking silly , i am just a beginner mate . – Raja Gopal Dec 29 '15 at 22:16
  • Your above json parse code inserts the data successfully . But , if i add url instead of product datas like $response = 'feed url'; it doesnt work . aslo tried $url = 'boatexportusa.com/wp-content/plugins/api/api.php?apikey=mykey'; then $response='$url'; this also not helped . can you tell me how to add feed url in this json parse code you gave . aslo how to check for duplicate entries – Raja Gopal Jan 4 '16 at 15:05
  • also tried following but nothing came up $ch = curl_init('url/myapikey') curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); $json = json_decode($response, true); if( ! empty($json[ 'results' ])) { foreach($json[ 'results' ] as $data) { $post_data = array( 'post_title' => $data[ 'post_title' ], 'post_name' => $data[ 'post_name' ], ); $post_id = wp_insert_post($post_data, $wp_error); } } – Raja Gopal Jan 4 '16 at 15:37
  • I echo and got the following error "Requested Range not satisfiable" – Raja Gopal Jan 4 '16 at 23:11
  • codex.wordpress.org/Function_Reference/wp_remote_get might be better to use than CURL - good luck – jgraup Jan 4 '16 at 23:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.