0

I have a scrape that generates content in JSON format and can be obtained by GET to a specific URL, the URL from which to extract information is this: https://api.webscraper.io/api/v1/scraping-job/4851593/json?api_token=aLXQWr2IbQCefgiLc1PfIjfx7GqvsBd3APVbU1pHchszzzFIFa6HFKsNmpft

I would like to be able to connect this with my WP so that the content is published automatically.

1
  • What have you tried so far? Commented Jul 16, 2021 at 5:20

2 Answers 2

0

You can use wp_insert_post() to add a new post programmatically. For Example,

$my_post = array(
  'post_title'    => wp_strip_all_tags( $json->title ),
  'post_content'  => $json->body,
  'post_status'   => 'publish',
  'post_author'   => 1,
  'post_category' => $json->categories
);
 
// Insert the post into the database
wp_insert_post( $my_post );

Your link has expired so I could not see the structure of JSON data. But the answer would be the same. Set up a cron job or use their webhook (if they have any) to call a page where you have set up this wp_insert_post()for automatic posting.

3
  • I updated the JSON link: api.webscraper.io/api/v1/scraping-job/4851593/… Commented Jul 12, 2021 at 13:07
  • Use json_decode(), set up a foreach() loop for array and use $json['titulo_post'] for title and $json['contenido_post'] for contents on the snippet above. Also, we can only help you to solve problem. You still have to do your own homework. I would recommend you to hire some professional if you are not fimilier with Wordpress Codex and PHP. Commented Jul 12, 2021 at 13:31
  • 1
    Thanks for your help. I understand the logic, first I decode the JSON and store it in an array to use it later use it in wp_insert_post (), I will post the code once I have it solved in case it can help someone Commented Jul 14, 2021 at 10:45
0

write a custom simple script that retrieves the json data and creates wp posts using the wp REST API and replace the $json_url variable with the URL of your JSON endpoint

we added a post_exists() function to check if a post with the same title already exists in the wp database

// URL of the JSON endpoint
$json_url = 'https://api.webscraper.io/api/v1/scraping-job/4851593/json?api_token=aLXQWr2IbQCefgiLc1PfIjfx7GqvsBd3APVbU1pHchszzzFIFa6HFKsNmpft';

// Retrieve JSON data
$response = file_get_contents($json_url);

// Check if data was successfully retrieved
if ($response === false) {
    die('Failed to fetch JSON data.');
}

// Convert JSON data to associative array
$data = json_decode($response, true);

// Check if JSON decoding was successful
if ($data === null) {
    die('Failed to decode JSON data.');
}

// Function to check if post already exists
function post_exists($title) {
    global $wpdb;
    $query = $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_title = %s AND post_type = 'post'", $title);
    $post_id = $wpdb->get_var($query);
    return $post_id;
}

// Loop through each item in the JSON data
foreach ($data as $item) {
    // Check if post already exists
    if (post_exists($item['title'])) {
        echo 'Post with title "' . $item['title'] . '" already exists. Skipping...<br>';
        continue;
    }

    // Create new post
    $post_data = array(
        'post_title' => $item['title'], // Assuming 'title' field exists in your JSON data
        'post_content' => $item['content'], // Assuming 'content' field exists in your JSON data
        'post_status' => 'publish', // Publish the post immediately
        // You can include other fields like 'post_excerpt', 'post_category', 'tags_input', etc. as needed
    );

    // Insert the post into WordPress
    $post_id = wp_insert_post($post_data);

    // Check if post was inserted successfully
    if (is_wp_error($post_id)) {
        echo 'Failed to insert post: ' . $post_id->get_error_message();
    } else {
        echo 'Post inserted successfully with ID: ' . $post_id . '<br>';
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.