How to Upload using WordPress MetaWeblog.newMediaObject and IXR_Library

How to Upload using WordPress MetaWeblog.newMediaObject and IXR_Library

Continuing my XMLRPC article, I will show how to use wordpress image upload metaWeblog.newMediaObject and IXR_Library.

I still to use IXR_Library class to upload media remotely on wordpress, where previously I apply to my article on Remote wordpress post with XML-RPC metaWeblog.newPost and IXR_Library Class

The advantage of using IXR_Library is IXR_Base64 classes that facilitate the use of base64 code in bits parameter in metaWeblog.newMediaObject.

The parameters used in metaWeblog.newMediaObject

  • blogid (int).
  • username (string ).
  • password (string)
  • struct data
    • name => Filename (string).
    • type => File MIME type (string).
    • bits => base64-encoded binary data (string).
    • overwrite => Bool ( Optional – Overwrite an existing attachment of the same name).

 Okay, lets begin the tutorial Upload using WordPress MetaWeblog.newMediaObject and IXR_Library 

1. First , you need to download IXR_Library.php here.

2. Create new php file as upload_image.php, and put IXR_Library.php in the same folder with it. Don’t forget to put your image in your directory (In my example folder/image.jpg)

3. Copy this script below on upload_image.php

	include "IXR_Library.php";
	$XmlRpc_result = null;
	$url= 'http://your_url.com/xmlrpc.php';
	$XmlRpc_client = new IXR_Client ($url);
	$fs = filesize ('folder/image.jpg');
	$file = fopen ('folder/image.jpg', 'rb');
	$data = fread ($file, $fs);
	fclose ($file);
	$content = array
	(
		'name' = 'image_name.jpg',
		'type' = 'image/jpeg',
		'bits' = new IXR_Base64 ($data)
	);
	$params = array(1,'your_username','your_password',$content);
	 try{
		$XmlRpc_result = $XmlRpc_client->query(
			'metaWeblog.newMediaObject',$params
		);
		$data = $XmlRpc_client->getResponse();
		echo $data['id'].'<br>';
                echo $data['file'].'<br>';
                echo $data['url'].'<br>';
	        echo $data['type'].'<br>';
         }
	 catch (Exception $e){
		 var_dump ( $e->getMessage ());
	 }

Explanation

        $fs = filesize ('folder/image.jpg');
	$file = fopen ('folder/image.jpg', 'rb');
	$data = fread ($file, $fs);
	fclose ($file);

We need to convert image file into string using fread php, ( see fread documentation here)

$content = array
	(
		'name' => 'image_name.jpg',
		'type' => 'image/jpeg',
		'bits' => new IXR_Base64 ($data)
	);

 Use new IXR_Base64 to encode $data

$XmlRpc_result = $XmlRpc_client->query(
			'metaWeblog.newMediaObject',$params
		);
		$data = $XmlRpc_client->getResponse();
		echo $data['id'].'<br/>';
                echo $data['file'].'<br/>';
                echo $data['url'].'<br/>';
	        echo $data['type'];

$XmlRpc_client->getResponse() will send back the return value.

Get image id and combine it with metaWeblog.newPost  to set featured image. To set featured image in your post, create custom_fields with key ‘_thumbnail_id’ and value ‘image_id’. see the example below

     $customfields=array('key'=>'_thumbnail_id', 'value'=>'53'); // set featured image here

     $title = htmlentities($title,ENT_NOQUOTES,$encoding); 
     $keywords = htmlentities($keywords,ENT_NOQUOTES,$encoding); 
 
     $content = array( 
 'title'=>$title, 
 'description'=>$body, 
 'mt_allow_comments'=>0, // 1 to allow comments 
 'mt_allow_pings'=>0, // 1 to allow trackbacks 
 'post_type'=>'post', 
 'mt_keywords'=>$keywords, 
 'categories'=>array($category), 
 'custom_fields' => array($customfields) , // how to set custom_fields
 'date_created_gmt' => $date
 );  

Thus tutorial How to upload using WordPress MetaWeblog.newMediaObject and IXR_Library, if you need any question please leave the comment. Read another wordpress xmlrpc tutorial here.

The following two tabs change content below.
This site is a personal Blog of Sigit Prasetya Nugroho, a Desktop developer and freelance web developer working in PHP, MySQL, WordPress.

Leave a Comment

Your email address will not be published.

Time limit is exhausted. Please reload CAPTCHA.

%d bloggers like this: