关键是不好调试:
代码:
<!--?php <br ?--> /* Plugin Name: B站自媒体同步工具 Plugin URI: http://example.com Description: A brief description of what the plugin does. Version: 1.0 Author: 华哥 Author URI: http://example.com License: GPL2 */ class BilibiliUploader { public function __construct() { add_action('save_post', [$this, 'upload_images_to_bilibili']); } public function upload_images_to_bilibili($post_id) { if (wp_is_post_revision($post_id)) { return; } $post_content = get_post_field('post_content', $post_id); preg_match_all('/<img[^>]+src="([^">]+)"/i', $post_content, $matches); if (!empty($matches[1])) { foreach ($matches[1] as $image_url) { $this->process_image($image_url); } } } private function process_image($image_url) { $temp_file = download_url($image_url); if (is_wp_error($temp_file)) { error_log('Failed to download image: ' . $image_url); return; } $this->uploadFile($temp_file); unlink($temp_file); } public function uploadFile($imagePath) { if (!file_exists($imagePath)) { error_log("文件不存在: $imagePath"); return; } error_log("开始上传图片到Bilibili: $imagePath"); $response = $this->makeRequest($imagePath); if ($response) { $this->handleResponse($response); } else { error_log("请求失败"); } } private function makeRequest($imagePath) { $url = "https://api.bilibili.com/x/dynamic/feed/draw/upload_bfs"; $boundary = "---------------------------" . uniqid(); // 使用动态生成的boundary $headers = [ "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:130.0) Gecko/20100101 Firefox/130.0", "Accept: */*", "Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2", "Accept-Encoding: gzip, deflate", // 修改为与原始代码一致 "Referer: https://t.bilibili.com/", "Origin: https://t.bilibili.com", "Connection: keep-alive", "Cookie: 4534234Cl4=A1F0A409-3626-8333-8964-2B9F32D92EC840243-023101913-MnLxL6Vqo8JzkbhjsXOcgQ%3D%3D; buvid_fp=5e9a8088e2093db52ac4432fb2bb81c2; enable_web_push=DISABLE; header_theme_version=CLOSE; home_feed_column=5; browser_resolution=1536-703; fingerprint=5e9a8088e2093db52ac4432fb2bb81c2; buvid_fp_plain=undefined; CURRENT_FNVAL=4048; rpdid=|(J~J|Rm|RuJ0J'uYm~~)kmY|; b_lsid=C342BDD2_192541C1C36; bsource=search_baidu; SESSDATA=2c853358%2C1743540007%2C1c395%2Aa1CjBoQPqw1p54Ut1L3sUZ-iKwDSkFrIWeDJfsmBhzhgNoqGgTCiG-SoohDFKKH0WS5PUSVjRZY0FnQWREVTZQbDFGS0J5d2FiTU9XeVNFU0huOEZES3B1c19UaEFiT1NwYTdBSlhTZWJmczQ5bFJNaWx4YTVkcEljdS1FaVhoVnY5Ql9JNTFJbkl3IIEC; bili_jct=f4a19205ce0e5fdfd26441438b9fd811; DedeUserID=15621741; DedeUserID__ckMd5=a7b49f848348c797; sid=h6yrhg31; hit-dyn-v2=1; bili_ticket=eyJhbGciOiJIUzI1NiIsImtpZCI6InMwMyIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MjgyNDcyMTQsImlhdCI6MTcyNzk4Nzk1NCwicGx0IjotMX0._g00SN27Nu3SJHAshhrK1WSCVryiM5PsYy2j1VNhSsQ; bili_ticket_expires=1728247154", "Sec-Fetch-Dest: empty", "Sec-Fetch-Mode: cors", "Sec-Fetch-Site: same-site", "Content-Type: multipart/form-data; boundary=$boundary" ]; $body = "--$boundaryrn" . "Content-Disposition: form-data; name="file_up"; filename="" . basename($imagePath) . ""rn" . "Content-Type: image/jpegrnrn" . file_get_contents($imagePath) . "rn" . "--$boundaryrn" . "Content-Disposition: form-data; name="biz"rnrn" . "new_dynrn" . "--$boundaryrn" . "Content-Disposition: form-data; name="category"rnrn" . "dailyrn" . "--$boundaryrn" . "Content-Disposition: form-data; name="csrf"rnrn" . "f4a19205ce0e5fdfd26441438b9fd811rn" . "--$boundary--rn"; $options = [ 'http' => [ 'method' => 'POST', 'header' => implode("rn", $headers), 'content' => $body ] ]; $context = stream_context_create($options); return file_get_contents($url, false, $context); } private function handleResponse($response) { $jsonResponse = json_decode($response, true); if ($jsonResponse && $jsonResponse['code'] === 0) { error_log("上传成功!"); $imageUrl = $jsonResponse['data']['image_url'] ?? ''; if ($imageUrl) { error_log("图片URL: $imageUrl"); } } else { error_log("上传失败。错误信息: " . ($jsonResponse['message'] ?? '未知错误')); } } } // 初始化插件 new BilibiliUploader(); ?>