Hello Guys,
I have successfully create chat API in PHP Script to get chat messages here is my working code.
<?php
set_time_limit(0);
$chan = "#channel";
$server = "irc.chat.twitch.tv";
$port = 6667;
$nick = "username";
$pass = "oauth:****************************"; // actual oauth is used in script.
$socket = fsockopen($server, $port,$errno,$errstr,15);
//echo $errno."\n".$errstr."\n"; // echoes "0 and no string. no errors connecting
fwrite($socket,"PASS ".$pass."\r\n"); // have tried PASS after USER/NICK, no changes
//fwrite($socket,"USER ".$nick."\r\n"); // have tried both fwrite and fputs. no difference in results.
fwrite($socket,"NICK ".$nick."\r\n");
// have tried NICK instead of USER, as well as both NICK and USER, and visa versa. still errors ":tmi.twitch.tv NOTICE * :Login unsuccessful"
// will only not error if only USER is sent.
//echo var_dump($socket); // resource(4) of type (stream) - what one would expect from an open socket.
fwrite($socket,"JOIN ".$chan."\r\n"); // have also tried sending all commands without "\r", only "\n" character. makes no difference
while(1) {
//echo "starting pull loop\n"; // prints on command prompt once
while($data = fgets($socket,256)) { // Script never gets past this point, unless login error, then it does 1 time.
//echo $data; // does not print unless login error
//exit;
$file = fopen("testfile.txt",'a+');
fwrite($file,$data."\r\n");
fclose($file);
$ex = explode (' ', $data);
if($ex[0] == "PING"){
fputs($socket, "PONG ".$ex[1]."\r\n");
}
}
}
?>
Now the thing here is, I am appending the file testfile.txt and its appending messages something like this
:tmi.twitch.tv 001 username :Welcome, GLHF!
:tmi.twitch.tv 002 username :Your host is tmi.twitch.tv
:tmi.twitch.tv 003 username :This server is rather new
:tmi.twitch.tv 004 username :-
:tmi.twitch.tv 375 username :-
:tmi.twitch.tv 372 username :You are in a maze of twisty passages, all alike.
:tmi.twitch.tv 376 username :>
:ceepee100!ceepee100@ceepee100.tmi.twitch.tv PRIVMSG #swagg :"you gotta take me to the loadout, I'm an expensive bich rn"
So my first question is how can I make json here and append all the messages in my “messages” json something like this
{
"messages": ["message1", "message2"]
}
And my second question is I do not want to add messages like
:tmi.twitch.tv 001 username :Welcome, GLHF!
I am trying to comment below code to remove above message …
fwrite($socket,"PASS ".$pass."\r\n");
fwrite($socket,"NICK ".$nick."\r\n");
fwrite($socket,"JOIN ".$chan."\r\n");
But then obviously not working … Can someone guide me to solve or udate my API please …