SHOJI's Code
 仕事や趣味で書いた各種言語のプログラミングコード(エクセルVBA,PHP,C/C++/C#,JavaScript等)、その他雑記。
2017.10<<123456789101112131415161718192021222324252627282930>>2017.12
上記の広告は1ヶ月以上更新のないブログに表示されています。
新しい記事を書く事で広告が消せます。

ブログへの自動投稿を PHPでしたいと思い、いろいろと調べていた。
使用するブログはXML-RPCに対応していたので、XML-RPCで行うことにしたのだが、さらにXML-RPCについていろいろと調べたり、実験したりするうちになんか色々と面倒だったので(new XML_RPC_Valueだらけになる)、ブログへのアクセス用のライブラリを作ることにした。

ソースは追記に載せるが、使い方はこんな感じ
<?php
require_once 'blog.php';

$blog = new BlogAccess( 'XML-RPCエンドポイントのURL' );

$uid = 'ユーザーID';
$pwd = 'パスワード';

$myblogs = $blog->blogger->getUsersBlogs('', $uid, $pwd);

if ($myblogs===false) {
exit($blog->lastError());
}

for ($i=0;$i<count($myblogs);$i++) {
echo "-- [".$myblogs[$i]['blogid']."] '".$myblogs[$i]['blogName']."' ".$myblogs[$i]['url']."\r\n";
}
?>


上記のような感じで (オブジェクト) -> (API名) -> (メソッド名) (引数...) という感じでコールすると、データが送信される。
メソッドやAPIが未定義でもできるだけ処理されるようにしてある。
PEAR の XML_RPC が必要。
ま、毎度のことなのだが、適当に作っていてテストも適当なので、使用される場合は自己責任にて・・・。
<?php
require_once 'XML/RPC.php';

class BlogAccess {
private $__client = null;
private $__api = array();
private $__err = array(
'code'=>0,
'description'=>''
);

function __construct($url, $proxy='', $proxy_username='', $proxy_password='') {
$re = '!^([^:]+://)*([^/]+)(:[0-9]+)*(/.+)*$!';
$rp = array();
if (!preg_match($re, $url, $r)) throw new Exception("failed to parse url '$url'.");
if ($proxy!='' && !preg_match($re, $proxy, $rp)) throw new Exception("failed to parse url '$proxy'.");

$this->__client = new XML_RPC_Client($r[4], $r[1].$r[2], intval($r[3]), $proxy, intval($rp[3]), $proxy_username, $proxy_password);
}

function __get($name) {
if (method_exists($this, "get_$name")) return call_user_func(array($this, "get_$name"));

$api = $this->getAPI($name);
if ($api!==false) return $api;

throw new Exception("Blogging API '$name' not found.");
}
function __set($name, $value) {
if (method_exists($this, "get_$name")) return call_user_func(array($this, "set_$name"), $value);

throw new Exception("write access to property '$name' denied.");
}

function __call($name, $args) {
if (method_exists($this->__client, $name)) return call_user_func_array(array($this->__client, $name), $args);

throw new Exception("method '$name' not found.");
}

public function get_client() {
return $this->__client;
}

public function getAPI($name) {
if (isset($this->__api[$name])) return $this->__api[$name];

try {
$classname = "BlogAPI_$name";
$api = class_exists($classname, false) ? new $classname($this) : new BlogAPI($this, $name);
return ($this->__api[$name] = $api);
}
catch(Exception $e) {
return false;
}
}

public function send($method) {
$msg = new XML_RPC_Message($method);

for ($i=1;$i<func_num_args();$i++) {
$msg->addParam(BlogAPI::PARAM(func_get_arg($i)));
}

$this->clearError();
$res = $this->__client->send($msg);
if (!$res) {
$this->setError(-1, 'Could not connect to server.');
return false;
}
else if ($res->faultCode()!==0) {
$this->setError($res->faultCode(), $res->faultString());
return false;
}

$data = XML_RPC_decode($res->value());
if (is_array($data) && count($data)==2 && isset($data['faultCode']) && isset($data['faultString'])) {
$this->setError($data['faultCode'], $data['faultString']);
return false;
}
return $data;
}

public function get_error() {
return $this->__err;
}

public function lastError() {
return $this->__err['description'];
}

public function clearError() {
$this->setError(0, '');
}


private function setError($code, $desc) {
$this->__err['code'] = $code;
$this->__err['description'] = $desc;
}
}

class BlogAPI {
protected $blog = null;
protected $prefix = '';

function __construct($blog, $prefix) {
$this->blog = $blog;
$this->prefix = $prefix . ($prefix!='' ? '.' : '');
}

function __call($name, $args) {
array_unshift($args, $name);
call_user_func_array(array($this, "send"), $args);
}

function send($name) {
$args = array($this->prefix.$name);
for ($i=1;$i<func_num_args();$i++) array_push($args, func_get_arg($i));
return call_user_func_array(array($this->blog, "send"), $args);
}

static function PARAM($data) {
if ($data instanceof XML_RPC_Value) return $data;

if (is_array($data)) {
$array = array();
$is_array = true;
$n = 0;
foreach ($data as $key=>$value) {
if ($key!==$n++) $is_array = false;
$array[$key] = self::param($data[$key]);
}
return new XML_RPC_Value($array, $is_array ? 'array' : 'struct');
}

return XML_RPC_encode($data);
}

protected static function BOOL($bool) {
return new XML_RPC_Value($bool, 'boolean');
}
protected static function INT($int) {
return new XML_RPC_Value($int, 'int');
}
protected static function REAL($real) {
return new XML_RPC_Value($real, 'double');
}
protected static function STR($str) {
return new XML_RPC_Value($str, 'string');
}
protected static function TIME($time) {
if (is_int($time)) $time = date("Ymd", $time)."T".date("His", $time);
return new XML_RPC_Value($time, 'dateTime.iso8601');
}
protected static function BIN($bin) {
return new XML_RPC_Value($bin, 'base64');
}
protected static function ARR($array, $type) {
if (is_array($type)) {
if (count($type)==1 && isset($type[0])) {
for ($i=0;$i<count($array);$i++) $array[$i] = self::ARR($array[$i], $type[0]);
}
else {
for ($i=0;$i<count($array);$i++) $array[$i] = self::STRUC($array[$i], $type);
}
}
else {
for ($i=0;$i<count($array);$i++) $array[$i] = new XML_RPC_Value($array[$i], $type);
}
return new XML_RPC_Value($array, 'array');
}
protected static function STRUC($struc, $def) {
foreach ($def as $key=>$type) {
if (isset($struc[$key])) {
if (is_array($type)) {
if (count($type)==1 && isset($type[0])) {
$struc[$key] = self::ARR($struc[$key], $type[0]);
}
else {
$struc[$key] = self::STRUC($struc[$key], $type);
}
}
else if ($type=='dateTime.iso8601') {
$struc[$key] = self::TIME($struc[$key]);
}
else {
$struc[$key] = new XML_RPC_Value($struc[$key], $type);
}
}
}
return self::PARAM($struc);
}
}

class BlogAPI_blogger extends BlogAPI {
function __construct($blog) {
parent::__construct($blog, 'blogger');
}

function newPost($appkey, $blogid, $username, $password, $content, $publish) {
return parent::send(
__FUNCTION__,
parent::STR($appkey),
parent::STR($blogid),
parent::STR($username),
parent::STR($password),
parent::STR($content),
parent::BOOL($publish)
);
}

function editPost($appkey, $blogid, $username, $password, $content, $publish) {
return parent::send(
__FUNCTION__,
parent::STR($appkey),
parent::STR($blogid),
parent::STR($username),
parent::STR($password),
parent::STR($content),
parent::BOOL($publish)
);
}

function deletePost($appkey, $postid, $username, $password, $publish) {
return parent::send(
__FUNCTION__,
parent::STR($appkey),
parent::STR($postid),
parent::STR($username),
parent::STR($password),
parent::BOOL($publish)
);
}

function getRecentPosts($appkey, $blogid, $username, $password, $numberOfPosts) {
return parent::send(
__FUNCTION__,
parent::STR($appkey),
parent::STR($blogid),
parent::STR($username),
parent::STR($password),
parent::INT($numberOfPosts)
);
}

function getUsersBlogs($appkey, $username, $password) {
return parent::send(
__FUNCTION__,
parent::STR($appkey),
parent::STR($username),
parent::STR($password)
);
}

function getUserInfo($appkey, $username, $password) {
return parent::send(
__FUNCTION__,
parent::STR($appkey),
parent::STR($username),
parent::STR($password)
);
}


function getTemplate($appkey, $blogid, $username, $password, $templateType) {
return parent::send(
__FUNCTION__,
parent::STR($appkey),
parent::STR($blogid),
parent::STR($username),
parent::STR($password),
parent::STR($templateType)
);
}

function setTemplate($appkey, $blogid, $username, $password, $template, $templateType) {
return parent::send(
__FUNCTION__,
parent::STR($appkey),
parent::STR($blogid),
parent::STR($username),
parent::STR($password),
parent::STR($template),
parent::STR($templateType)
);
}
}

class BlogAPI_metaWeblog extends BlogAPI {
private static $STRUC_CONTENT = array(
'title'=>'string',
'description'=>'string',
'categories'=>array('string'),
'dateCreated'=>'dateTime.iso8601',

'mt_allow_comments'=>'int',
'mt_allow_pings'=>'int',
'mt_convert_breaks'=>'string',
'mt_text_more'=>'string',
'mt_excerpt'=>'string',
'mt_keywords'=>'string',
'mt_tb_ping_urls'=>array('string')
);
private static $STRUC_MEDIA_OBJECT = array(
'bits'=>'base64',
'name'=>'string'
);

function __construct($blog) {
parent::__construct($blog, 'metaWeblog');
}

function newPost($blogid, $username, $password, $content, $publish) {
return parent::send(
__FUNCTION__,
parent::STR($blogid),
parent::STR($username),
parent::STR($password),
parent::STRUC($content, self::$STRUC_CONTENT),
parent::BOOL($publish)
);
}

function editPost($blogid, $username, $password, $content, $publish) {
return parent::send(
__FUNCTION__,
parent::STR($blogid),
parent::STR($username),
parent::STR($password),
parent::STRUC($content, self::$STRUC_CONTENT),
parent::BOOL($publish)
);
}

function getPost($postid, $username, $password) {
return parent::send(
__FUNCTION__,
parent::STR($postid),
parent::STR($username),
parent::STR($password)
);
}

function getRecentPosts($blogid, $username, $password, $numberOfPosts) {
return parent::send(
__FUNCTION__,
parent::STR($blogid),
parent::STR($username),
parent::STR($password),
parent::INT($numberOfPosts)
);
}

function newMediaObject($blogid, $username, $password, $file) {
return parent::send(
__FUNCTION__,
parent::STR($blogid),
parent::STR($username),
parent::STR($password),
parent::STRUC($file, self::$STRUC_MEDIA_OBJECT)
);
}
}

class BlogAPI_mt extends BlogAPI {
private static $STRUC_CATEGORY = array(
'categoryId'=>'string',
'isPrimary'=>'boolean'
);

function __construct($blog) {
parent::__construct($blog, 'mt');
}

function getRecentPostTitles($blogid, $username, $password, $numberOfPosts) {
return parent::send(
__FUNCTION__,
parent::STR($blogid),
parent::STR($username),
parent::STR($password),
parent::INT($numberOfPosts)
);
}

function getCategoryList($blogid, $username, $password) {
return parent::send(
__FUNCTION__,
parent::STR($blogid),
parent::STR($username),
parent::STR($password)
);
}

function getPostCategories($postid, $username, $password) {
return parent::send(
__FUNCTION__,
parent::STR($postid),
parent::STR($username),
parent::STR($password)
);
}

function setPostCategories($postid, $username, $password, $categories) {
return parent::send(
__FUNCTION__,
parent::STR($postid),
parent::STR($username),
parent::STR($password),
parent::ARR($categories, self::$STRUC_CATEGORY)
);
}

function supportedMethods($blogid=false, $username='', $password='') {
if ($blodid===false) return parent::send(__FUNCTION__);

return parent::send(
__FUNCTION__,
parent::STR($blogid),
parent::STR($username),
parent::STR($password)
);
}

function supportedTextFilters($blogid=false, $username='', $password='') {
if ($blodid===false) return parent::send(__FUNCTION__);

return parent::send(
__FUNCTION__,
parent::STR($blogid),
parent::STR($username),
parent::STR($password)
);
}

function getTrackbackPings($postid, $username=false, $password='') {
if ($username===false) return parent::send(__FUNCTION__, parent::STR($postid));

return parent::send(
__FUNCTION__,
parent::STR($postid),
parent::STR($username),
parent::STR($password)
);
}

function publishPost($postid, $username, $password) {
return parent::send(
__FUNCTION__,
parent::STR($postid),
parent::STR($username),
parent::STR($password)
);
}

function setNextScheduledPost($postid, $dateCreated, $username, $password) {
return parent::send(
__FUNCTION__,
parent::STR($postid),
parent::TIME($dateCreated),
parent::STR($username),
parent::STR($password)
);
}
}

class BlogAPI_wp extends BlogAPI {
private static $STRUC_SET_OPTION = array(
'name'=>'string',
'value'=>'string'
);
private static $STRUC_EDIT_COMMENT = array(
'status'=>'string',
'date_created_gmt'=>'dateTime.iso8601',
'content'=>'string',
'author'=>'string',
'author_url'=>'string',
'author_email'=>'string'
);
private static $STRUC_NEW_COMMENT = array(
'comment_parent'=>'int',
'content'=>'string',
'author'=>'string',
'author_url'=>'string',
'author_email'=>'string'
);
private static $STRUC_NEW_PAGE = array(
'wp_slug'=>'string',
'wp_password'=>'string',
'wp_page_parent_id'=>'int',
'wp_page_order'=>'int',
'wp_author_id'=>'int',
'title','string',
'description'=>'string',
'mt_excerpt'=>'string',
'mt_text_more'=>'string',
'mt_allow_comments'=>'int',
'mt_allow_pings'=>'int',
'dateCreated'=>'dateTime.iso8601',
'custom_fields'=>array(
'dateCreated'=>'dateTime.iso8601',
'userid'=>'int',
'page_id'=>'int',
'page_status'=>'string',
'description'=>'string',
'title'=>'string',
'link'=>'string',
'permaLink'=>'string',
'categories'=>array('string'),
'excerpt'=>'string ',
'text_more'=>'string',
'mt_allow_comments'=>'int',
'mt_allow_pings'=>'int',
'wp_slug'=>'string',
'wp_password'=>'string',
'wp_author'=>'string',
'wp_page_parent_id'=>'int',
'wp_page_parent_title'=>'string',
'wp_page_order'=>'int',
'wp_author_id'=>'int',
'wp_author_display_name'=>'string',
'date_created_gmt'=>'dateTime.iso8601',
'custom_fields'=>array(
'id'=>'string',
'key'=>'string',
'value'=>'string',
),
'wp_page_template'=>'string'
)
);
private static $STRUC_PAGE = array(
'dateCreated'=>'dateTime.iso8601',
'userid'=>'int',
'page_id'=>'int',
'page_status'=>'string',
'description'=>'string',
'title'=>'string',
'link'=>'string',
'permaLink'=>'string',
'categories'=>array('string'),
'excerpt'=>'string ',
'text_more'=>'string',
'mt_allow_comments'=>'int',
'mt_allow_pings'=>'int',
'wp_slug'=>'string',
'wp_password'=>'string',
'wp_author'=>'string',
'wp_page_parent_id'=>'int',
'wp_page_parent_title'=>'string',
'wp_page_order'=>'int',
'wp_author_id'=>'int',
'wp_author_display_name'=>'string',
'date_created_gmt'=>'dateTime.iso8601',
'custom_fields'=>array(
'id'=>'string',
'key'=>'string',
'value'=>'string',
),
'wp_page_template'=>'string'
);
private static $STRUC_CATEGORY = array(
'name'=>'string',
'slug'=>'string',
'parent_id'=>'int',
'description'=>'string'
);
private static $STRUC_FILE = array(
'name'=>'string',
'type'=>'string',
'bits'=>'base64',
'overwrite'=>'boolean'
);
private static $STRUC_COMMENTS = array(
'post_id'=>'int',
'status'=>'string',
'offset'=>'int',
'number'=>'int'
);

function __construct($blog) {
parent::__construct($blog, 'wp');
}

function getUsersBlogs($username, $password) {
return parent::send(
__FUNCTION__,
parent::STR($username),
parent::STR($password)
);
}

function getTags($blogid, $username, $password) {
return parent::send(
__FUNCTION__,
parent::INT($blogid),
parent::STR($username),
parent::STR($password)
);
}

function getCommentCount($blogid, $username, $password, $postid) {
return parent::send(
__FUNCTION__,
parent::INT($blogid),
parent::STR($username),
parent::STR($password),
parent::STR($postid)
);
}

function getPostStatusList($blogid, $username, $password) {
return parent::send(
__FUNCTION__,
parent::INT($blogid),
parent::STR($username),
parent::STR($password)
);
}

function getPageStatusList($blogid, $username, $password) {
return parent::send(
__FUNCTION__,
parent::INT($blogid),
parent::STR($username),
parent::STR($password)
);
}

function getPageTemplates($blogid, $username, $password) {
return parent::send(
__FUNCTION__,
parent::INT($blogid),
parent::STR($username),
parent::STR($password)
);
}

function getOptions($blogid, $username, $password, $option) {
return parent::send(
__FUNCTION__,
parent::INT($blogid),
parent::STR($username),
parent::STR($password),
parent::ARR($option, 'string')
);
}

function setOptions($blogid, $username, $password, $option) {
return parent::send(
__FUNCTION__,
parent::INT($blogid),
parent::STR($username),
parent::STR($password),
parent::ARR($option, self::$STRUC_SET_OPTION)
);
}

function deleteComment($blogid, $username, $password, $commentid) {
return parent::send(
__FUNCTION__,
parent::INT($blogid),
parent::STR($username),
parent::STR($password),
parent::INT($commentid)
);
}

function editComment($blogid, $username, $password, $commentid, $comment) {
return parent::send(
__FUNCTION__,
parent::INT($blogid),
parent::STR($username),
parent::STR($password),
parent::INT($commentid),
parent::STRUC($comment, self::$STRUC_EDIT_COMMENT)
);
}

function newComment($blogid, $username, $password, $postid, $comment) {
return parent::send(
__FUNCTION__,
parent::INT($blogid),
parent::STR($username),
parent::STR($password),
parent::INT($postid),
parent::STRUC($comment, self::$STRUC_NEW_COMMENT)
);
}

function getCommentStatusList($blogid, $username, $password) {
return parent::send(
__FUNCTION__,
parent::INT($blogid),
parent::STR($username),
parent::STR($password)
);
}

function getPage($blogid, $pageid, $username, $password) {
return parent::send(
__FUNCTION__,
parent::INT($blogid),
parent::INT($pageid),
parent::STR($username),
parent::STR($password)
);
}

function getPages($blogid, $username, $password) {
return parent::send(
__FUNCTION__,
parent::INT($blogid),
parent::STR($username),
parent::STR($password)
);
}

function getPageList($blogid, $username, $password) {
return parent::send(
__FUNCTION__,
parent::INT($blogid),
parent::STR($username),
parent::STR($password)
);
}

function newPage($blogid, $username, $password, $page, $publish) {
return parent::send(
__FUNCTION__,
parent::INT($blogid),
parent::STR($username),
parent::STR($password),
parent::STRUC($page, self::$STRUC_NEW_PAGE),
parent::BOOL($publish)
);
}

function deletePage($blogid, $username, $password, $pageid) {
return parent::send(
__FUNCTION__,
parent::INT($blogid),
parent::STR($username),
parent::STR($password),
parent::INT($pageid)
);
}

function editPage($blogid, $pageid, $username, $password, $page) {
return parent::send(
__FUNCTION__,
parent::INT($blogid),
parent::INT($pageid),
parent::STR($username),
parent::STR($password),
parent::STRUC($page, self::$STRUC_NEW_PAGE)
);
}

function getAuthors($blogid, $username, $password) {
return parent::send(
__FUNCTION__,
parent::INT($blogid),
parent::STR($username),
parent::STR($password)
);
}

function getCategories($blogid, $username, $password) {
return parent::send(
__FUNCTION__,
parent::INT($blogid),
parent::STR($username),
parent::STR($password)
);
}

function newCategory($blogid, $username, $password, $category) {
return parent::send(
__FUNCTION__,
parent::INT($blogid),
parent::STR($username),
parent::STR($password),
parent::STRUC($category, self::$STRUC_CATEGORY)
);
}

function deleteCategory($blogid, $username, $password, $categoryid) {
return parent::send(
__FUNCTION__,
parent::INT($blogid),
parent::STR($username),
parent::STR($password),
parent::INT($categoryid)
);
}

function suggestCategories($blogid, $username, $password, $category, $maxresults) {
return parent::send(
__FUNCTION__,
parent::INT($blogid),
parent::STR($username),
parent::STR($password),
parent::STR($category),
parent::INT($maxresults)
);
}

function uploadFile($blogid, $username, $password, $data) {
return parent::send(
__FUNCTION__,
parent::INT($blogid),
parent::STR($username),
parent::STR($password),
parent::STRUC($data, self::$STRUC_FILE)
);
}

function getComment($blogid, $username, $password, $commentid) {
return parent::send(
__FUNCTION__,
parent::INT($blogid),
parent::STR($username),
parent::STR($password),
parent::INT($commentid)
);
}

function getComments($blogid, $username, $password, $commentid) {
return parent::send(
__FUNCTION__,
parent::INT($blogid),
parent::STR($username),
parent::STR($password),
parent::INT($commentid)
);
}
}
?>




2011/1/26 結構バグが多かったので修正。またWordPress APIも追加。

テーマ:PHP - ジャンル:コンピュータ
コメント
この記事へのコメント
コメントを投稿する

管理者にだけ表示を許可する
トラックバック
この記事のトラックバックURL
この記事へのトラックバック
copyright © 2004-2006 SHOJI, Powered By FC2ブログ all rights reserved.
上記広告は1ヶ月以上更新のないブログに表示されています。新しい記事を書くことで広告を消せます。