你好我是非常新的编码在php和使用curl所以在回复时请记住这一点。我试图从JSON对象中获取单个元素并且无法弄清楚如何做到目前为止我已经写了这个并不是很多但似乎无法让它工作任何帮助都会非常感激
$curl=curl_init("https://api.brawlhalla.com/legend/3/?api_key=KEY");
$resp=curl_exec ($curl);
$jsonObj=json_decode($resp);
echo $jsonObj->legend_id;
curl_close($curl);
这是json对象的样子
{ "legend_id": 3, "legend_name_key": "bodvar", "bio_name": "B\u00f6dvar", "bio_aka": "The Unconquered Viking, The Great Bear", "bio_quote": "\u201cI speak, you noble vikings, of a warrior who surpassed you all. I tell of a great bear-man who overcame giants and armies, and of how he came to leave our world and challenge the Gods.\u201d", "bio_quote_about_attrib": "\"-The Saga of B\u00f6dvar Bearson, first stanza\"", "bio_quote_from": "\"Listen you nine-mothered bridge troll, I'm coming in, and the first beer I'm drinking is the one in your fist.\"", "bio_quote_from_attrib": "\"-B\u00f6dvar to Heimdall, guardian of the gates of Asgard\"", "bio_text": "Born of a viking mother and bear father, B\u00f6dvar grew up feared and mistrusted by his own people.\nB\u00f6dvar's first nemesis was the terrible giant bear Grothnar, his own brother. By defeating Grothnar in a battle that lasted seven days, B\u00f6dvar chose to side with humanity and became the protector of the people of the north. He led his Skandian people against the Witch Queen of Helheim, slew the White Dragon Sorcerer, and lived the life of an all-conquering hero.\nAfter he single-handedly ended the Giant Wars by trapping the fire giant king in his own volcano, B\u00f6dvar sensed his work was done. But he felt doomed to never be taken by the Valkyries to Valhalla because he could never manage to be defeated in battle. So he travelled to Asgard himself, broke down the doors, and let himself in.\nValhalla is everything B\u00f6dvar hoped - an endless reward of feasting and fighting, with himself among its greatest champions.", "bot_name": "B\u00f6tvar", "weapon_one": "Hammer", "weapon_two": "Sword", "strength": "6", "dexterity": "6", "defense": "5", "speed": "5" }
答案 0 :(得分:0)
错过了控制打印响应或将其返回变量的选项。使用curl_init下面的以下代码:
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
CURLOPT_RETURNTRANSFER - TRUE,将传输作为curl_exec()返回值的字符串返回,而不是直接输出。
详细了解Here