1

I am trying to get the title of this broadcast from twitch API but I am not sure very much about code.. I have the following code here.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.twitch.tv/kraken/channels/test_user1");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
print $result;

and here's what it outputs.

{"mature":null,"status":"Broadcasting LIVE on Justin.tv","broadcaster_language":null,"display_name":"Test_user1","game":null,"language":"en","_id":22747064,"name":"test_user1","created_at":"2011-06-02T20:04:03Z","updated_at":"2016-04-19T21:00:05Z","delay":null,"logo":"https://static-cdn.jtvnw.net/jtv_user_pictures/test_user1-profile_image-ac0a2f0d39dda770-300x300.jpeg","banner":null,"video_banner":null,"background":null,"profile_banner":null,"profile_banner_background_color":null,"partner":false,"url":"https://www.twitch.tv/test_user1","views":99,"followers":1,"_links":{"self":"https://api.twitch.tv/kraken/channels/test_user1","follows":"https://api.twitch.tv/kraken/channels/test_user1/follows","commercial":"https://api.twitch.tv/kraken/channels/test_user1/commercial","stream_key":"https://api.twitch.tv/kraken/channels/test_user1/stream_key","chat":"https://api.twitch.tv/kraken/chat/test_user1","features":"https://api.twitch.tv/kraken/channels/test_user1/features","subscriptions":"https://api.twitch.tv/kraken/channels/test_user1/subscriptions","editors":"https://api.twitch.tv/kraken/channels/test_user1/editors","teams":"https://api.twitch.tv/kraken/channels/test_user1/teams","videos":"https://api.twitch.tv/kraken/channels/test_user1/videos"}}

I am trying to get the portion that says "status":"Broadcasting LIVE on Justin.tv" how can I go about doing so?

John Doe
  • 143
  • 1
  • 10
  • That response is formatted in JSON. So, you'll need to decode the JSON and get the value you're looking for. http://nitschinger.at/Handling-JSON-like-a-boss-in-PHP/ should help you, specifically the Decoding JSON in PHP section. – John D. Apr 29 '16 at 18:13
  • What you're getting is a json string. Decode it and get the value, like this: `$json = json_decode($result); echo $json->status;` – Rajdeep Paul Apr 29 '16 at 18:15
  • Thank you @JohnD. for that answer I was reading over that very helpful for someone who is learning as myself :D – John Doe Apr 29 '16 at 18:17

1 Answers1

1

$result is encoded in json, you can decode it using json_decode, i.e.:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.twitch.tv/kraken/channels/test_user1");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
$decodedJson = json_decode($result, true);
echo $decodedJson['status'];
//Broadcasting LIVE on Justin.tv
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268