{"id":771,"date":"2017-12-26T01:35:56","date_gmt":"2017-12-25T16:35:56","guid":{"rendered":"https:\/\/hi3103.net\/notes\/?p=771"},"modified":"2022-07-09T23:33:31","modified_gmt":"2022-07-09T14:33:31","slug":"xml%e3%82%92%e3%83%91%e3%83%bc%e3%82%b9%e3%81%99%e3%82%8b","status":"publish","type":"post","link":"https:\/\/hi3103.net\/notes\/web\/771","title":{"rendered":"\u306f\u3066\u306a\u30c0\u30a4\u30a2\u30ea\u30fc\u306eXML\u3092\u30d1\u30fc\u30b9\u3057\u3066CSV\u5f62\u5f0f\u3067\u51fa\u529b\u3057WordPress\u306b\u30a4\u30f3\u30dd\u30fc\u30c8\u3059\u308b"},"content":{"rendered":"<h3>\u76ee\u7684<\/h3>\n<p>\u306f\u3066\u306a\u30c0\u30a4\u30a2\u30ea\u30fc\u306b\u66f8\u3044\u305f\u8a18\u4e8b\u3092\u3001\u306f\u3066\u306a\u8a18\u6cd5\u306e\u307e\u307eWordPress\u306b\u30a4\u30f3\u30dd\u30fc\u30c8\u3059\u308b<\/p>\n<h3>\u3084\u308a\u304b\u305f<\/h3>\n<ol>\n<li>\u30c0\u30a4\u30a2\u30ea\u30fc\u306e\u7ba1\u7406\u753b\u9762\u304b\u3089\u300c\u306f\u3066\u306a\u30c0\u30a4\u30a2\u30ea\u30fc\u5f62\u5f0f\u300d\u306exml\u3092\u30a8\u30af\u30b9\u30dd\u30fc\u30c8<\/li>\n<li>xml\u3092\u30d1\u30fc\u30b9\u3057\u3066CSV\u306b\u5909\u63db<\/li>\n<li>WordPress\u30d7\u30e9\u30b0\u30a4\u30f3\u300cReally Simple CSV Importer\u300d\u3092\u4f7f\u3063\u3066\u30a4\u30f3\u30dd\u30fc\u30c8<\/li>\n<\/ol>\n<p><!--more--><\/p>\n<h3>XML\u3092\u30d1\u30fc\u30b9\u3057\u3066CSV\u306b\u51fa\u529b\u3059\u308bPHP\u30d7\u30ed\u30b0\u30e9\u30e0<\/h3>\n<h4>\u6750\u6599<\/h4>\n<ul>\n<li>index.php<\/li>\n<li>download.php<\/li>\n<li>hoge.xml\n<ul>\n<li>\u306f\u3066\u306a\u30c0\u30a4\u30a2\u30ea\u30fc\u306e\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u30c7\u30fc\u30bf<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4>\u30bd\u30fc\u30b9<\/h4>\n<h5>index.php<\/h5>\n<pre><code class=\"language-php\">&lt;!DOCTYPE html&gt;\n&lt;html lang=\"ja\"&gt;\n&lt;head&gt;\n    &lt;meta charset=\"utf-8\"&gt;\n    &lt;title&gt;\u306f\u3066\u306a\u30c0\u30a4\u30a2\u30ea\u30fc\u306eXML\u3092\u30d1\u30fc\u30b9\u3057\u3066CSV\u3067\u51fa\u529b\u3059\u308b&lt;\/title&gt;\n&lt;\/head&gt;\n&lt;body&gt;\n    &lt;?php\n    \/* \u914d\u5217\u306e\u898b\u51fa\u3057\u5217\u3092\u4f5c\u6210 *\/\n    $entries[] = array(\n        \"post_id\",\n        \"post_name\",\n        \"post_author\",\n        \"post_date\",\n        \"post_type\",\n        \"post_status\",\n        \"post_title\",\n        \"post_content\",\n        \"post_category\",\n        \"post_tags\",\n        );\n\n    \/* \u306f\u3066\u306a\u30c0\u30a4\u30a2\u30ea\u30fc\u306eXML\u3092\u30d1\u30fc\u30b9 *\/\n    $xml = simplexml_load_file('hi3103.xml');\n    $json = json_encode($xml);\n    $array = json_decode($json,TRUE);\n    $days = $array[\"day\"];\n    foreach($days as $day){\n        \/\/day\u30bf\u30b0\u306edate\u5c5e\u6027\u306e\u5185\u5bb9\u3092\u53d6\u5f97\n        $attr = $day[\"@attributes\"];\n        $date = $attr[\"date\"]; \/\/yyy-mm-dd\n        $date = date('Y\/n\/j G:H',strtotime($date)); \/\/ yyyy\/m\/d 0:00 \u5f62\u5f0f\u306b\u3059\u308b\n\n        \/\/body\u30bf\u30b0\u306e\u5185\u5bb9\u3092\u53d6\u5f97\n        $body = $day[\"body\"];\n\n        \/\/body\u30bf\u30b0\u306e\u5185\u5bb9\u3092\u898b\u51fa\u3057\u3054\u3068\u306b\u5206\u5272\u3057\u3066\u914d\u5217\u306b\u683c\u7d0d\n        $body = preg_split('\/\\*([0-9]{10})\\*(.*?)\\n\/', $body, null, PREG_SPLIT_DELIM_CAPTURE);\n\n        \/\/body\u304b\u3089\u8a18\u4e8bID\u3092\u53d6\u5f97\n        $id = $body[1];\n\n        \/\/body\u304b\u3089\u30bf\u30a4\u30c8\u30eb\u884c\u3092\u53d6\u5f97\n        $meta = $body[2];\n        \/\/\u30bf\u30a4\u30c8\u30eb\u3068\u30bf\u30b0\u3092\u5206\u5272\u3057\u3066\u914d\u5217\u306b\u683c\u7d0d\n        $meta = preg_split('\/\\[|\\]\/', $meta, null, PREG_SPLIT_DELIM_CAPTURE);\n        \/\/\u914d\u5217\u306e\u4e2d\u306e\u7a7a\u8981\u7d20\u3092\u524a\u9664\u3059\u308b\n        $meta = array_filter($meta, \"strlen\");\n        \/\/\u6dfb\u5b57\u3092\u632f\u308a\u76f4\u3059\n        $meta = array_values($meta);\n        \/\/\u914d\u5217\u304b\u3089\u6700\u5f8c\u306e\u8981\u7d20\u3092\u524a\u9664\uff06\u524a\u9664\u3057\u305f\u8981\u7d20\u3092title\u306b\u683c\u7d0d\n        $title = array_pop($meta);\n        \/\/\u6b8b\u3063\u305f\u914d\u5217\u3092\u30ab\u30f3\u30de\u533a\u5207\u308a\u306e\u6587\u5b57\u5217\u3068\u3057\u3066tag\u306b\u683c\u7d0d\n        $tag = implode(',', $meta);\n\n        \/\/body\u304b\u3089\u672c\u6587\u3092\u53d6\u5f97\n        $content = $body[3];\n\n        \/\/\u914d\u5217\u306b\u683c\u7d0d\n        $entries[] = array(\n            \/\/ \"post_id\"        =&gt; \n            null,\n            \/\/ \"post_name\"      =&gt; \n            null,\n            \/\/ \"post_author\"    =&gt; \n            'hi3103',\n            \/\/ \"post_date\"      =&gt; \n            $date,\n            \/\/ \"post_type\"      =&gt; \n            'post',\n            \/\/ \"post_status\"    =&gt; \n            'draft',\n            \/\/ \"post_title\" =&gt; \n            $title,\n            \/\/ \"post_content\"   =&gt; \n            $content,\n            \/\/ \"post_category\"  =&gt; \n            'hatena_diary',\n            \/\/ \"post_tags\"      =&gt; \n            $tag,\n            );\n    }\n\n    \/* CSV\u30d5\u30a1\u30a4\u30eb\u3092\u51fa\u529b*\/\n\n    \/\/ \u30d5\u30a1\u30a4\u30eb\u540d\u3092\u65e5\u4ed8\u304b\u3089\u751f\u6210\n    date_default_timezone_set('Asia\/Tokyo');\n    $filename = \"hatena-diary-\".date(\"YmdHis\").\".csv\";\n\n    \/\/ \u30d5\u30a1\u30a4\u30eb\u30dd\u30a4\u30f3\u30bf\u3092\u30aa\u30fc\u30d7\u30f3\n    $fp = fopen($filename, 'w');\n\n    \/\/\u914d\u5217\u304b\u3089csv\u30d5\u30a1\u30a4\u30eb\u3078\u306e\u66f8\u304d\u8fbc\u307f\n    foreach ($entries as $fields) {\n        fputcsv($fp, $fields);\n    }\n\n    \/\/ \u30d5\u30a1\u30a4\u30eb\u30dd\u30a4\u30f3\u30bf\u3092\u30af\u30ed\u30fc\u30ba\n    fclose($fp);\n    ?&gt;\n\n    &lt;p&gt;&lt;?php echo $filename; ?&gt;&lt;\/p&gt;\n\n    &lt;form method=\"post\" action=\"download.php\"&gt;\n        &lt;input type=\"hidden\" name=\"filename\" value=\"&lt;?php echo $filename; ?&gt;\"&gt;\n        &lt;input type=\"submit\" value=\"\u30d5\u30a1\u30a4\u30eb\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\"&gt;\n    &lt;\/form&gt;\n\n&lt;\/body&gt;\n&lt;\/html&gt;<\/code><\/pre>\n<ul>\n<li>\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u30dc\u30bf\u30f3\u3092\u30af\u30ea\u30c3\u30af\u3059\u308b\u3068download.php\u304c\u8d70\u308b<\/li>\n<\/ul>\n<h5>download.php<\/h5>\n<pre><code class=\"language-php\">&lt;?php\nif($_SERVER['HTTPS']=='on'){\n    $filename = $_POST['filename'];\n\n    \/\/ HTTP\u30d8\u30c3\u30c0\u3092\u8a2d\u5b9a\n    header('Content-Type: application\/octet-stream');\n    header('Content-Length: '.filesize($filename));\n    header('Content-Disposition: attachment; filename='.$filename);\n\n    \/\/ \u30d5\u30a1\u30a4\u30eb\u51fa\u529b\n    readfile($filename);\n\n    \/\/ \u30d5\u30a1\u30a4\u30eb\u524a\u9664\n    unlink($filename);\n}\n?&gt;<\/code><\/pre>\n<ul>\n<li>\u30d5\u30a1\u30a4\u30eb\u306e\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3068\u524a\u9664\u3092\u884c\u3046<\/li>\n<\/ul>\n<h3>\u53c2\u8003URL<\/h3>\n<h4>simpleXML\u306e\u6271\u3044<\/h4>\n<ul>\n<li>\n<p><a href=\"https:\/\/qiita.com\/fantm21\/items\/81d2c56f426da8f7daf4\">PHP\u3067XML\u3092\u30d1\u30fc\u30b9 &#8211; Qiita<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/liginc.co.jp\/programmer\/archives\/5317\">\u4e00\u624b\u9593\u5fc5\u8981\u3002PHP\u3067\u306eSimpleXML\u95a2\u6570\u306e\u30d1\u30fc\u30b9\u51e6\u7406 | \u682a\u5f0f\u4f1a\u793eLIG<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"https:\/\/qiita.com\/ka_to\/items\/54fe4d5bb655841f85d8\">simplexml_load_file(simplexml_load_string) \u3092\u914d\u5217\u306b\u5909\u63db\u3059\u308b &#8211; Qiita<\/a><\/p>\n<\/li>\n<li>\n<p>\u30ea\u30d5\u30a1\u30ec\u30f3\u30b9<\/p>\n<ul>\n<li><a href=\"http:\/\/php.net\/manual\/ja\/simplexml.examples-basic.php\">PHP: \u57fa\u672c\u7684\u306a SimpleXML \u306e\u4f7f\u7528\u6cd5 &#8211; Manual<\/a><\/li>\n<li><a href=\"http:\/\/php.net\/manual\/ja\/function.simplexml-load-file.php\">PHP: simplexml_load_file &#8211; Manual<\/a><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h4>\u914d\u5217\u306a\u3069\u306e\u6271\u3044<\/h4>\n<ul>\n<li><a href=\"https:\/\/qiita.com\/kazu56\/items\/6947a0e4830eb556d575\">\u3010PHP\u3011\u9023\u60f3\u914d\u5217\u3001\u914d\u5217\u3078\u306e\u8ffd\u52a0 &#8211; Qiita<\/a><\/li>\n<li>\u6b63\u898f\u8868\u73fe\u3067\u6587\u5b57\u5217\u3092\u5206\u5272\u3057\u3066\u914d\u5217\u306b\u5165\u308c\u308b\n<ul>\n<li><a href=\"http:\/\/php.net\/manual\/ja\/function.preg-split.php\">PHP: preg_split &#8211; Manual<\/a><\/li>\n<\/ul>\n<\/li>\n<li>\u914d\u5217\u3092n\u7b49\u5206\u3059\u308b\n<ul>\n<li><a href=\"http:\/\/php.net\/manual\/ja\/function.array-chunk.php\">PHP: array_chunk &#8211; Manual<\/a><\/li>\n<\/ul>\n<\/li>\n<li>\u914d\u5217\u304b\u3089\u7a7a\u8981\u7d20\u3092\u524a\u9664\u3059\u308b\n<ul>\n<li><a href=\"http:\/\/www.hachi-log.com\/php-arrayfilter-arrayvalue\/\">PHP array_filter()\u3068array_values()\u3092\u4f7f\u3063\u3066\u914d\u5217\u304b\u3089\u7a7a\u8981\u7d20\u3092\u524a\u9664\u3057\u3066\u6dfb\u5b57\u3092\u3064\u3081\u308b\u65b9\u6cd5 | \u30cf\u30c1\u30ed\u30b0<\/a><\/li>\n<\/ul>\n<\/li>\n<li>\u914d\u5217\u304b\u3089\u6700\u5f8c\u306e\u8981\u7d20\u3092\u53d6\u308a\u51fa\u3059\n<ul>\n<li><a href=\"http:\/\/php.net\/manual\/ja\/function.array-pop.php\">PHP: array_pop &#8211; Manual<\/a><\/li>\n<\/ul>\n<\/li>\n<li>\u914d\u5217\u3092\u30ab\u30f3\u30de\u533a\u5207\u308a\u306e\u6587\u5b57\u5217\u306b\u76f4\u3059\n<ul>\n<li><a href=\"https:\/\/php.programmer-reference.com\/php-explode-implode\/\">PHP \u30ab\u30f3\u30de\u533a\u5207\u308a\u6587\u5b57\u5217\u21d4\u914d\u5217 \u3092\u5909\u63db\u3059\u308b \u2013 PHP\u3061\u3087\u3053\u3063\u3068\u30ea\u30d5\u30a1\u30ec\u30f3\u30b9<\/a><\/li>\n<\/ul>\n<\/li>\n<li>\u591a\u6b21\u5143\u9023\u60f3\u914d\u5217\u306b\u683c\u7d0d\u3059\u308b\n<ul>\n<li><a href=\"https:\/\/qiita.com\/shuntaro_tamura\/items\/97d5652626b3eedc1085\">PHP \u591a\u6b21\u5143\u9023\u60f3\u914d\u5217\u306e\u6271\u3044 &#8211; Qiita<\/a><\/li>\n<\/ul>\n<\/li>\n<li>\u65e5\u4ed8\u306e\u30d5\u30a9\u30fc\u30de\u30c3\u30c8\n<ul>\n<li><a href=\"http:\/\/raining.bear-life.com\/php\/php%E3%81%AEdate%E9%96%A2%E6%95%B0%E3%81%A7%E6%97%A5%E4%BB%98%E3%81%AE%E3%83%95%E3%82%A9%E3%83%BC%E3%83%9E%E3%83%83%E3%83%88%E5%A4%89%E6%9B%B4\">php\uff1aphp\u306edate\u95a2\u6570\u3067\u65e5\u4ed8\u306e\u30d5\u30a9\u30fc\u30de\u30c3\u30c8\u5909\u66f4 | raining<\/a><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u76ee\u7684 \u306f\u3066\u306a\u30c0\u30a4\u30a2\u30ea\u30fc\u306b\u66f8\u3044\u305f\u8a18\u4e8b\u3092\u3001\u306f\u3066\u306a\u8a18\u6cd5\u306e\u307e\u307eWordPress\u306b\u30a4\u30f3\u30dd\u30fc\u30c8\u3059\u308b \u3084\u308a\u304b\u305f \u30c0\u30a4\u30a2\u30ea\u30fc\u306e\u7ba1\u7406\u753b\u9762\u304b\u3089\u300c\u306f\u3066\u306a\u30c0\u30a4\u30a2\u30ea\u30fc\u5f62\u5f0f\u300d\u306exml\u3092\u30a8\u30af\u30b9\u30dd\u30fc\u30c8 xml\u3092\u30d1\u30fc\u30b9\u3057\u3066CSV\u306b\u5909\u63db WordPres [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[53],"tags":[21,24,23],"class_list":["post-771","post","type-post","status-publish","format-standard","hentry","category-web","tag-php","tag-wordpress","tag-xml"],"_links":{"self":[{"href":"https:\/\/hi3103.net\/notes\/wp-json\/wp\/v2\/posts\/771","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hi3103.net\/notes\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hi3103.net\/notes\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hi3103.net\/notes\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/hi3103.net\/notes\/wp-json\/wp\/v2\/comments?post=771"}],"version-history":[{"count":0,"href":"https:\/\/hi3103.net\/notes\/wp-json\/wp\/v2\/posts\/771\/revisions"}],"wp:attachment":[{"href":"https:\/\/hi3103.net\/notes\/wp-json\/wp\/v2\/media?parent=771"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hi3103.net\/notes\/wp-json\/wp\/v2\/categories?post=771"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hi3103.net\/notes\/wp-json\/wp\/v2\/tags?post=771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}