Hey,
does anyone of you have an idea what I might miss? If not, I think I just go back to the normal “polling behaviour” but I thought using the webhooks would’ve been nice.
I know that there are several topics on webhooks on the forum, and I searched it thoroughly, if I missed the solution I am the one to blame. Anyways, even though I think I have done everything correctly, I am not receiving any ‘stream_changed’ notifications. Any ideas?
What I have done so far:
Subscribing to topic ‘stream_change’:
//Subscribe to topic.
$subscribe_to = 'https://api.twitch.tv/helix/streams?user_id=12345';
//Init connection.
$curl_connection = curl_init(https://api.twitch.tv/helix/webhooks/hub);
//Header.
$curl_header = array('Client-ID: ' . CLIENT_ID);
//Define body data.
$data['hub.callback'] = $callback_adress;
$data['hub.mode'] = 'subscribe';
$data['hub.topic'] = $subscribe_to;
$data['hub.lease_seconds'] = $lease_in_seconds; //currently 1500
//URLfiy it.
foreach($data as $key => $value){
$post_items[] = $key . '=' . $value;
}
$post_string = implode ('&', $post_items);
//Add args.
curl_setopt($curl_connection, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($curl_connection, CURLOPT_HTTPHEADER, $curl_header);
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, TRUE);
//Let curl_exec return a string instead of only bool.
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, TRUE);
$httpcode = curl_getinfo($curl_connection, CURLINFO_HTTP_CODE);
if($httpcode == "202"){
$subscription_result = TRUE;
}
In the $callback_address I do the following (also as shown in the forums):
if(isset($_GET['hub_challenge']) && !empty($_GET['hub_challenge'])){
$r = $_GET['hub_challenge'];
echo $r;
}
What happens is the following:
- I execute the subscribe topic request.
- I receive the ‘hub_challenge’ (for me it is ‘hub_challenge’ not ‘hub.challenge’)
- I echo the ‘hub_challenge’.
But I receive nothing! No going online/offline notification, no stream title/game change…why?!
The callback address is not even called once after echoing the hub_challenge (I have a error_log(“script was called”) function there…what to do ?
In addition, to verify that my subscription to topic ‘stream_change’ was successful, I tried to get my active subscriptions for this client:
$curl_handle=curl_init();
//Define GET request header.
$content_header = array('Authorization: Bearer ' . $bearer);
curl_setopt_array($curl_handle, array(
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_URL => TWITCH_HELIX_API . '/webhooks/subscriptions?first=10',
CURLOPT_HTTPHEADER => $content_header,));
//Exec curl_handle, receive a JSON decoded result on success, FALSE otherwise.
$jsonUserObj = curl_exec($curl_handle);
//Successfully received a response.
if($jsonUserObj !== FALSE){
//JSON decode $result.
$subscription_data = json_decode($jsonUserObj);
}
And, unfortunately, but fully correctly, this is empty (or rather tells me that I have 0 active subscriptions).
Anyone having a clue what I am missing?
How long does it take until a subscription becomes “active”? I.e. changes are submitted?
Thanks a lot in advance!!
p.s. As mentioned before I could also go back to the polling behaviour but webhooks seem way nicer, so it would be cool to be able to use them.