wp_remote_get returns an array, not a json string. I’m not sure what you are trying to do, but my guess is that you need to decode the $season_response[‘body’] if the link is supposed to give you a json string.
wp_remote_get returns an array of headers and then the content:
$response = wp_remote_get( ‘http://www.example.com/index.html’ );
if( is_array($response) ) {
$header = $response[‘headers’]; // array of http header lines
$body = $response[‘body’]; // use the content
}
This is taken from here: https://codex.wordpress.org/Function_Reference/wp_remote_get
change this line from: $season_data = json_decode($season_response, true);
to: $season_data = json_decode($season_response[‘body’], true);
and see if that works.
Good luck!
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 9 years, 2 months ago by
rmsgreig.
If you are getting no response at all, you may not be accessing the API correctly. Do you get anything back when you make a manual API request through your browser? You should be prompted for username and password when accessing this way.
Your use of wp_remote_get() is correct if Basic Authorization is the only thing the API requires. You may need to inquire about your issue through the API’s support.
I get a response when I make a manual request to the browser it just doesnt seem to come through to wordpress. Is there anything else I could do to check this within wordpress? I am talking to the api developer as well to try and figure something out but as I dont have that much wordpress experience I am worried that it is my error
Thanks
The devil is in the details. If the API requires other arguments besides an authorization header, I wouldn’t know that. Calling wp_remote_get() itself is straight forward enough. Try requesting another URL through wp_remote_get() that requires no other arguments to be sure the requests are actually going out. Sometimes the server configuration can prevent this from working.
Try some alternative arguments like ‘sslverify’ => false, in case there is a problem with their certificate (if using HTTPS). Or maybe a longer timeout. Things like that. Make changes one at a time so if it suddenly starts working you’ll know exactly what it took.