| Actually this is one of the very important feature to post blog stuffs by metaWeblog API.
Followings are the simple example of posting attachment by metaWeblog API. Actually the unique filename should be passed as third parameter, but the blow sample code automatically extract the file name from the file source. <?php
//requires xmlrpc.inc from http://phpxmlrpc.sourceforge.net/
require_once('xmlrpc.inc');
$g_blog_url = "http://testurl.kurapa.com/api/";
$g_id = "***********";
$g_passwd = "*******";
$GLOBALS['xmlrpc_internalencoding'] = 'UTF-8';
function metaWeblog_newMediaObject( $blogid, $file_source, $file_name="")
{
global $g_id;
global $g_passwd;
global $g_blog_url;
// file name extraction from file source in case of having no file name as the third parameter
if ($file_name=="")
{
$p=0;
$file_len = strlen($file_source);
for($i=0; $i<$file_len; $i++)
{
if ($file_source[$i]=='/') $p=$i+1;
}
for( $i=$p; $i<$file_len; $i++) $file_name .= $file_source[$i];
}
// load file content
$fp=fopen($file_source, "rb");
if (!$fp) return null; // file open failure !!
while( !feof($fp))
{
$file_content .= fread( $fp, 1024); // 1024 is the server compatible buffer size
flush();
@ob_flush();
}
fclose($fp);
$client = new xmlrpc_client( "{$g_blog_url}");
$f = new xmlrpcmsg("metaWeblog.newMediaObject", // metaWeblog.newMediaObject Method
array(
new xmlrpcval("{$blogid}", "string"), // blogid.
new xmlrpcval($g_id, "string"), // user ID.
new xmlrpcval($g_passwd, "string"), // password.
new xmlrpcval( // attachment body
array(
'name' => new xmlrpcval($file_name, "base64"),
'bits' => new xmlrpcval($file_content, "base64"),
), "struct")
)
);
$f->request_charset_encoding = 'UTF-8';
$response = $client->send($f);
return $response;
} |