百度站点的主动推送

作者:谢高升 发布:2017-10-26 浏览:2640次

让百度站点很好的收录你的文章,就在你发布文章的时候推送到百度的平台上去,如果是靠百度的蜘蛛去爬的话可能是很慢的,

// 创建
    public function actionCreate()
    {
        $model = new PostForm();
        // 定义场景
        $model->setScenario(PostForm::SCENARIOS_CREATE);
        
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            
            if (! $model->create()) {
                // echo 123;exit;
                Yii::$app->session->setFlash('warning', $model->_lastError);
            } else {
            //文章创建之后推送到百度的接口
                $this->postTobaidu($model->id);
                return $this->redirect([
                    'post/view',
                    'id' => $model->id
                ]);
            }
        }
        // 查询分类
        $cat = CatModel::getAllCats();
        return $this->render('create', [
            'model' => $model,
            'cat' => $cat
        ]);
    }
    
    /**
     * 百度主动推送
     */
    public function postTobaidu($id){
       /*  $urls = array(
            'https://xgs888.top/post/view?id=58',
            'http://www.example.com/2.html',
        ); */
        $api = 'http://data.zz.baidu.com/urls?site=xgs888.top&token=yourtoken';
        $ch = curl_init();
        $options =  array(
            CURLOPT_URL => $api,
            CURLOPT_POST => true,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POSTFIELDS => "https://xgs888.top/post/view?id=".$id,
            CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
        );
        curl_setopt_array($ch, $options);
        $result = curl_exec($ch);
        return $result;
    }


标签: 百度推送