【PHP】ライブドアブログに「AtomPub API 」使って記事投稿・画像投稿!
「AtomPub API 」というのを使ってリモートのサーバーから投稿します!
投稿処理には「HTTP_Request2」が必要なので、あらかじめサーバーにインストールしておいてください!
↓
HTTP_Request2のインストールの仕方:
PEARをインストールしないでHTTP_Request2を使う方法 - やったこと
①画像投稿の仕方
画像投稿のコードは以下のとおりです、ライブドアのブログに「hogege.jpg」という画像をアップロードしています!$userや$rootEndPointや$passには、自分のブログに設定されている情報をセットしてね!
require_once(dirname(__FILE__)."/lib/HTTP/Request2.php"); $user = "hogegege"; $rootEndPoint = "https://livedoor.blogcms.jp/atompub/hogegege"; $pass = "ABCDEFG"; $time = gmdate("Y-m-d\TH:i:s\Z"); $nonce = pack('H*', sha1(md5(time()))); $digest = base64_encode(pack('H*', sha1($nonce.$time.$pass))); $img_dir = dirname(__FILE__)."/"; $imgfile = $img_dir.'hogege.jpg'; $imgdata = file_get_contents('./hogege.jpg', true); $content_type = image_type_to_mime_type(exif_imagetype($imgfile)); $url = 'http://livedoor.blogcms.jp/atom/blog/'.$user.'/image'; $wsse = 'UsernameToken Username="'.$user.'", '. 'PasswordDigest="'.$digest.'", '. 'Nonce="'.base64_encode($nonce).'", '. 'Created="'.$time.'"'; $headers = array( 'X-WSSE: ' . $wsse, 'Content-Type: ' . $content_type, 'Expect:' ); try{ $req = new HTTP_Request2(); $req->setUrl($url); $req->setMethod(HTTP_Request2::METHOD_POST); $req->setHeader($headers); $req->setBody($imgdata); $response = $req->send(); $xml = simplexml_load_string($response->getBody()); $src = $xml->content['src']; $thumbnail = $xml->content['thumbnail']; } catch (HTTP_Request2_Exception $e) { die($e->getMessage()); } catch (Exception $e) { die($e->getMessage()); }
②記事投稿の仕方
お次は記事の投稿です!上記とだいたい同じ感じで投稿できます!
require_once(dirname(__FILE__)."/lib/HTTP/Request2.php"); $user = 'hogege'; $rootEndPoint = 'https://livedoor.blogcms.jp/atompub/hogege'; $pass = 'ABCEDFG'; $time = gmdate("Y-m-d\TH:i:s\Z"); $nonce = pack('H*', sha1(md5(time()))); $digest = base64_encode(pack('H*', sha1($nonce.$time.$pass))); $url = 'http://livedoor.blogcms.jp/atom/blog/'.$user.'/article'; $title = 'ここにタイトル'; $description = 'ここに本文'; $description2 = 'ここに追記'; $category = 'ここにカテゴリー'; $created = date('Y-m-d\TH:i:s\Z'); $wsse = 'UsernameToken Username="'.$user.'", '. 'PasswordDigest="'.$digest.'", '. 'Nonce="'.$nonce.'", '. 'Created="'.$time.'"'; $rawdata = '<?xml version="1.0" encoding="utf-8"?>'. '<entry xmlns="http://www.w3.org/2005/Atom" xmlns:app="http://www.w3.org/2007/app" xmlns:blogcms="http://blogcms.jp/-/spec/atompub/1.0/">'. '<title>'.$title.'</title>'. '<category term="'.$category.'" />'. '<blogcms:source>'. '<blogcms:body><![CDATA['.$description.']]></blogcms:body>'. '<blogcms:more><![CDATA['.$description2.']]></blogcms:more>'. '</blogcms:source>'. '</entry>'; $headers = array('X-WSSE: '.$wsse, 'Expect:'); try{ $req = new HTTP_Request2(); $req->setUrl($url); $req->setMethod(HTTP_Request2::METHOD_POST); $req->setHeader($headers); $req->setBody($rawdata); $response = $req->send(); } catch (HTTP_Request2_Exception $e) { die($e->getMessage()); } catch (Exception $e) { die($e->getMessage()); }
コードは下記のブログを参考にさせていただきました!ありがと~!
参考ブログ:
http://pixieslalala.blog.jp/archives/1038269324.html
http://webpaprika.com/498.html