将 gitblog 的博客内容搬迁到 优快云

本文介绍了一种使用PHP脚本将Markdown格式的博客文章自动迁移到优快云平台的方法。通过简单的程序实现了博客文章的批量上传,并提供了判断文章是否成功发布的依据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

写博客其实已经好久了,但一直没找到舒服的写博方式,今天在转优快云,看到优快云的博客也可以用MARKDOWN下了,瞬间想要把以前的博客转过来的冲动,所以就花了些时间写了个程序自动搬迁了,程序很简单,只是为了记录下来,日后还会搬迁一些其他的博客内容不是?

解决方案
先看下目录结构

HTTRequest.php 用于网络数据请求、抓取等的封装类(做过模拟请求的应该都知道)
优快云.php 用于自动发布 csdn markdown 博客
demo.php 执行程序,可在 cli 下执行 php demo.php 测试(这里以 gitblog 的博客为例)

接下来看 优快云.php

<?php
class csdn
{
    public function publish($data, $type = 'original')
    {
        $uri = '/mdeditor/setArticle';// 发布博客的URI
        $data['type'] = $type;// 类型,原创(original),转载,翻译
        $data['status'] = 0;
        $data['level'] = 0;
        $data['channel'] = 17;
        $data['type'] = 'original';
        $data['articleedittype'] = 1;
        return $this->request($uri, $data);
    }

    private function request($uri, $data = null)
    {
        $httpRequest = new HTTPRequest('write.blog.youkuaiyun.com');
        $httpRequest->setRequestUri($uri);
        if($data) {
            $httpRequest->setType('POST');
            $httpRequest->setData($data);
        }       
        $httpRequest->setHeader('Referer', 'http://write.blog.youkuaiyun.com/mdeditor');
        $httpRequest->setHeader('cookie', '这里保证是你自己的博客COOKIE');
        $httpRequest->execute();
        return $httpRequest->getResponseText();
    }
}

调用方式

include_once('HTTPRequest.php');
include_once('csdn.php');
$csdn = new csdn;
$post['title'] = urlencode('this is title');
$post['markdowncontent'] = urlencode('this is markdowncontent');
$post['content'] = urlencode('this is content');
$post['tags'] = urlencode('this is tags');
$post['description'] = urlencode('this is description');
$post['categories'] = urlencode('this is categories');

$result = $csdn->publish($post);

然后就可以根据 result 来判断是否发布成功了,比如如下方式

$result = json_decode($csdn->publish($post), 1);
if($result['status'] == 'success') {
    echo 'publish success' . PHP_EOL;
} else {
    echo 'publish failed' . PHP_EOL;
    file_put_contents('log.txt', $val, FILE_APPEND);
}
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值