Search for posts tagged with: Google

Avatar

最近有打算在php下实现一个gtalk的机器人,google了一下发现在Google的code站中已经有一个比较成熟的脚本了-XMPPHP

使用非常简单

<?php

// activate full error reporting
//error_reporting(E_ALL & E_STRICT);

include ‘XMPPHP/XMPP.php’;

#Use XMPPHP_Log::LEVEL_VERBOSE to get more logging for error reports
#If this doesn’t work, are you running 64-bit PHP with < 5.2.6?
$conn = new XMPPHP_XMPP(’talk.google.com’, 5222, ‘username’, ‘password’, ‘xmpphp’, ‘gmail.com’, $printlog=false, $loglevel=XMPPHP_Log::LEVEL_INFO);

try {
$conn->connect();
$conn->processUntil(’session_start’);
$conn->presence();
$conn->message(’someguy@someserver.net’, ‘This is a test message!’);
$conn->disconnect();
} catch(XMPPHP_Exception $e) {
die($e->getMessage());
}
?>

我需要的是能够接受我指令的代码,依然带了一个实例程序

修改了下,让他帮我去网站上查询一个城市当天的天气

<?php

// activate full error reporting
//error_reporting(E_ALL & E_STRICT);

include ‘XMPP.php’;

function get_url($city)
{
$url = ‘http://search.weather.com.cn/static/url_gb.php?CityInfo=’;
$url=$url.$city;
#$content = file_get_contents($url);
$ch = curl_init($url);
ob_start();
curl_exec($ch);
curl_close($ch);
$content = ob_get_contents();
ob_end_clean();
$con =htmlspecialchars($content);
$url = strstr($con,’URL’);
preg_match(”(\d+)”,$url,$matches);

$rrr = ‘http://www.weather.com.cn/html/weather/’.$matches[0].’.shtml’;
return $rrr;
}
function get_wether_info($url)
{
#$contents = file_get_contents($url);
$ch = curl_init($url);
ob_start();
curl_exec($ch);
curl_close($ch);
$contents = ob_get_contents();
ob_end_clean();
$w=strstr($contents,’box_contentwea’);
$ww = strstr($w,’strong’);
$sun = substr($ww,7,100);
$next_tag = stripos($sun,’</strong>’);
$wether = substr($sun,0,$next_tag);
$ww = strstr($w,’no_today’);
$ww = substr($ww,0,100);
$next_tag = stripos($ww,’</em>’);
$tem = substr($ww,10,$next_tag-10);
$win=substr($ww,$next_tag+8,60);
$next_tag = stripos($win,’<em>’);
$win = substr($win,$next_tag,60-$next_tag);
$next_tag = stripos($win,’</em>’);
$win = substr($win,4,$next_tag-4);
$info = $wether.’ ‘.$tem.’ ‘.$win;

return $info;
}

ignore_user_abort();
set_time_limit(0);
#Use XMPPHP_Log::LEVEL_VERBOSE to get more logging for error reports
#If this doesn’t work, are you running 64-bit PHP with < 5.2.6?

$conn = new XMPPHP_XMPP(’talk.google.com’, 5222, ‘nonewithme’, ‘×××××××’, ‘xmpphp’, ‘gmail.com’, $printlog=true, $loglevel=XMPPHP_Log::LEVEL_INFO);
$conn->autoSubscribe();

try {
$conn->connect();
while(!$conn->isDisconnected()) {
$payloads = $conn->processUntil(array(’message’, ‘presence’, ‘end_stream’, ’session_start’));
foreach($payloads as $event) {
$pl = $event[1];
switch($event[0]) {
case ‘message’:
print “———————————————————————————\n”;
print “Message from: {$pl['from']}\n”;
if($pl['subject']) print “Subject: {$pl['subject']}\n”;
print $pl['body'] . “\n”;
print “———————————————————————————\n”;
if($pl['body'] == ‘quit’) $conn->disconnect();
$key = “(\Aw )”;
if(preg_match($key,$pl['body']))
{
$len=strlen($pl['body']);
$city = substr($pl['body'],2,$len-2);
$url = get_url($city);
$info = get_wether_info($url);
$conn->message($pl['from'], $body=”\”{$info}\”.”, $type=$pl['type']);
}
else
{
$conn->message($pl['from'], $body=”Thanks for sending me \”{$pl['body']}\”.”, $type=$pl['type']);
}

if($pl['body'] == ‘break’) $conn->send(”</end>”);
break;
case ‘presence’:
print “Presence: {$pl['from']} [{$pl['show']}] {$pl['status']}\n”;
break;
case ’session_start’:
print “Session Start\n”;
$conn->getRoster();
$conn->presence($status=”Cheese!”);
break;
}
}
}
} catch(XMPPHP_Exception $e) {

die($e->getMessage());
}
?>

直接发送”w “+城市名(拼音)/城市zip

如“w shanghai”

Tagged with: , , , .