Php for retrieve list a video archived

Hello I need some basic help to work with twich Api and php, i’m trying this code to retrive videos archive but is not working. I already can generate the token.

 $url = 'https://api.twitch.tv/helix/videos';
$data = array('user_id' => 'xxxxxxxxx');

$options = array(
    'http' => array(
        'header' => "Content-type: application/vnd.twitchtv.v5+json \r\n .
         Client-Id: $client_id\r\n .
         Authorization: Bearer $token",
        'method' => 'GET',
        'content' => http_build_query($data)
    )
);

but i get Warning: file_get_contents(https://api.twitch.tv/helix/videos): Failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request

Better would be if you can help me have the video_id of the video archive resulting by the actual live stream occurring

thanks

  1. Don’t use file_get_contents
  2. Don’t use file_get_contents
  3. Don’t use file_get_contents
  4. Don’t use file_get_contents
  5. Don’t use file_get_contents
  6. Don’t use file_get_contents
  7. Don’t use file_get_contents
  8. Don’t use file_get_contents
  9. Don’t use file_get_contents
  10. Don’t use file_get_contents

Did I mention, don’t use file_get_contents!

You need to conver to using cURL

Try

<?php

        $ch = curl_init('https://api.twitch.tv/helix/videos?user_id=' . $user_id);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Client-ID: ' . $client_id,
            'Authorization: Bearer ' . $token
        ));

        $r = curl_exec($ch);
        $i = curl_getinfo($ch);

        curl_close($ch);

        if ($i['http_code'] == 200) {
            $videos = json_decode($r);

            if (json_last_error() == JSON_ERROR_NONE) {
                 // do stuff with $videos
            } else {
                $error = 'An Error ocucred at Parsing the vidoes response';
            }
        } else {
            $error = 'An Error occured fetching the vidoes';
        }

Also, don’t use file_get_contents

Once again I need your help. This is not working, I get “401 missing user information but i have all well setted”. Thanks in advance.

$client_id = 'xxxxxxxxxxxxxxxxx';
$client_secret = 'xxxxxxxxxxxxxxxxxxxxx';

$url = 'https://id.twitch.tv/oauth2/token';
$data = array('client_id' => $client_id, 'client_secret' => $client_secret, 'grant_type' => 'client_credentials');

$options = array(
	'http' => array(
		'header' => "Content-type: application/x-www-form-urlencoded\r\n",
		'method' => 'POST',
		'content' => http_build_query($data)
	)
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
if ($result === FALSE) {}

#var_dump($result);

$arrresult=json_decode($result);

foreach ($arrresult as $key=>$obj){
	#echo " $key => $obj <br>";
	if ($key="access_token") { $token=$obj; break; }
}


echo "token $token<br><br>";
$user_id=xxxxxxxxxxx;
/*$ch = curl_init('https://api.twitch.tv/helix/streams'); #streams #videos

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
	'Client-ID: ' . $client_id,
	'Authorization: Bearer ' . $token
));

$r = curl_exec($ch);
$i = curl_getinfo($ch);




$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api.twitch.tv/helix/channels?broadcaster_id=' . $broadcaster_id);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');

curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"title\":\"$title\", \"broadcaster_language\":\"es\"}");

$headers = array();
$headers[] = "Authorization: Bearer $token";
$headers[] = "Client-Id: $client_id";
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
$i = curl_getinfo($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}

curl_close($ch);

This is the code for generating a “server access token”

This requires a user access token.

Additionally you need user:edit:broadcast to be present on the token.

thanks!!!

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