thanks for your help, I still seem to be returning no data when I call the api though ?? really don’t know what I am doing wrong…
<?php
/**
* Plugin Name: this plugin
* Plugin URI:
* Description:
* Version: 1.0.0
* Author:
* Author URI:
* License: GPL2
*/
// import seasons and competitions
function import_seasons()
{
$season_request = 'https://thisdomain.co.uk/api/season';
$username = 'thisdomain';
$password = 'thisdomain/user';
$headers = array( 'Authorization' => 'Basic ' . base64_encode( "$username:$password" ) );
$season_response = wp_remote_get( $season_request, array( 'headers' => $headers));
if ( is_wp_error( $season_response ) ) { $error_string = $season_response->get_error_message(); echo '<div id="message" class="error"><p>' . $error_string . '</p></div>'; }
if( is_array($season_response ) ) {
$season_header = $season_response['headers']; // array of http header lines
$season_body = json_decode( $season_response['body'] ); // use the content
$season_data = $season_body;
}
else{
$season_data = wp_remote_retrieve_body($season_response);
}
}
function insert_or_update($season_data) {
if ( ! $season_data)
return false;
$args = array(
'meta_query' => array(
array(
'key' => 'season_id',
'value' => $season_data->id
)
),
'post_type' => 'seasons',
'post_status' => array('publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit'),
'posts_per_page' => 1
);
$season = get_posts( $args );
$season_id = '';
if ( $season )
$season_id = $season[0]->ID;
$season_post = array(
'ID' => $season_id,
'post_title' => $season_data->startDate,
'post_content' => $season_data_data->competition,
'post_type' => 'seasons',
'post_status' => ( $season ) ? $season[0]->post_status : 'publish'
);
$season_id = wp_insert_post( $season_post );
if ( $season_id ) {
update_post_meta( $season_id, 'season_id', $season_data->id );
update_post_meta( $season_id, 'json', addslashes( file_get_contents( 'php://input' ) ) );
}
print_r( $season_id );
}
add_action('init', 'import_seasons');
add_action('init', 'insert_or_update');
?>
-
This reply was modified 7 years, 8 months ago by rmsgreig.