How to upload attachments such as image by metaWeblog API ?

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.
<?
//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;
}
Another posts included in "PHP"
| XMLRPC example (0) | 2008/01/07 |
| How to block a connection from the certain site ? (0) | 2008/01/14 |
| File downloading via HTTP (0) | 2008/01/21 |
| How to post blog content by metaweblog API ? (3) | 2008/01/02 |
| Posting to a Wordpress Blog Via XMLRPC Using Blogger API (1) | 2007/12/15 |
| foreach loop (0) | 2007/10/16 |
| Can PHP switch in strings ? (0) | 2007/10/16 |
| PHP Switch Example (0) | 2007/10/16 |
Trackback : Cannot send a trackbact to this post.
-
Subject different money making ideas
2010/01/28 22:58
moneyideas
-
Subject different money making ideas
2010/01/29 07:13
moneyideas
-
Subject different money making ideas
2010/01/31 16:43
moneyideas
Prev

Rss Feed