Alright, so first of all, i'm still kind of a newbie in PHP.
I'm trying to make a chatbot that responds to the keyword "shut up" from people that tell that to the bot in my game server, but I can't seem to find a way how...
Here's the bot's original code...
<?php
$choosechar = "#43CC#1#35#ItsJustABot#%";
$fh = fopen('badtimetim.txt','r');
$word_array = array(fgets($fh));
$word = rand(0,58);
$lines = file("badtimetim.txt");
while ($line = fgets($fh)) {
// <... Do your work with the line ...>
// echo($line);
// Connect to the AO Server
if (!($fp = fsockopen("127.0.0.1", "27017", $errno, $errstr, 15))) {
die("Failed to connect. Doesn't seem like the server is up anyway?");
}
// Set timeout to 1 second
if (!stream_set_timeout($fp, 1)) die("Could not set timeout.");
// Fetch first line of response and echo it
echo fgets($fp);
// Say line and repeat
fwrite($fp, $choosechar);
fwrite($fp, "#4D90#chat#(a)dolannormal#Dolan#dolannormal#".$lines[array_rand($lines)]."#jud#1#1#0#0#0#0#35#0#1#%");
sleep(120);
// Stuff
echo fgets($fp);
}
fclose($fh);
What i'm exactly trying to achieve here is when the bot detects this (asterisks should be wildcards or something):
#4D90#chat#*#*#*#shut up#*#*#*#*#*#*#*#*#*#*#%
I want the bot to send this data to the server in response using fwrite:
#4D90#chat#(a)dolanangry#Dolan#dolanangry#no#jud#1#1#0#0#0#0#35#0#1#%
How do I do this? Any help is appreciated, thanks.
EDIT: Forgot to mention, i'm using a .bat file to run PHP and the PHP code and not a website.
EDIT2: Made question more specific
$input_string_with_shut_up=$_POST['chat_msg']
$output=str_replace('%shut up%','#4D90#chat#(a)dolanangry#Dolan#dolanangry#no#jud#1#1#0#0#0#0#35#0#1#%',$input_string_with_shut_up)
echo $output;
This might do
Related
HI I have posted a lot of question regarding this but it is different from this one. It's like a continuation or rather other part question.
I have a card vending device (CVD), Now my laptop has no serial port so I used the USB to connect to the device.
My cable is, USB to RS232. Now I have downloaded or rather given to me by a friend an exe which send a command to device on a click of a button. But note that this is an exe only. No Source code.
I'm now creating my own program to send the data command to device on a button click as well.
Here's some facts:
1. Im using Php
2. I'm need to send a hex command of 42
3. is the command for cardout
Here's what I've got so far.
exec("mode COM15 BAUD=9600 PARITY=none data=8 stop=1 xon=off");
$fp = #fopen('COM2', "w+");
if (!$fp) {
echo "Not open";
} else {
sleep(3);
echo "Open";
$str = "01";
$str = pack("H*",$str);
fwrite($fp, $str);
fputs($fp, $str);
$str2 = "2A";
$str2 = pack("H*",$str2);
fwrite($fp, $str2);
fputs($fp,$str2);
$buff = fread($fp, 10);
echo ">> ".$buff." <<";
//fclose($fp);
}
But it is just returning Open>> <<
Try with a
print_r($buff);
It will show you the type of variable and all data it contains and maybe continue working on how to show that variable.
I need to search gmail messages (google apps for work) via imap/php. However, imap_search criterias are not enough to catch the messages in question.
The code I use looks like this:
$imap = imap_open("{imap.gmail.com:993/imap/ssl}Label1/label2", $user_email, $user_passwd);
$msg_list = imap_search($imap, 'TEXT "Delivered-To: username+label#gmail.com"');
imap_close($imap);
The imap_search call didn't return anything.
I did some research, it seems I can filter messages based on "Delivered-To" header field via the gmail search syntax X-GM-RAW, but I just couldn't achieve this, I tried all these calls (and more):
$msg_list = imap_search($imap, 'UID SEARCH X-GM-RAW "deliveredto:username+label#gmail.com"');
$msg_list = imap_search($imap, 'SEARCH X-GM-RAW "deliveredto:username+label#gmail.com"');
$msg_list = imap_search($imap, 'X-GM-RAW "deliveredto:username+label#gmail.com"');
But it didn't work, anyone knows what's wrong with my code?
OK, either I don't know how to ask questions, SOers are busy, or I'm asking difficult questions.
Anyway, now I know the imap_* functions built-into PHP don't handle direct IMAP commands, so I had to either use the zend framework (too heavy for my needs), or directly connect to imap via sockets.
I chose the second option, the code is as follow (code stolen from here and adapted for my own needs), in case someone needs it:
<?php
// Open a socket
if (!($fp = fsockopen('ssl://imap.gmail.com', 993, $errno, $errstr, 15))) die("Could not connect to host");
// Set timout to 1 second
if (!stream_set_timeout($fp, 1)) die("Could not set timeout");
// Fetch first line of response and echo it
echo fgets($fp);
// =========================================
fwrite($fp, "0001 LOGIN user.name#gmail.com YOUR_PASSWORD_HERE_WITHOUT_QUOTES\r\n");
// ie. fwrite($fp, "0001 LOGIN super.dude#gmail.com pass123\r\n");
// Keep fetching lines until response code is correct
while ($line = trim(fgets($fp)))
{
echo "Line = [$line]\n";
$line = preg_split('/\s+/', $line, 0, PREG_SPLIT_NO_EMPTY);
$code = $line[0];
if (strtoupper($code) == '0001') {
break;
}
}
// =========================================
fwrite($fp, "0002 SELECT Inbox\r\n");
// Keep fetching lines until response code is correct
while ($line = trim(fgets($fp)))
{
echo "Line = [$line]\n";
$line = preg_split('/\s+/', $line, 0, PREG_SPLIT_NO_EMPTY);
$code = $line[0];
if (strtoupper($code) == '0002') {
break;
}
}
// =========================================
fwrite($fp, "0003 SEARCH X-GM-RAW \"deliveredto:user.name+someLabel#gmail.com\"\r\n");
// Keep fetching lines until response code is correct
while ($line = fgets($fp))
{
echo "Line = [$line]\n";
$line = preg_split('/\s+/', $line, 0, PREG_SPLIT_NO_EMPTY);
$code = $line[0];
if (strtoupper($code) == '0003') {
break;
}
}
fclose($fp);
echo "I've finished!";
?>
Voila! Just copy and paste and now you have access to the gmail syntax right from PHP! (Hey vote if you like :p)
I'm writing an application that uses a .php script to get tweets using the twitter search API.
See below code:
<?php
$hashtag = 'hashtag'; // We search Twitter for the hashtag
$show = 25; // And we want to get 25 tweets
// Local path
$cacheFile = '../../_data/tweets.json.cache'; // A cachefile will be placed in _data/
$json = file_get_contents("http://search.twitter.com/search.json?result_type=recent&rpp=$show&q=%23" . $hashtag. "%20-RT") or die("Could not get tweets");
$fp = fopen($cacheFile, 'w');
fwrite($fp, $json);
fclose($fp);
?>
My problem is that I want to make sure that this script runs without fail, or if it does fail at least doesn't keep looping.
The script is going to be run automatically every 1 minute.
Would anyone know a good way to handle errors here?
TL;DR: How do I handle errors on my code?
in simple, use '#' prefix for function. It suppresses errors from displaying. Read More Here
<?php
$hashtag = 'hashtag'; // We search Twitter for the hashtag
$show = 25; // And we want to get 25 tweets
$cacheFile = '../../_data/tweets.json.cache'; // A cachefile will be placed in _data/
$json = #file_get_contents("http://search.twitter.com/search.json?result_type=recent&rpp=$show&q=%23" . $hashtag . "%20-RT");
if (!empty($json)) {
$fp = fopen($cacheFile, 'w');
fwrite($fp, $json);
fclose($fp);
} else {
echo "Could not get tweets";
exit;
}
?>
Here is the code that I am using:
if (!($fp = fsockopen('ssl://imap.gmail.com', '993', $errno, $errstr, 15)))
echo "Could not connect to host";
$server_response = fread($fp, 256);
echo $server_response;
fwrite($fp, "C01 CAPABILITY"."\r\n");
while (!feof($fp)) {
echo fgets($fp, 256);
}
I get the first response:
OK Gimap ready for requests from xx.xx.xx.xx v3if9968808ibd.15
but then the page times out. I have searched through stream_set_blocking, stream_set_timeout, stream_select, fread, etc. but could not get it to work. I need to read all the data that the server sends and then proceed with other commands (I would be retrieving emails using imap).
Thanks
Your script is hanging in the while loop at the end. This is because you have used !feof() as the condition for the loop, and the server is not closing the connection. This means the feof() will always return false and the loop will continue forever.
This will not be problem when your write a full implementation, as you will be looking for response codes and can break out of the loop accordingly, for example:
<?php
// Open a socket
if (!($fp = fsockopen('ssl://imap.gmail.com', 993, $errno, $errstr, 15))) {
die("Could not connect to host");
}
// Set timout to 1 second
if (!stream_set_timeout($fp, 1)) die("Could not set timeout");
// Fetch first line of response and echo it
echo fgets($fp);
// Send data to server
echo "Writing data...";
fwrite($fp, "C01 CAPABILITY\r\n");
echo " Done\r\n";
// Keep fetching lines until response code is correct
while ($line = fgets($fp)) {
echo $line;
$line = preg_split('/\s+/', $line, 0, PREG_SPLIT_NO_EMPTY);
$code = $line[0];
if (strtoupper($code) == 'C01') {
break;
}
}
echo "I've finished!";
Your script should be working. In fact, it is working.
See the results below on my pc when I ran your code:
* OK Gimap ready for requests from xx.xx.xx.xx l5if4585958ebb.20
* CAPABILITY IMAP4rev1 UNSELECT IDLE NAMESPACE QUOTA ID XLIST CHILDREN X-GM-EXT-1 XYZZY SASL-IR AUTH=XOAUTH
C01 OK Thats all she wrote! l5if4585958ebb.20
Since gmail doesn't disconnect you. No end of file occurs. And the page loading simply times out.
In other words: Your script will just keep waiting and waiting until gmail does disconnect, which unfortunately happens after your page load has already timed out.
starting from this point: http://www.mattakis.com/blog/kisg/20090708/broadcasting-video-with-android-without-writing-to-the-file-system
I'm trying to create an application to save a video stream from mobile camera to a remote server.
(I found several examples in google code for android part: ipcamera-for-android, spydroid-ipcamera, etc..)
I read some answers here and around the network, but can not find the solution about how to "read" and save the stream of data on the server side.
My knowledge of java is poor, so I'd rather be able to create server-side script in PHP (using server sockets, or other stuffs). Someone can help on this part?
UPDATE
using my little knowledge of tools such as mplayer / ffmpeg mencorer I'm able to save the video stream ...for example using ipcamera-for-android and its nanohttp server using on server side:
ffmpeg-i "http://{ip of android phone}:8080/live.flv" /my/server/path/stream.flv
However, can only be used in LAN,
I need that mobile connect server and not viceversa.
UPDATE 2
Some progress.. using this script on server side
#!/usr/bin/php5
<?php
$handle = fopen("stream.3gp","w");
$socket = stream_socket_server("tcp://192.168.0.102:9000", $errno, $errstr);
if ($socket)
{
echo "start listening\n";
while ( $conn = stream_socket_accept($socket, 180))
{
echo "phone connected\n";
while ($chunk = stream_socket_recvfrom($conn, 1500))
{
fwrite($handle,$chunk);
}
}
}
fclose($handle);
fclose($socket);
?>
however, 3gp file is not yet playable..
UPDATE 3
#!/usr/bin/php5
<?php
$socket = stream_socket_server("tcp://192.168.0.102:9000", $errno, $errstr);
$file = "saved.3gp";
$threegp_header = "\x00\x00\x00\x18\x66\x74\x79\x70\x33\x67\x70\x34\x00\x00\x03\x00\x33\x67\x70\x34\x33\x67\x70\x36";
$four_bytes = "\x00\x00\x00\x00";
if (!$socket) {
echo "$errstr ($errno)\n";
} else {
echo "server start listening\n";
while ( $conn = #stream_socket_accept($socket, 180))
{
echo "phone connected\n";
$handle = fopen($file,"w");
//mediaRecorder gives invalid stream header, so I replace it discarding first 32 byte, replacing with 28 good byte (standard 3gp header plus 4 empty bytes)
$discard = stream_get_contents($conn, 32);
fwrite($handle, $threegp_header);
fwrite($handle, $four_bytes);
//then confinue to write stream on file until phone stop streaming
while(!feof($conn))
{
fwrite($handle, stream_get_contents($conn, 1500));
}
echo "phone disconnected\n";
fclose($handle);
//then i had to update 3gp header (bytes 25 to 28) with the offset where moov atom starts
$handle = fopen($file,"c");
$output = shell_exec('grep -aobE "moov" '.$file);
$moov_pos = preg_replace('/moov:(\d+)/i', '\\1', $output);
$moov_pos_ex = strtoupper(str_pad(dechex($moov_pos - 24), 8, "0", STR_PAD_LEFT));
fwrite($handle, $threegp_header);
$tmp = '';
foreach(str_split($moov_pos_ex,2) as $hex)
{
$tmp .= pack('C*', hexdec($hex));
}
fwrite($handle, $tmp);
fclose($handle);
}
echo "phone disconnected\n";
}
#fclose($handle);
fclose($socket);
?>
after some experiments, this time vlc/mplayer seems that can play it.. still some problems with audio (but i think i've something wrong on android side)
You're probably going to want to use PHP's server socket functionality.
Here's a handy tutorial which goes through what you need to do in order to implement data streaming.
Depending on the incoming stream (protocol, etc.) you've ended up or want to end up using:
I'm not sure what you are willing to use/install on the LAMP, or what you'd prefer, but I know VLC can easily capture an incoming stream.
http://wiki.videolan.org/Documentation:Streaming_HowTo/Receive_and_Save_a_Stream
Of course, the Command Line only version of VLC is probably what you want. I've never done it, not sure how that works, I would hope it doesn't install a metric ton of extra packages. This is something to look at concerning possible concerns.