Fix for API key invalid referer!
-
Struggles with invalid referer when you have API key restrictions? You will need to edit the load_content function in yotuwp.php. By default, wp_remote_get does NOT send the Referer in the header information. After digging into #37820 it looks like a simple fix, but has to be done in the code. After editing the area where it says $response = wp_remote_get to add the headers section to the array, this plugin will start working.
To debug and test before and after, add error_log(print_r($response, true)); directly after the original $response line.
public function load_content( $url ) {
$url .= '&key='. trim($this->api['api_key']);
if ( $this->is_cache) {
$cache_id = md5( $url);
$cache_content = $this->cache( $cache_id );
if( !is_string( $cache_content ) ) {
$response = wp_remote_get( $url, array(
'timeout' => $this->timeout,
'headers' => array(
'Referer' => home_url()
)
));
$this->cache( $cache_id, json_encode( $response ) );
}else $response = json_decode( $cache_content, true);
}else
$response = wp_remote_get( $url, array(
'timeout' => $this->timeout,
'headers' => array(
'Referer' => home_url()
)
));
if (is_wp_error( $response ) || $response['response']['code'] !== 200)
{
$msg = '';
if( is_array( $response) && isset( $response['body'] ) ) {
$obj = json_decode( $response['body'] );
$msg = $obj->error->message;
}
if( isset( $response->errors) && isset( $response->errors['http_request_failed'] ) ) {
$msg = $response->errors['http_request_failed'][0];
}
return array( 'items' => array(), 'error' => true, 'msg' => $msg);
}
return json_decode( $response['body'] );
}
Viewing 7 replies - 1 through 7 (of 7 total)
Viewing 7 replies - 1 through 7 (of 7 total)
The topic ‘Fix for API key invalid referer!’ is closed to new replies.