0

我无法从 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";
?>

谁能建议我如何让它呼应“名称”。

任何帮助,将不胜感激 :)

4

1 回答 1

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}]";
于 2014-06-03T18:54:30.423 回答