For the new year, one kind person gave me a domain. If there is a domain, then there must be something there. I decided to make myself a blog. But since the last 2 years I used livejournal I wanted my posts to be synchronized with LJ.
I program with the help of my favorite Zend Framework.
Part of the live logging API is based on the
XML-RPC protocol , a description of this API can be found
here .
In order to add a new post in LJ, we first need to get a challenge.
We will use the Zend_XmlRpc_Client component from the Zend Framework.
Create its object -
$xmlRpcClient = new Zend_XmlRpc_Client( 'http://www.livejournal.com/interface/xmlrpc' );
the incoming parameter is the server that we are going to contact in this case, we have the server LJ.
Send a request to the server:
$chalengeResponse = $xmlRpcClient->call( 'LJ.XMLRPC.getchallenge' );
This request will return the server's response to us - this is an array in which both the challenge will be specified, it will be hashed along with your LJ password and sent to the server - after all, it will be more secure to transfer passwords. Also in this array, you get the server LJ server and the time when your challenge expires. We will not wait until it expires and immediately post.
But first you need to properly form a request.
If you glance
here you will find an example of the necessary xml file which should be sent, you should also notice that it contains a single parameter of the struct type.
In Zend_XmlRpc_Client - all data types from php are automatically converted to the required ones for XML-RPC.
Well, just in case, we will take another component that is Zend_XmlRpc_Value, namely Zend_XmlRpc_Value_Struct for our case. Like any Zend_XmlRpc_Value, this component has a getAsDOM () method that will return you an object of the
DOMDocument class that is included in php. There is also a saveXML () method - with which you can get xml. In our case, we can display what we get to see the differences from the example and make some corrections.
The Zend_XmlRpc_Value_Struct constructor takes an associative array.
well, about it we will do something 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' => ' 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 passed an array of parameters, let's figure out what the parameters were there:
- username - user name
- auth_method - authorization method
- auth_challenge - the challenge we got
- auth_response - the md5 challange encrypted with the same password from the LJ encrypted
- event - the text of our topic
- subject - the topic header
- the following 5 parameters indicate the date of publication of the topic (yes, yes, you can put it in hindsight :))
props are the parameters of the topic; you can read a lot of them for a long time about them here . - security - access rights can be public, private and usemask; if you use the latter, you will need to specify the parameter allowmask. I indicated that I need to preserve formatting and tags.
this parameter is also a struct.
It now remains to create our query and execute it:
$request = new Zend_XmlRpc_Request();
$request->setMethod( 'LJ.XMLRPC.postevent' );
$request->addParam($postOptions);
$xmlRpcClient->doRequest($request);
We created a request object. Zend_XmlRpc_Request set the LJ.XMLRPC.postevent method for it, added a parameter, and executed a request to the server.
The Zend_XmlRpc_Request class also has a toXml method that returns the generated xml, in case we need to use it somewhere else, for example, save it and give it to a friend.
we also want to get the id of the post in LJ and its link
we use the getLastResponse () method, and then we get what the server answered us with the getReturnValue () method
$newPost = $xmlRpcClient->getLastResponse()->getReturnValue();
In the $ newPost array, there was a server response, which in itself contains a link to a post in LJ and its id.
In fact, it could have been much easier. I intentionally complicated everything to show the other components involved in Zend_XmlRpc_Client.
It could be like this:
$newPost = $xmlRpcClient->call( 'LJ.XMLRPC.getchallenge' , array( /* */ ));
In the array of parameters we could shove all the parameters that we need only
It is worth remembering how the data types are converted from php to xml-rpc because they will be converted automatically by this table:
Type in PHP | XML RPC Type |
integer | int |
double | double |
boolean | boolean |
string | string |
array | array |
array (associative) | struct |
object | array |
In order to edit a post, you just need to send an LJ.XMLRPC.editevent request with the same parameters as when adding a post, just add another itemid that is returned to you when you add a post.
And to delete a post, you need to edit it but without subject and event.
Thank you all, I hope someone come in handy. Maybe next time I will write about Zend_XmlRpc_Server if I figure out why to use it :).