Search results for 'newMediaObject'. 1 post(s) found.

  1. 2008/01/07 How to upload attachments such as image by metaWeblog API ?
2008/01/07 14:24

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;
}

Trackback 3 Comment 0

Trackback : Cannot send a trackbact to this post.

  1. Subject different money making ideas

    Tracked from moneyideas 2010/01/28 22:58 delete

    moneyideas

  2. Subject different money making ideas

    Tracked from moneyideas 2010/01/29 07:13 delete

    moneyideas

  3. Subject different money making ideas

    Tracked from moneyideas 2010/01/31 16:43 delete

    moneyideas