The Cache: Technology Expert's Forum
 
*
Welcome, Guest. Please login or register. October 14, 2019, 06:19:58 PM

Login with username, password and session length


Pages: [1]
  Print  
Author Topic: metaWeblog.newPost to Wordpress blog (xmlrpc.php)  (Read 13774 times)
Bompa
Administrator
Lifer
*****
Offline Offline

Posts: 564


Where does this show?


View Profile
« on: January 10, 2009, 09:27:16 PM »

I've been using XMLRPC::Lite when remote posting to my fleet of wordpress blogs,
but I don't like anything about it:

I mean, REALLY, does this look like normal code?
Code:
    my $self = {
        server   => XMLRPC::Lite->proxy( $post_url ),
        username => $user,
        password => $pass
    };

eval {
    my $call = $self->{ server }->call(
        'metaWeblog.newPost',
        1,                              # blogid, ignored
        $self->{ username },
        $self->{ password }, {
            title       => $title,
            description => $description,
            categories  => [('Uncategorized')],
            mt_allow_comments => '0',
            mt_allow_pings => '0',
            mt_tb_ping_urls => '0',
            mt_keywords         => [@tags],

        },
        1                               # publish
    );

It took me two days to figure out how to send the tags: [@tags] a reference to list or someshit.   Huh?


XMLRPC::Lite comes with a feature that abruptly ends all code execution
on certain errors; called fatal, I have no idea why any programmer in his
right mind would build in such a thing.  Why not send a warning and let the
coder utilizing the module do as he wishes?

Anyway,I had to learn to use perls eval function to trap those errors.  This took
time from my nap schedule, but now I just get a warning and my code continues.

However, there is the matter of the response which the remote server sends back
to my program.  It's in some sort of data structure that no one can access without
using ANOTHER MODULE called Data::Dumper, which translates the data being held
hostage inside XMLRPC::Lite into a friendly text string.

However, Data::Dumper took me more time to learn (and only partially learn), and
sent me scurrying around the web for docs.

Geeesus!

So, I would like to just send my xml to xmlrpc.php with a simple POST and I have
got that working like this:

Code:
$xml_string =<<here;
<?xml version="1.0" encoding="UTF-8"?>
<methodCall>
<methodName>metaWeblog.newPost</methodName>
<params>
<param>
<value><int>1</int></value>
</param>
<param>
<value><string>$user</string></value>
</param>
<param>
<value><string>$pass</string></value>
</param>
TRIMMED...


But, the body of my post contains left angle brackets (for links) which are not allowed in XML format.

"Any characters are allowed in a string except < and &, which are encoded
as &lt; and &amp;. A string can be used to encode binary data."

- http://www.xmlrpc.com/spec


Ok, I can encode those, np, but Wordpress displays them as literal <, not as part of the HTML source code.

I also tried &#60;  but no go.

Has anyone got into this shit other than me?

Bompa
« Last Edit: January 10, 2009, 09:43:21 PM by Bompa » Logged

"The most beautiful and profound emotion we can experience is the sensation of the mystical..." - Albert Einstein
dirk
Global Moderator
Expert
*****
Offline Offline

Posts: 416


View Profile
« Reply #1 on: January 10, 2009, 10:09:20 PM »

Hi Bompa,

it's no easy stuff and the documentation isn't really good.

Anyway, your code makes sense. I'm using a similar one:

Code:
    my $xml = new XMLRPC::Lite;
    my $result;
    my $error;
    eval {
        my $server = $xml->proxy($blog_xmlrpc_url);

        my $call = $server->call(
            'metaWeblog.newPost',
            $blogID,
            $user_login,
            $user_pass,
            {
                'categories'  => [@categories],
                'title'       => $title,
                'description' => $description,
                'mt_excerpt'  => $excerpt,
                'mt_keywords' => $tags,
            },
            1
        );

        if ( $call->fault ) {
            $error = $call->faultstring;
        }
        else {
            $result = $call->result;
        }
    };

    my $post_id = q{};

    if ($@) {
        warn "new_post: Blog URL: $blog_url\neval returned error: $@!\n\n";
    }
    else {
        if ( defined($result) ) {
            if ( ref $result ne '' ) {    # $result is a hash
                warn
                    "new_post: Blog URL: $blog_url\n\$result is a hash! \$result: $result\n\n";
            }
            else {
                $post_id = $result;
            }
        }
        elsif ( defined($error) ) {
            warn
                "new_post: Blog URL: $blog_url\nRequest returned error: $error\n\n";
        }
        else {
            warn "new_post: Blog URL: $blog_url\nRequest failed: $!\n\n";
        }
    }

I have added a detailed error handling and return the $post_id
of the new posting.

Dirk
Logged
Bompa
Administrator
Lifer
*****
Offline Offline

Posts: 564


Where does this show?


View Profile
« Reply #2 on: January 11, 2009, 04:55:30 PM »

I have added a detailed error handling and return the $post_id
of the new posting.

Dirk


Thank you Dirk.


Logged

"The most beautiful and profound emotion we can experience is the sensation of the mystical..." - Albert Einstein
Bompa
Administrator
Lifer
*****
Offline Offline

Posts: 564


Where does this show?


View Profile
« Reply #3 on: January 15, 2009, 09:47:24 PM »

I've been using XMLRPC::Lite when remote posting to my fleet of wordpress blogs,
but I don't like anything about it:

Just to add to my rant, I upgraded my activeperl the other day from 5.8 to 5.10
(how they number these things, I dunno), and come to find out, the latest version
does not come with the XMLRPC::LITE module.  I mean, it's not available with the
Perl Package Manager (PPM).  gawd!

I suppose I could install it manually, but I've never had any luck with that route
in the past, so I have both versions of perl installed and I have added

use lib 'C:/Perl5.8/site/lib';

to the top of my script that needs to find the xmlrpc:lite module.  That line adds that path
to @INC which contains all the paths that Perl needs to search in order to load a module.
We'll see how that goes.







Quote
So, I would like to just send my xml to xmlrpc.php with a simple POST and I have
got that working like this:

Code:
$xml_string =<<here;
<?xml version="1.0" encoding="UTF-8"?>
<methodCall>
<methodName>metaWeblog.newPost</methodName>
<params>
<param>
<value><int>1</int></value>
</param>
<param>
<value><string>$user</string></value>
</param>
<param>
<value><string>$pass</string></value>
</param>
TRIMMED...


But, the body of my post contains left angle brackets (for links) which are not allowed in XML format.

"Any characters are allowed in a string except < and &, which are encoded
as &lt; and &amp;. A string can be used to encode binary data."

- http://www.xmlrpc.com/spec


I did find a solution to this.  Include that data that needs to be escaped in a CDATA thingie.


Code:
<member><name>description</name><value><string><![CDATA[$description]]></string></value></member>

The $description is the variable that, in my case, contains all sorts of text and html, including
hyperlinks.

Happy new year!
Bompa

PS: Some of you know that I've been fiddling around with the GD library pixel functions with PHP
as nothing similiar has been available for ActivePerl (perl for windows), HOWEVER, GD now works with
Windows (need activeperl 5.10).  I just discovered this yesterday and quicker than a New York heartbeat
moved my GD code from php to perl.  Now, I'm just whizzing thru that stuff.

« Last Edit: January 15, 2009, 09:58:45 PM by Bompa » Logged

"The most beautiful and profound emotion we can experience is the sensation of the mystical..." - Albert Einstein
perkiset
Olde World Hacker
Administrator
Lifer
*****
Offline Offline

Posts: 10096



View Profile
« Reply #4 on: January 15, 2009, 11:47:48 PM »

Right on Bomps, always nice when a plan comes together. Glad you're back in a tool you're better with.
Logged

It is now believed, that after having lived in one compound with 3 wives and never leaving the house for 5 years, Bin Laden called the U.S. Navy Seals himself.
Pages: [1]
  Print  
 
Jump to:  

Perkiset's Place Home   Best of The Cache   phpMyIDE: MySQL Stored Procedures, Functions & Triggers
Politics @ Perkiset's   Pinkhat's Perspective   
cache
mart
coder
programmers
ajax
php
javascript
Powered by MySQL Powered by PHP Powered by SMF 1.1.2 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks


Valid XHTML 1.0! Valid CSS!