# (Resolved)Creating a chat bot in PHP CLI - not pulling any information

**URL:** <https://discuss.dev.twitch.com/t/resolved-creating-a-chat-bot-in-php-cli-not-pulling-any-information/6262>\
**Category:** Chat and IRC\
**Created:** [May 25, 2016, 8:58pm UTC](https://discuss.dev.twitch.com/t/resolved-creating-a-chat-bot-in-php-cli-not-pulling-any-information/6262 "2016-05-25T20:58:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![DecibleBreaker](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/deciblebreaker/32/1755_2.png) [@DecibleBreaker](https://discuss.dev.twitch.com/u/DecibleBreaker)\
**Post date:** [May 25, 2016, 8:58pm UTC](https://discuss.dev.twitch.com/t/resolved-creating-a-chat-bot-in-php-cli-not-pulling-any-information/6262/1 "2016-05-25T20:58:59Z")

</div>

Simply put, I’m starting from the bottom up, building a PHP chatbot. Right now, the only thing I’m trying to get it to do, is connect, join a channel, and take all messages as received and just dump it all into a file, no more, no less.

Since, although I understand the basics of IRC, I’d like to see what a raw chatlog looks like, in order to figure out just how i want to parse it.

From what I see from here: [http://help.twitch.tv/customer/en/portal/articles/1302780](http://help.twitch.tv/customer/en/portal/articles/1302780)  
I don’t see what I’m doing wrong.

After an fsockopen(), I attempt to log in. The link shows PASS and NICK being used, but when I use that, I get an error “:tmi.twitch.tv NOTICE \* :Login unsuccessful” when I use PASS and USER as you’ll see in my script, it throws no errors, but I can’t seem to even pull the MOTD. Because I do get the error message, I know that fgets() is working,

```
<?php
set_time_limit(0);

$chan = "#deciblebreaker";
$server = "irc.chat.twitch.tv";
$port = 6667;
$nick = "deciblebreaker";
$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
		$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");
		}
	}
}
?>

```

And yes, I’m aware that the code is far from ideal. I know I’m opening and closing the dumpfile repeatedly, and there is a reason it’s being done like that for now. At the moment, all I want it to do is pull and write. I am completely clueless as to what is preventing me from pulling data from the channel. no MOTD, no chat from the channel, nothing.

---

<div class="post-metadata">

**Author:** ![3ventic](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/3ventic/32/298_2.png) [@3ventic](https://discuss.dev.twitch.com/u/3ventic)\
**Post date:** [May 25, 2016, 10:22pm UTC](https://discuss.dev.twitch.com/t/resolved-creating-a-chat-bot-in-php-cli-not-pulling-any-information/6262/2 "2016-05-25T22:22:21Z")

</div>

```auto
PASS oauth:token\r\n
NICK username\r\n

```

no `USER` required. Login unsuccessful would suggest your oauth token is invalid or doesn’t match the username. Your code works fine for me (changing USER to NICK).

---

<div class="post-metadata">

**Author:** ![DecibleBreaker](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/deciblebreaker/32/1755_2.png) [@DecibleBreaker](https://discuss.dev.twitch.com/u/DecibleBreaker)\
**Post date:** [May 26, 2016, 1:33am UTC](https://discuss.dev.twitch.com/t/resolved-creating-a-chat-bot-in-php-cli-not-pulling-any-information/6262/3 "2016-05-26T01:33:22Z")

</div>

Thank you.  
I have no idea why, but my oauth was different than when I generated it. Got the new oauth in and everything is working clean.

---

<div class="post-metadata">

**Author:** ![TrainerTimmy](https://d3ovvz0v2ajtpu.cloudfront.net/user_avatar/discuss.dev.twitch.com/trainertimmy/32/1050_2.png) [@TrainerTimmy](https://discuss.dev.twitch.com/u/TrainerTimmy)\
**Post date:** [May 26, 2016, 1:36am UTC](https://discuss.dev.twitch.com/t/resolved-creating-a-chat-bot-in-php-cli-not-pulling-any-information/6262/4 "2016-05-26T01:36:18Z")

</div>

Also keep in mind that you _must_ put `\r\n` at the end of every line of text you send to the server

---

<div class="post-metadata">

**Author:** ![system](https://d2opxh93rbxzdn.cloudfront.net/original/2X/0/05909729875cce3b53e59d5561c4d23bb14948fc.png) [@system](https://discuss.dev.twitch.com/u/system)\
**Post date:** [June 25, 2016, 1:40am UTC](https://discuss.dev.twitch.com/t/resolved-creating-a-chat-bot-in-php-cli-not-pulling-any-information/6262/5 "2016-06-25T01:40:29Z")

</div>

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