用C语言重写php代码(一百多行代码),快的话半天内完成(紧急)

zb1480649628460 发布于 2016/12/02 11:38
阅读 21
收藏 0

项目基于Linux服务器,需要用C语言重写php代码,代码只有一百多行,实现的功能是把数据放入内存,然后读取出来,php的代码如下:

<?php

/**

 * 计划任务

 * 实时获取聊天记录和上墙记录

 * 保存至memcached中

 */

// 连接msyql

// $conn = mysql_pconnect("localhost", "root", "123456");

$conn = mysql_connect("localhost", "spcom", "spcom123111");

if (!$conn) {

    err('Unable to connect to DB: ' . mysql_error());

}

// 选择数据库

if (!mysql_select_db("aispcom")) {

    err('Unable to select aispcom: ' . mysql_error());

}

// memcache

$memcache = memcache_pconnect('localhost', 11211);

if(!$memcache) {

    err('msg : Connection to memcached failed');

}

// 获取所有私聊消息 start ------------------------------

$start_time = strtotime(date("Y-m-d 18:00:00",strtotime("-7 day")));

$sql = 'select s.id, s.weid, s.rid, s.from_openid, s.to_openid, s.flag, s.image, s.content, s.createtime, f.nickname as f_nickname, f.headimgurl as f_headimgurl, f.sex as f_sex from ims_weixin_slmsg as s left join ims_weixin_fans as f on s.from_openid = f.openid where s.isshow = 1 and s.createtime between ' . $start_time . ' and ' . $_SERVER['REQUEST_TIME'] . ' order by s.createtime desc';

// pt($sql);

$result = mysql_query($sql);

if (!$result) {

    err('msg : run query ('.$sql.') from DB: ' . mysql_error());

}

// 没有数据

if (mysql_num_rows($result) == 0) {

    err('msg : No data');

}

// 数据数组

$array = $vals = array();

$i = 0;

while ($row = mysql_fetch_assoc($result)) {

    $row['f_nickname'] = empty($row['f_nickname'] ) ? '夜咖会员' . $row['id'] : $row['f_nickname'];

    $row['f_headimgurl'] = empty($row['f_headimgurl'] ) ? '/Public/chat/images/logo.jpg' : $row['f_headimgurl'];

    $row['formattime'] = formatDatetime($row['createtime']);    // 对时间的格式化

    $row['content'] = replaceEmoSrc($row['content']);   // 对内容表情处理

    $array[] = $row;

    $vals[$i++] = $row['createtime'];

}

// 存入memcached

if($array) {

    // pt($array);

    // 对数组逆向排序

    array_multisort($vals, SORT_ASC, $array);

    // pt($array);

    $memcache->set("slmsg", $array);

} else {

    err('msg : No array');

}

mysql_free_result($result);

// 获取所有私聊消息 end ------------------------------

// 获取所有上墙消息 start ------------------------------

// 按天数查询

$start_time = strtotime(date("Y-m-d 18:00:00",strtotime("-2 day")));

$sql = 'select w.id, w.rid, w.content, w.image, w.type, w.flag, w.createtime, w.ispb, w.pbtime, w.bpfor, w.cftime, w.iscfbp, w.shout_nickname, f.openid, f.nickname, f.headimgurl, f.sex, g.isblacklist from ims_weixin_wall as w left join ims_weixin_fans f on w.openid = f.openid left join ims_weixin_flag g on w.openid = g.openid and w.rid = g.rid where w.isshow = 1 and w.createtime between ' . $start_time . ' and ' . $_SERVER['REQUEST_TIME'] . ' order by w.createtime desc';

$result = mysql_query($sql);

if (!$result) {

    err('wall : run query ('.$sql.') from DB: ' . mysql_error());

}

// 没有数据

if (mysql_num_rows($result) == 0) {

    err('wall : No data');

}

// 数据数组

$array = $vals = array();

$j = 0;

while ($row = mysql_fetch_assoc($result)) {

    $row['nickname'] = empty($row['nickname'] ) ? '夜咖会员' . $row['id'] : $row['nickname'];

    $row['headimgurl'] = empty($row['headimgurl'] ) ? '/Public/chat/images/logo.jpg' : $row['headimgurl'];

    $row['formattime'] = formatDatetime($row['createtime']);    // 对时间的格式化

    $row['content'] = replaceEmoSrc($row['content']);   // 对内容表情处理

    $row['friend_content'] = '';    // 为TA霸屏时TA的发言

    if(($row['ispb'] == 1) && ($row['bpfor'] != '')) {

        $exp = explode('|', $row['content']);

        if(!empty($exp[1])) {

            $row['content'] = $exp[0];

            $row['friend_content'] = $exp[1];

        }

    }

    $array[] = $row;

    $vals[$j++] = $row['createtime'];

}

// pt($array);

// 存入memcached

if($array) {

    // 对数组逆向排序

    array_multisort($vals, SORT_ASC, $array);

    // 循环把相同的酒吧单独做为一个集合

    $item = array();

    foreach($array as $v){

        $item[$v['rid']][] = $v;

    }

    // pt($item);

    // 把每个酒吧集合分别保存

    foreach ($item as $k => $v) {

        // memcache_delete($memcache, 'wall_'.$k);

        $memcache->set('wall_'.$k, $v);

    }

} else {

    err('wall : No array');

}

mysql_free_result($result);

// $memcache->flush();

// $memcache->close();

// 获取所有上墙消息 end ------------------------------

// 发生错误

function err($info) {

    $filepath = dirname(__FILE__) . '/logs/cron_aisp_' . date('Ymd', $_SERVER['REQUEST_TIME']) . '.txt';

    // 是否存在

    if(!file_exists($filepath)) {

        $handle = fopen($filepath, 'w');

        fwrite($handle, "\r\n init ... \r\n");

        fclose($handle);

    }

    // 添加至开头

    $filetowt = fopen($filepath, 'r+');

    $string = '[' . date('Y-m-d H:i:s', time()) . "]\r\n" . $info . "\r\n-----------------\r\n" . file_get_contents($filepath);

    fwrite($filetowt, $string);

    fclose($filetowt);

    exit;

}

// 调试打印

function pt($arr) {

    echo "<pre>";

    print_r($arr);

exit;

}

// 替换消息内容中表情符号

function replaceEmoSrc($content){

    $emo = array("[飞吻]","[勾引]","[哈欠]","[切]","[超人]","[色]","[呲牙]", "[发怒]", "[双喜]", "[快哭了]", "[5V]", "[糗大了]", "[月亮]", "[憨笑]", "[NO]", "[爱心]", "[阴险]", "[吐]", "[扮鬼脸]", "[流泪]", "[睡着了]", "[哭笑]", "[吓]", "[下雨]", "[委屈]", "[酷]", "[傲慢]", "[晕]", "[敲打]", "[咒骂]", "[礼物]", "[心碎]", "[抠鼻]", "[给力]", "[衰]", "[鄙视]", "[闭嘴]", "[猪头]", "[亲亲]", "[鼓掌]");

    foreach($emo as $key => $val){

        if((strpos($content, $val) !== false)){

            $imgurl = '/addons/meepo_bigerwall/template/mobile/newmobile/common/emon/' . ($key + 1) . '.png';

            $replace = '<img class="txtImg" src="' . $imgurl . '" alt="">';

            $content = str_replace($val, $replace, $content);

        }

    }

    return $content;

}

// 日期时间转换

function formatDatetime($datetime)

{

    if($datetime) {

        $now = date("Ymd",time());  // 今天日期

        $yes = date("Ymd",strtotime("-1 day")); // 昨天日期

        $str = date("Ymd",$datetime);   // 判断日期

        switch ($str) {

            case $now:

                $dt = date("H:i",$datetime);

                break;

            case $yes:

                $dt = '昨天 ' . date("H:i",$datetime);

                break;

            default:

                $dt = date("m-d H:i",$datetime);

                break;

        }

        return $dt;

    } else {

        return false;

    }

}

加载中
返回顶部
顶部