Zend_XmlRpc_Client and Crossposting to LiveJournal

a new year, a kind person gave me a domain. If you have a domain then there something should be. Decided to make a blog. But since the last 2 years I used livejournal, I wanted to have my messages synced with Facebook.
I programmed with the help of a loved Zend Framework.

Part of the API of LiveJournal based on Protocol XML-RPC, for a description of this API can be found here.
To add a new post on LJ first, we need to challenge.
Will use Zend_XmlRpc_Client component of Zend Framework.
Create object
$xmlRpcClient = new Zend_XmlRpc_Client('http://www.livejournal.com/interface/xmlrpc');

the incoming parameter is we have transferred the server to which we will apply in this case, we have specified the server LJ.
Send the request to the server:

$chalengeResponse = $xmlRpcClient- > call('LJ.XMLRPC.getchallenge');


This query will return us the server's response is an array which will contain and challenge, it will are hashed with your password on LJ and go on a server it is more secure to transmit passwords. Also in this array you will get the server time learn and time expires your challenge. We will not wait until it will expire and will immediately post.
But you must first properly form the request.
If you look here you find an example of the desired xml file to be uploaded, you should notice that it contains the only parameter of the struct type.

In Zend_XmlRpc_Client — all types of data from php automatically transforming into need for XML-RPC.
Well we just in case we get another component is Zend_XmlRpc_Value namely Zend_XmlRpc_Value_Struct for our case. Like any Zend_XmlRpc_Value this component has a method getAsDOM() which will return an object of class DOMDocument which is included in php. Also there is a method saveXML() — c which you can use to xml. In our case, we can display what we get to see the differences from the example and to make any amendments.
In Zend_XmlRpc_Value_Struct constructor takes an associative array.
well, something that we can do like this:

$postOptions = new Zend_XmlRpc_Value_Struct(
array(
'username' => $ljUsername
'auth_method' => 'challenge'
'auth_challenge' => $chalengeResponse['challenge']
'auth_response' => md5($chalengeResponse['challenge'] . md5($this->_ljPassword))
'ver' =>'1'
'event' => 'This post was written using the Protocol xml-rpc'
'subject' => 'xml-rpc test'
'year' => 2010
'mon' => 1,
'day' => 3,
'hour' => 4
'min' => 33
'props' => array(
'opt_preformatted' => true
'taglist' => 'tag1, tag2, tag3'
)
'security' =>'public'
));


* This source code was highlighted with Source Code Highlighter.


In the constructor we pass in a parameters array, let's understand that there were options:
the
    the
  • username — the name of the LJ user
  • the
  • auth_method — authentication method
  • the
  • auth_challenge — challenge which we have received
  • the
  • auth_response — encrypted by md5 function challenge along with the encrypted password from LiveJournal
  • subject — the title of the topic

    the following 5 parameters indicate the date of publication of a topic (Yes, you can put in hindsight :) )
    props are the parameters of a topic a lot of them all listed for a long time can read about them here. the

  • security — permissions can be public, private and usemask if you are using the latest then you need to be sure to specify the allowmask parameter. I pointed out that I need to preserve the formatting and tags.
    this option is also — struct.


Now we have to create our query and execute it:

$request = new Zend_XmlRpc_Request();
$request->setMethod('LJ.XMLRPC.postevent');
$request- > addParam($postOptions);
$xmlRpcClient->doRequest($request);



We have created a request object Zend_XmlRpc_Request introduced a method LJ.XMLRPC.postevent, added the option and made the request to the server.
The class Zend_XmlRpc_Request also has a method toXml which returns the generated xml, in case we need to use it somewhere else, like to preserve and give to a friend by heart.

we just want to get the id of the post in LJ and link
we use the method getLastResponse(), and then get that answered us server with method getReturnValue()
$newPost = $xmlRpcClient- > getLastResponse ()- > getReturnValue();


In the array $newPost was the server's response, which in itself contains a link to the post in LiveJournal and it id.

Actually could be much easier I intentionally complicated to show all the other components participating in the work of the Zend_XmlRpc_Client.
Could be this:
$newPost = $xmlRpcClient- > call('LJ.XMLRPC.getchallenge', array(/*here the parameter array*/));

In the array of options we could put all the parameters we need only
it is worth remembering how to transform data types from php to xml-rpc for they will be converted automatically on the sign:
the the the the the the the the
PHP XML-RPC
integer int
double double
boolean boolean
string string
array array
array (associative) struct
object array


To edit a post, you just send a request to LJ.XMLRPC.editevent with the same parameters as when adding a post, just add another itemid which is refundable when you add a post.
And to remove the post you need to edit it but without a subject and event.

Thank you all, I hope someone can come in handy. Maybe next time I will write about Zend_XmlRpc_Server, if I think of why to use it :).
Article based on information from habrahabr.ru

Комментарии

Популярные сообщения из этого блога

ODBC Firebird, Postgresql, executing queries in Powershell

garage48 for the first time in Kiev!

The Ministry of communications wants to ban phones without GLONASS