Search code examples
phpapiunirestmashape

Mashape API PHP - No Data Displayed?


I am having trouble getting any data out of a mashape API, I have made the UNIREST POST but I am unable to echo the data back to myself, this is what the call should return;

{
 "result": {
"name": "The Elder Scrolls V: Skyrim",
"score": "92",
"rlsdate": "2011-11-11",
"genre": "Role-Playing",
"rating": "M",
"platform": "PlayStation 3",
"publisher": "Bethesda Softworks",
"developer": "Bethesda Game Studios",
"url": "http://www.metacritic.com/game/playstation-3/the-elder-scrolls-v-skyrim"
}
}

And my code...

require_once 'MYDIR/mashape/unirest-php/lib/Unirest.php';

$response = Unirest::post(
"https://byroredux-metacritic.p.mashape.com/find/game",
array(
 "X-Mashape-Authorization" => "MY API AUTH CODE"
),
array(
"title" => "The Elder Scrolls V: Skyrim",
"platform" => undefined
)
);

echo "$response->body->name";
?>

Can anyone suggest how I can get it to echo the "name".

Any help would be appreciated :)


Solution

  • It appears the way you are trying to display the body is preventing you from seeing an error in the response telling you that the property platform is required to be a number.

    Try using json_encode($response->body) instead to see the full response:

    <?php
    
    require_once 'lib/Unirest.php';
    
    $response = Unirest::post(
      "https://byroredux-metacritic.p.mashape.com/find/game",
    
      array(
        "X-Mashape-Authorization" => "-- your authorization key goes here --"
      ),
    
      array(
        "title" => "The Elder Scrolls V: Skyrim",
        "platform" => 1 # try placing undefined here.
      )
    );
    
    echo json_encode($response->body);
    
    ?>
    

    Once you've gotten the response you can then use $response->body->result->name:

    $result = $response->body->result;
    echo "{$result->name} has a score of [{$result->score}]";