Как отобразить один результат из результата JSON - PullRequest
0 голосов
/ 01 июня 2018

Здравствуйте, я очень новичок в программировании на 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" }

1 Ответ

0 голосов
/ 01 июня 2018

Пропустил параметр, управляющий ответом на печать, или вернул его в переменную.Используйте следующий фрагмент кода ниже curl_init:

curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

CURLOPT_RETURNTRANSFER - TRUE, чтобы вернуть передачу в виде строки возвращаемого значения curl_exec () вместо непосредственного его вывода.

Подробнее об этом Здесь

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...