0

I am trying to get some data from an API (igdb.com/api) but I am having trouble passing authentication.

Since my website is a wordpress website I am using wp_remote_get to retrieve the response from the HTTP request. My PHP code at the moment:

 <?php

require(dirname(__FILE__) . '/wp-load.php');

$request = wp_remote_get('https://igdbcom-internet-game-database-v1.p.mashape.com/games');

$headers = array();
$headers[] = 'Accept: application/json';
$headers[] = 'X-Mashape-Key: myKey'; 

$body = wp_remote_retrieve_body( $request );

$data_api = json_decode($body, true);

var_dump($data_api);

?>

The error I get is "Missing Mashape application key" so I know my code is wrong but can't figure out how to insert the API key properly. I understand from the API documentation that installing Unirest might facilitate things for me but have no clue how to install this library in wordpress and can't find any information on this. If someone could tell me wha to change in my code to pass authentication that would be greatly appreciated.

My apologies is this is a dumb question, as this is all new to me.

1
  • 1
    You configure $headers, but you never pass that variable into any function call. Commented May 10, 2017 at 15:39

1 Answer 1

3

You should definitely pass the headers to the request. See documentation here

wp_remote_get('https://igdbcom-internet-game-database-v1.p.mashape.com/games',
             array( 'headers' => array( 
                          'Accept' => 'application/json',
                          'X-Mashape-Key' => mykey )));

and the full code based on your example

 <?php

require(dirname(__FILE__) . '/wp-load.php');


$headers = array();
$headers[] = 'Accept: application/json';
$headers[] = 'X-Mashape-Key: myKey'; 

$request = wp_remote_get('https://igdbcom-internet-game-database-v1.p.mashape.com/games' , 
                          array( 'headers' => $headers ));


$body = wp_remote_retrieve_body( $request );

$data_api = json_decode($body, true);

var_dump($data_api);

?>
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much for your answer! Now varp_dump returns NULL but this is an entirely different problem that I will figure out and it means that I have passed authentication so thanks again :)

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.