• Hi

    I am using the xml-rpc interface and trying to use it to upload an image to my blog.

    I use the following code:

    $username = "username";
        $password = "password";
        $rpcurl = "http://myblog.com/xmlrpc.php";
    
        $image = fopen("file.jpg","r");
        $file = fread($image,filesize("file.jpg"));
        fclose($image);
        $filetype = "image/jpeg";
        $filename = "testimage.jpg";
    
    $result = wpUploadFileXMLRPC($blogid,$rpcurl,$username,$password,$filename, $filetype, $file);
    echo(print_r($result));
    
    function wpUploadFileXMLRPC($blogid,$rpcurl,$username,$password,$filename, $filetype, $file){
    
        $params = array('',$username,$password,array('name'=>$filename, 'type'=>$filetype,'bits'=>$file,'overwrite'=>true));
        $request = xmlrpc_encode_request('wp.uploadFile',$params);
        $result = makeCall($request,$rpcurl);
    
        return($result);
    }

    I get the following response from the blog:

    <?xml version=”1.0″?>
    <methodResponse>
    <params>
    <param>
    <value>
    <struct>
    <member><name>file</name><value><string>wpid-testimage.jpg</string></value></member>
    <member><name>url</name><value><string>http://myblog.com/wp-content/uploads/2010/03/wpid-testimage8.jpg</string></value></member&gt;
    <member><name>type</name><value><string>image/jpeg</string></value></member>
    </struct>
    </value>
    </param>
    </params>
    </methodResponse>

    Yet when I go to the URL given.. all that I see is the path of the URL not an image.

    Can anyone see what’s going wrong?

    Thanks

Viewing 5 replies - 1 through 5 (of 5 total)
  • Did you check the file on the server to make sure it got uploaded properly? When you call http://myblog.com/wp-content/uploads/2010/03/wpid-testimage8.jpg have you looked at the HTTP headers to make sure it’s returning what it should be? Specifically checking that it is returning the right content type.

    Thread Starter derbyshire5

    (@derbyshire5)

    Hi Joseph

    Yes, the type is wrong.. it’s just not getting the data in correctly..

    HTTP request sent, awaiting response… 200 OK
    Length: 8 [image/jpeg]

    Is what it was returning, so after digging some more I realised the file data was not base64 encoded, so I changed my function to now be:

    function wpUploadFileXMLRPC($blogid,$rpcurl,$username,$password,$filename, $filetype, $file){
    
        xmlrpc_set_type($file, 'base64');
        $params = array('',$username,$password,array('name'=>$filename, 'type'=>$filetype,'bits'=>$file,'overwrite'=>false));
        $request =  preg_replace('/
    /', '', xmlrpc_encode_request('wp.uploadFile',$params));
    
        echo($request);
        $result = makeCall($request,$rpcurl);
        echo("here\n");
        echo(var_dump($result)."\n");
        return($result);
    }

    And now it doesn’t work at all, no errors though.. the output from the script is:

    bool(false)

    If i dump out the request it looks like:

    <?xml version=”1.0″ encoding=”iso-8859-1″?>
    <methodCall>
    <methodName>wp.uploadFile</methodName>
    <params>
    <param>
    <value>
    <string/>
    </value>
    </param>
    <param>
    <value>
    <string>username</string>
    </value>
    </param>
    <param>
    <value>
    <string>password</string>
    </value>
    </param>
    <param>
    <value>
    <struct>
    <member>
    <name>name</name>
    <value>
    <string>testimage.jpg</string>
    </value>
    </member>
    <member>
    <name>type</name>
    <value>
    <string>image/jpeg</string>
    </value>
    </member>
    <member>
    <name>bits</name>
    <value>
    <base64>/9j/4AAQSkZJRgABAQEA<snip>some base64 encoded data</snip>AHYV1dXVoxv4EK3s//9k=</base64>
    </value>
    </member>
    <member>
    <name>overwrite</name>
    <value>
    <boolean>0</boolean>
    </value>
    </member>
    </struct>
    </value>
    </param>
    </params>
    </methodCall>

    Which looks fine?

    Any pointers most welcome 🙂

    Here’s what my test looked like:

    
    
    <?xml version="1.0"?>
    <methodCall>
    	<methodName>wp.uploadFile</methodName>
    	<params>
    		<param>
    			<value><string>blog_id</string></value>
    			</param>
    		<param>
    			<value><string>_USERNAME_</string></value>
    			</param>
    		<param>
    			<value><string>_PASSWORD_</string></value>
    			</param>
    		<param>
    			<value>
    			<struct>
    			<member>
    			<name>name</name>
    			<value><string>clear.gif</string></value>
    			</member>
    			<member>
    			<name>bits</name>
    			<value><base64>R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7</base64></value>
    			</member>
    			<member>
    			<name>type</name>
    			<value><string>image/gif</string></value>
    			</member>
    			</struct>
    			</value>
    			</param>
    		</params>
    	</methodCall>

    hi,
    I am a general physician and a beginer,currently i am using a blog from http://www.weblog.com. I need to use flickr to upload my pics on my blog. But whenever i try, i receieve following message:
    “The username and password you entered were not valid”
    Would you please tell me what mistake i am making?

    thanks

    drjawwadkhan –

    You’ll need to contact support for the product you are using, this forum is for WordPress users.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Using XML-RPC to upload images’ is closed to new replies.