<?php

set_time_limit(270);
error_reporting(E_ERROR | E_WARNING | E_PARSE);


$url = 'http://www.lycecho.com/api/getOriginal';
$token = '';//更换这里密钥
$v = 1;//1=兼容好 2=改写好 3=改写也好
$content_tag_name = '内容'; //内容字段名称
$title_tag_name = '标题'; //标题字段名称
$doTitle = 1; //伪原创标题 1伪原创 0 不伪原创

$headdd = '';
$taill = '';


$titlenum = 21;//一个中文3字符，这里21等于7个字，如果标题超过7个字，那么就截取前面7个字来匹配百度下拉
/*百度下拉有两种形式
*买的榴莲是脆的怎么办(百度下拉词1)
*买的榴莲是脆的怎么办_百度下拉词1
*第二种形式中间的特殊符号可以自定义
*/
$baidu_type = 1;//两种形式，分别对应1和2
$baidu_connect = "_";//处理标题时的特殊符号

$titlenum = empty($titlenum)?"21":$titlenum;



switch($LabelArray['PageType'])
{
    case 'List'://处理列表页，只能处理html
    break;
    case 'Pages'://处理多页，只能处理html
    break;
    case 'Content'://处理默认页，只能处理html
    break;
    case 'Save'://只有保存时是可以处理标签值的
        // 保存原文

    try {
        /**********************************************************************/
    // 这一步用来获取伪原创文章
        /**********************************************************************/


        $title = urlencode($LabelArray[$title_tag_name]);
        $r = json_decode(file_get_contents("http://www.baidu.com/sugrec?pre=1&p=1&ie=utf-8&prod=pc&csor=2&wd={$title}"),true);
        if(!empty($r['g'])){

            /***********************存在下拉的代码区：处理百度标题******************************/
        //判断获取到的百度下拉数量
            $baidu_num = count($r['g']);
        //如果百度下拉有一个以上就随机获取一个标题
            if($baidu_num != 1){
                $baidu_title = $r['g'][mt_rand(0,count($r['g'])-1)]['q'];
                $baidu_title = $baidu_title==$LabelArray[$title_tag_name]?$r['g'][mt_rand(0,count($r['g'])-1)]['q']:$baidu_title;
                $baidu_title = $baidu_title==$LabelArray[$title_tag_name]?$r['g'][mt_rand(0,count($r['g'])-1)]['q']:$baidu_title;
                $baidu_title = $baidu_title==$LabelArray[$title_tag_name]?$r['g'][mt_rand(0,count($r['g'])-1)]['q']:$baidu_title;
            //三重过滤后，组合标题
                if($baidu_type==1 && $baidu_title != $LabelArray[$title_tag_name]){
                    $LabelArray[$title_tag_name] = $LabelArray[$title_tag_name]."(".$baidu_title.")";
                }else if($baidu_type==2){
                    $LabelArray[$title_tag_name] = $LabelArray[$title_tag_name].$baidu_connect.$baidu_title;
                }
            }
            /***********************标题处理结束******************************/
        }

        /**********************************************************************/
    // 这一步用来获取伪原创文章
        /**********************************************************************/

        $content = $LabelArray[$content_tag_name];
        $resp = array();
        if($doTitle){
         $resp =  get_wyc_article($content,$LabelArray[$title_tag_name]);
         $LabelArray[$title_tag_name] = $resp['title'];
     }else{
        $resp =  get_wyc_article($content);
    }

    $LabelArray[$content_tag_name] = $headdd. $resp['content']. $taill;
}
catch (Exception $e) {
    $LabelArray['标题'] .= $e->getMessage();
    $LabelArray[$content_tag_name] .= $e->getMessage();
}
break;
default:
       
}

echo serialize($LabelArray);


function get_wyc_article($str,$title='') {
    global $url;
    global $token;
    global $v;
    $wyc = curl_request($url, array('content'=>$str,'title'=>$title,'token'=>$token,'v'=>$v,'locks'=>array('测试','测试2')));
    return $wyc['data'];
}



//参数1：访问的URL，参数2：post数据(不填则为GET)，参数3：提交的$cookies,参数4：是否返回$cookies
function curl_request($url,$post=''){
    if (! extension_loaded('curl')) {
        file_exists('./ext/php_curl.dll') && dl('php_curl.dll'); // 加载扩展
    }

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
    if (ini_get('open_basedir') == '' && strtolower(ini_get('safe_mode')) != 'on'){
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
    }

    

    if($post) {
        $post = json_encode($post);
        curl_setopt($curl, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json; charset=utf-8',
            'Content-Length: ' . strlen($post)
        ));
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
    }

        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);                   //https请求 不验证证书
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);                 //https请求 不验证hosts
        curl_setopt($curl, CURLOPT_TIMEOUT, 150);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $data = curl_exec($curl);
        if (curl_errno($curl)) {
            return curl_error($curl);
        }
        curl_close($curl);
        return json_decode($data,!0);
        
    }
