Blog Details
PHP 写一个用cookie登陆百家号发布文章的完整代码,包括封面图片等操作
发布作者
Project
发布时间
20 June, 2022
Post by
Gilbert Smith
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium deleniti atque corrupti quos dolores et quas molestias excepturi sint quidem rerum facilis est
以下是完整的PHP实现方案,包含Cookie登录验证、文章发布和封面图片上传功能:
<?php
class BaijiahaoPublisher {
private $cookieFile = 'baijia_cookie.txt';
private $userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)';
public function __construct($cookieStr = '') {
if(!empty($cookieStr)) {
file_put_contents($this->cookieFile, $this->formatCookie($cookieStr));
}
}
private function formatCookie($raw) {
return str_replace(['; ', ';'], ';', $raw);
}
public function checkLogin() {
$ch = curl_init('https://baijiahao.baidu.com/builder/rc/content/list');
curl_setopt_array($ch, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_COOKIEFILE => $this->cookieFile,
CURLOPT_HTTPHEADER => ['User-Agent: '.$this->userAgent]
]);
$response = curl_exec($ch);
curl_close($ch);
return strpos($response, '我的内容') !== false;
}
public function uploadCover($imagePath) {
$boundary = '----'.md5(time());
$fileContent = file_get_contents($imagePath);
$filename = basename($imagePath);
$body = "--{$boundary}\r\n".
"Content-Disposition: form-data; name=\"file\"; filename=\"{$filename}\"\r\n".
"Content-Type: image/jpeg\r\n\r\n".
$fileContent."\r\n".
"--{$boundary}--";
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'https://baijiahao.baidu.com/builder/rc/api/upload/pic',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $body,
CURLOPT_HTTPHEADER => [
"Content-Type: multipart/form-data; boundary={$boundary}",
'Cookie: '.file_get_contents($this->cookieFile),
'User-Agent: '.$this->userAgent
],
CURLOPT_RETURNTRANSFER => true
]);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result, true);
}
public function publishArticle($title, $content, $coverUrl = '', $options = []) {
$defaults = [
'is_original' => 1,
'sync_to_wenku' => 0,
'cover_type' => empty($coverUrl) ? 1 : 2,
'cover_url' => $coverUrl,
'tag' => '',
'content_format' => 'html'
];
$params = array_merge($defaults, $options);
$params['title'] = $title;
$params['content'] = $content;
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => 'https://baijiahao.baidu.com/builder/rc-pub/article/publish',
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => http_build_query($params),
CURLOPT_HTTPHEADER => [
'Content-Type: application/x-www-form-urlencoded',
'Cookie: '.file_get_contents($this->cookieFile),
'User-Agent: '.$this->userAgent
],
CURLOPT_RETURNTRANSFER => true
]);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result, true);
}
}
// 使用示例
$publisher = new BaijiahaoPublisher('BAIDUID=XXX; BDUSS=XXX;...');
if($publisher->checkLogin()) {
// 上传封面
$cover = $publisher->uploadCover('/path/to/cover.jpg');
if(isset($cover['data']['url'])) {
// 发布带封面的文章
$result = $publisher->publishArticle(
'带封面的测试文章',
'<p>本文包含自定义封面图片</p>',
$cover['data']['url'],
['tag' => '技术']
);
print_r($result);
}
} else {
echo "Cookie已失效";
}
?>
Title:
- Donec sodales sagittis
- Sed consequat leo eget
- Aliquam lorem ante
- Aenean ligula eget
- Cum sociis natoque
Comments :
Delores Williams 1 hr Ago
If several languages coalesce, the grammar of the resulting language is more simple and regular than that of the individual

Leave a Reply:
Search
Categories
Upcoming Post
Popular Post
Tag Clouds
Instagram Post





