將 FB page 內容匯入 Wordpress

edited 三月 2014 in 內容管理系統
因為想要將某個 FB page 的內容全部塞進新的 Wordpress 網站,先是有朋友將資料以 facebook graph api 將每篇文章轉成這樣的格式
Array
(
[id] => ''
[from] => Array
(
[category] => ''
[name] => ''
[id] => ''
)

[message] => ''
[picture] => ''
[link] => ''
[icon] => ''
[privacy] => Array
(
[value] =>
)

[type] => photo
[status_type] => added_photos
[object_id] => ''
[created_time] => 2014-03-27T15:33:32+0000
[updated_time] => 2014-03-27T15:33:32+0000
[likes] => Array
(
[data] => Array
(
[0] => Array
(
[id] => ''
[name] => ''
)


)

[paging] => Array
(
[cursors] => Array
(
[after] => ''
[before] => ''
)

[next] => ''
)

)

)


之後我接手處理,透過下面程式將它轉為 WordPress eXtended RSS 格式,然後透過 http://wordpress.org/plugins/wordpress-importer/ 匯入資料

<?php

class SimpleXMLExtended extends SimpleXMLElement {

public function addCData($cdata_text) {
$node = dom_import_simplexml($this);
$no = $node->ownerDocument;
$node->appendChild($no->createCDATASection($cdata_text));
}

}

$types = array();

$rss = simplexml_load_file('in.xml', 'SimpleXMLExtended');
unset($rss->channel->item);
$count = 0;
foreach (glob('*.txt') AS $file) {
$posts = json_decode(file_get_contents($file), true);
foreach ($posts['data'] AS $post) {
if (!empty($post['from']['id']) && $post['from']['id'] === 'fb_page_id') {
if (empty($post['message'])) {
if (!isset($post['description'])) {
continue;
}
$post['message'] = $post['description'];
if (empty($post['message'])) {
continue;
}
}
$node = $rss->channel->addChild('item');
$postTime = strtotime($post['created_time']);
$node->pubDate = date('D, j M Y H:i:s O', $postTime); //Thu, 27 Mar 2014 18:54:30 +0000
$node->title = mb_substr($post['message'], 0, 20, 'utf-8') . '...';
$node->title = str_replace(\"\n\", ' ', $node->title);
$ce = $node->addChild('encoded');
$message = preg_replace('/[\x00-\x09\x0B\x0C\x0E-\x1F\x7F]/', '', $post['message']);
$postLink = 'https://www.facebook.com/NtuNewsEForum/posts/' . str_replace('fb_page_id_', '', $post['id']);
$message .= \"\n\n<a href=\\"{$postLink}\\" target=\\"_blank\\">原始文章</a>\";
if(!empty($post['link'])) {
$message .= \"\n<a href=\\"{$post['link']}\\" target=\\"_blank\\">相關連結</a>\";
}
$ce->addCData($message);
$dc = $node->addChild('creator');
$dc->addCData('pu');


$node->addChild('post_date', date('Y-m-d H:i:s', $postTime));
$node->addChild('status', 'publish');
$node->addChild('post_type', 'post');
}
}
}
$content = $rss->asXML();
$content = str_replace(array(
'creator>',
'encoded>',
'status>',
'post_type>',
'post_date>',
), array(
'dc:creator>',
'content:encoded>',
'wp:status>',
'wp:post_type>',
'wp:post_date>',
), $content);
file_put_contents('out.xml', $content);


其中 in.xml 是先透過 wordpress 匯出的資料格式,以它為基礎去生一個 out.xml 給匯入程式用

這裡只處理文字內容,因為時間不多,所以就沒有再深入圖片等資料格式的處理,留給有興趣的朋友研究
Sign In or Register to comment.