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...