How can I convert my API code from JSON to cURL

Hi guys,

I’m recently upgraded my server’s PHP version which has caused an error with my JSON code:

$channelName = PixelGumTV
$clientId = 'xxxxxxxxxxxxxxxxxxxxxxxxxx';
$json_array = json_decode(file_get_contents(''.strtolower($channelName).'?client_id='.$clientId), true);
if ($json_array['stream'] != NULL) {
    $channelTitle = $json_array['stream']['channel']['display_name'];
    $streamTitle = $json_array['stream']['channel']['status'];
    $currentGame = $json_array['stream']['channel']['game'];

Can anyone tell me how I can convert this code so I can obtain exactly the same information? ($channelTitle, $streamTitle, & $currentGame).

Many thanks!

I don’t do a lot in PHP but always preferred GuzzleHttp. The channel ID below belongs to PixelGumTV as I am using v5, I think you are defaulting to v3 based on the code up there and that is about to be made the non-default and third-party support removed early this year.

$client = new GuzzleHttp\Client();

    $response = $client->request('GET',
                                [ 'headers' => [
                                    'Client-ID' => 'myclientID',
                                    'Accept' => 'application/vnd.twitchtv.v5+json' ]

    $result = $response->getBody();
    $object = json_decode($result);
catch (Exception $e)
    $result = $e->getResponse();

Right now, offline so just returns NULL:

illusion% php ./test_twitch.php 
object(stdClass)#31 (1) {

So, swapped to the channels endpoint:

illusion% php ./test_twitch.php 
object(stdClass)#31 (23) {
  string(82) "S2 Ep.174: Multiplayer Madness! | DAILY STREAM | !schedule !grab !pixelpot !coffee"
  string(2) "en"
  string(12) "unknown_rtmp"
  string(10) "PixelGumTV"
  string(24) "The Jackbox Party Pack 4"

Additional notes here on your duplicate post

To one of Barry’s points, from hitting Google, file_get_contents() is known to have hiccups. Perhaps you could try the refactor as I indicate above and see what happens. If that fails then I think Barry has outlined any other possible issues as they relate to certificates.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.