WordPress.org

Ready to get started?Download WordPress

Forums

XML-RPC wp.uploadFile breaks images (4 posts)

  1. 80leaves
    Member
    Posted 2 years ago #

    Hi everybody,

    I have following simple code to upload a file through xml-rpc to my wordpress blog. The file appears within the wordpress media library, but the image can't be displayed as it seems to be corrupt. I assume I did something wrong with the base64 encoding.

    <?php
    
    require('./wp-includes/class-IXR.php');
    $rpc = new IXR_CLIENT('http://myBlog.com/xmlrpc.php');
    
    $image = array(
        'name'  => 'myTestImagenew.jpg',
        'type'  => 'image/jpg',
        'bits'  => base64_encode(file_get_contents("exifTest.jpg")),
    	'overwrite' => 0);
    
    $status = $rpc->query(
    	'wp.uploadFile',
    	1,
    	'USERNAME',
    	'PASSWORD',
    	$image);
    
    if(!$status){
    	echo 'Error ('.$rpc->getErrorCode().'): '.$rpc->getErrorMessage();
    	exit;
    }
    
    echo 'new post id: '.$rpc->getResponse();
    ?>

    Does anybody have some hints?

  2. 80leaves
    Member
    Posted 2 years ago #

    after a little research I found a few things out:

    'bits' => base64_encode(file_get_contents("exifTest.jpg")),
    should be
    'bits' => new IXR_Base64($data)
    in combination with

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

    catching the send xml i found out that instead of <base64> the image is decoded as <string>, even with using new IXR_BASE64(data)

    Am I using it just wrong or does anyone have a hint to resolve it?

    Thx alot

  3. 80leaves
    Member
    Posted 2 years ago #

    my bad, the codesnipped worked, it was a typo in the file resource, using new IXR_Base64(...) works nice

  4. seqqueman
    Member
    Posted 2 years ago #

    Great, thanks for your posts.
    This is the code I use:

    <?php
    
    require('path/to/class-IXR.php');
    $rpc = new IXR_CLIENT('http://myblog.com/xmlrpc.php');
    
    $username = 'user';
    $password = 'pass'; 
    
    $url = "http://...JPG";
    
    if(function_exists('file_get_contents')) {
    $image = array(
        'name'  => 'myTestImagenew.jpg',
        'type'  => 'image/jpg',
    	'bits' => new IXR_Base64(file_get_contents($url)),
    	'overwrite' => 0);
    
    $status = $rpc->query(
    	'wp.uploadFile',
    	1,
    	$username,
    	$password,
    	$image);
    
    if(!$status){
    	echo 'Error ('.$rpc->getErrorCode().'): '.$rpc->getErrorMessage();
    	exit;
    }else{
    	$response = $rpc->getResponse();
    	echo 'The URL is: '.$response['url'];
    }
    }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic