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:
I would like to be able to connect this with my WP so that the content is published automatically.
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.
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.
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>';
}
}