Mashape API PHP - 没有数据显示?

时间:2014-06-03 09:24:37

标签: php api unirest mashape

我无法从mashape API中获取任何数据,我已经进行了UNIREST POST但是我无法将数据回复给自己,这就是调用应该返回的内容;

{
 "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"
}
}

我的代码......

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";
?>

任何人都可以建议我如何让它回应"名称"。

任何帮助将不胜感激:)

1 个答案:

答案 0 :(得分:2)

您尝试显示正文的方式似乎阻止您在回复中看到错误,告知您属性platform必须是number

尝试使用json_encode($response->body)来查看完整的回复:

<?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);

?>

获得回复后,您可以使用$response->body->result->name

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