火车头采集ecshop 文章接口文件

<?php
header("Content-Type:text/html;charset=utf-8");

$host = $_SERVER['HTTP_HOST'];
//if ($host!="localhost" && $host!="25-99.com" && $host!="www.25-99.com") die("域名未授权");

define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');

$basepath = ROOT_PATH;

$act = $_GET["action"];
if ($act=="post"){
    //发布文章
    extract ( $_POST, EXTR_OVERWRITE ); // 格式化post参数
    if (!$cat_id){
        if (stripos($catname,":")<1){ exit( "分类不正确" );}
    }
    if (! $title) { exit ( "标题不能为空" ); }
    if (! $content) { exit ( "内容不能为空" ); }

    /* 字段列表 */
    $field_list = array("title","content","author","author_email", "is_open", "keywords","cat_id","description");

    $field_arr = array(
        'add_time'      => gmtime(),
    );
    
    $field_arr['title'] = $title;
    $field_arr['content'] = GetCurContent($content);
    $field_arr['author'] = $author;
    $field_arr['author_email'] = $author_email;
    $field_arr['keywords'] = $keywords;
    $field_arr['description'] = $description;
    $field_arr['cat_id'] = $cat_id;
    $field_arr['is_open'] = 1;
    $field_arr['article_type'] = "0";
    //如果上传了分类,则使用该分类
    if (!empty($catname)){
        $pid = "0";
        $cid = "0";
        $cats = explode(":",$catname);
        foreach ($cats AS $cat){
            if (!empty($cat) && $cat!=""){
                $sql = "SELECT cat_id FROM " . $ecs->table('article_cat')." where cat_name='$cat' and parent_id=$pid";
                $res=$db->query($sql);
                $row = $db->fetchRow($res);
                if (!empty($row))
                {
                    $cid = $row["cat_id"];
                    $pid = $cid;
                }
                else
                {
                    $sql = "INSERT INTO " . $ecs->table('article_cat') . " (cat_name,parent_id) VALUES ('" . addslashes($cat) . "','".$pid."')";
                    $db->query($sql);
                    $cid = $db->insert_id();
                    $pid = $cid;
                }
            }
        }
        $field_arr['cat_id'] = $cid;
    }
    
    
    
    /* 给文章最后增加三个带有超链接的关键词 */
    //取出关键字表的数据
    $sql="select * from ". $GLOBALS['ecs']->table('content_key') ;
    $res_k=$GLOBALS['db']->getAll($sql);
    //从关键字中随机取出三个数组
    $random_keys=array_rand($res_k,3);
    $field_arr['content'] = $field_arr['content'].'<br/><br/>华企商城更多商品介绍:<a href="' .$res_k[$random_keys[0]]['key_url']. '">' .$res_k[$random_keys[0]]['key_name']. '</a> &nbsp;&nbsp;&nbsp;&nbsp;<a href="' .$res_k[$random_keys[1]]['key_url']. '">' .$res_k[$random_keys[1]]['key_name']. '</a>&nbsp;&nbsp;&nbsp;&nbsp;<a href="' .$res_k[$random_keys[2]]['key_url']. '">' .$res_k[$random_keys[2]]['key_name']. '</a>';
    
    
    $db->autoExecute($ecs->table('article'), $field_arr, 'INSERT');
    $gid = $db->insert_id();
    
    echo("文章发布成功");
}else{
    //获取分类
    $sql = "select cat_id,parent_id,cat_name from ".$ecs->table('article_cat');
    $rs = $db->query($sql);
    if(!$rs){die("valid result!");}
    echo("<select>");
    while($row = $db->fetchRow($rs)){
        if($row["parent_id"]==0){
            echo "<option value='".$row["cat_id"]."'>".$row["cat_name"]."</option>";
        }else{
            echo "<option value='".$row["cat_id"]."'>----".$row["cat_name"]."</option>";
        }
    }
    echo("</select>");
}



/**
  * 得到远程图片
  *
  * @access  public
  * @param   goods_desc      $goods_desc         要处理的内容
  * @return  mix         如果成功返回缩略图的路径,失败则返回false
  */
function GetCurContent($goods_desc)
{
    $body = stripslashes($goods_desc);
    $img_array = array();
    //$body = ereg_replace("HTTP://".$_SERVER['SERVER_NAME']."/","",$body);//将源文件进行替换
    preg_match_all("/src=[\"|'|\s]{0,}(http:\/\/([^>]*)\.(gif|jpg|png))/isU",$body,$img_array);
    $img_array = array_unique($img_array[1]);

    set_time_limit(0);
    $imgurl = "bdimages/upload1/".date('Ymd');
    $imgpath = ROOT_PATH.$imgurl;

    $millisecond = date("YmdHms");
    if (!file_exists($imgpath))
    {
        if (!make_dir($imgpath))
        {
            return false;
        }
    }
    else
    {

        foreach($img_array as $key =>$value)
        {
            $value = trim($value);
            $get_file = @file_get_contents($value);
            $rndfilename = $imgpath."/".$millisecond.$key.".".substr($value,-3,3);
            $fileurl = "http://".$_SERVER['SERVER_NAME']."/".$imgurl."/".$millisecond.$key.".".substr($value,-3,3);
            //             $fileurl = "/".$imgurl."/".$millisecond.$key.".".substr($value,-3,3);
            if($get_file)
            {
                $fp = @fopen($rndfilename,"w");
                @fwrite($fp,$get_file);
                @fclose($fp);
            }
            //echo $rndfilename."<br/>".$fileurl."<br/>";
            $body = str_replace($value,$fileurl,$body);;
            echo $body;
        }
    }
    $body = addslashes($body);
    return $body;
}

?>

 

posted @ 2016-12-29 10:44  Abner3721  阅读(1213)  评论(0编辑  收藏  举报