WordPress: Posting images using XML-RPC

The following code uploads a file using xmlrpc... and without using all kinds of libs, you just need the regular php-xmlrpc module.

$rpcurl='http://www.enter-your-domain-name-he.re/xmlrpc.php';
$username='admin';
$password='your-password';
$blogid=1; //Post ID
 
$file=file_get_contents('file.jpg');
$filetype = "image/jpeg";
$filename = "remote_filename.jpg";
 
xmlrpc_set_type($file,'base64'); // <-- required!
$params = array($blogid,$username,$password,
            array('name'=>$filename,'type'=>$filetype,'bits'=>$file,'overwrite'=>false));
$request = xmlrpc_encode_request('wp.uploadFile',$params);
 
$result = go($request,$rpcurl);
print_r($result);
 
function go($request,$rpcurl){
    $ch = curl_init();
    curl_setopt($ch,CURLOPT_POST,1);
    curl_setopt($ch,CURLOPT_URL,$rpcurl);
    curl_setopt($ch,CURLOPT_POSTFIELDS,$request );
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    $result = curl_exec($ch);
    return $result;
}
© GeekLabInfo WordPress: Posting images using XML-RPC is a post from GeekLab.info. You are free to copy materials from GeekLab.info, but you are required to link back to http://www.geeklab.info

1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 4.50 out of 5)
Loading...

3 comments on “WordPress: Posting images using XML-RPC”

  1. The above code is not working with the new wordpress version. I am trying to upload a music album cover via xmlrpc, it is showing nothing. Can you please update the above code?

  2. I've tested it, code still works perfectly to upload images on WordPress 4.7. The print_r line was not giving back much information though, because the "return $result" line was missing.

  3. Hi, I can upload the file to WP through RPC. I upload images. But how can I call the WP image process to work? Because I need this process to do the cropping/resizing uploaded images too. Right now the uploaded image only like a text file contains base64 string in a filename of .jpg.

Leave a Reply