워드프레스에서 JSON 파일을 blogger로 임포트하려면 blogger에 맞는 XML 파일을 작성해야 합니다. 그러나 JSON을 blogger로 변환하는 플러그인이 있지만 사용자에 맞게 작동하지 않는다는 데 있습니다.

가장 쉬운 방법은 JSON 파일을 RSS 피드로 작성하여 워드프레스에 “가져오기”한 다음 이걸 다시 “Export to Blogger“ 플러그인으로 ”내보내기“해서 blogger 에 임포트하는 것입니다.

온라인에 json을 xml로 변경하는 도구가 많이 있습니다. 하지만 실제로 해보면 잘 작동하지 않는다는 걸 알 수 있습니다. 여기서는 JSON을 RSS 로 변환시킬 수 있는 PHP를 만들어 보겠습니다.

<?php

// 작성할 파일
$putfile = './putfile.txt';
// 가져올 파일
$getfile = './getfile.txt';

/* 
  getfile.txt 내용
  [
    {
      "title":"Funny Food",
      "datetime":"Thu, 01 Jan 1970 00:00:00 +0000",
      "tag":"action,animals",
      "description":"contents"
    }
    ...
  ]
*/
$json = json_decode(
  file_get_contents($getfile), true
);
print_r($json);


$str = null;
$str = '<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
';

foreach($json as $v)
{
  $str .= '
    <item>';
  $str .= '<title>' . htmlspecialchars($v['title']) . "</title>";
  $str .= '<pubDate>' . $v['datetime'] . "</pubDate>";
  $arr .= '<description>' . htmlspecialchars($v['description']) . '</description>';
  $cat = explode(',', $v['tag']);
  if(!empty(trim($v['tag']))) {
    foreach($cat as $q) {
      $str .= '<category>'.$q.'</category>';
    }
  }
  $str .= '</item>';
}
$str .= "</channel>
</rss>";


file_put_contents($putfile, $str);
?>

0 댓글