Some PHP for posting to the Red Matrix – Willkommen in der Christlichen Blogger Community

HURRAAA!!!! WIR SIND UMGEZOGEN!!!! * BESUCHE UNSERE NEUE SEITE! »

Some PHP for posting to the Red Matrix

By De Benny

I’ve been talking about the RedMatrix on this blog before. As you might know, the RedMatrix is a… well, it’s more than a social network, but you can use it that way, it is decentralised (there are several interacting servers called “hubs”), you can easily run your own (if I can do it so can you), have nomadic identities (which means you have all your data stored on your USB Stick)… but what do I tell you? I already wrote about it and you read it before.

Nonetheless: If this seems worth testing for you, why not register at my hub and play around a bit, just send me a message to confirm your account afterwards, because my hub is set to admin confirmation as I don’t want to drag spammers to my hub.

These days I played around a bit and came up with a php program to send status updates to my RedMatrix channels (a channel can be thought of as representing a person or an organisation, I have for example a personal channel and a channel that represents this blog, where articles from here are supposed to be crossposted to).

RedMatrix makes use of the former Twitter API, so if you used that one you almost certainly know more than I do… Anyway.

You can post to the RedMatrix by sending an HTTP POST request to a certain link which is made up of the hub you are registered on (as you can be registered on several hubs you can to my understanding use any of these – with the respective login credentials) follow by the path “/api/statuses/update”.

So let’s guess you are registered on my hub, https://red.debenny.de, the HTTP POST request is to be sent to the following address:

https://red.debenny.de/api/statuses/update

What we now need is the data we want to send there. First we need username and password. The username is the E-Mail address you used to register, not any of your channel names. Keep that in mind.

Next, we need the actual content of the status. Then we can also give it a title and add some categories. We also have to name the channel we intend to post to, as we could have several ones.

One more thing before we start with the input form. We can send either BBCode or HTML content. BBCode is the standard within the RedMatrix, there is also an option for Markdown, but that must be enabled to work, you’d use the BBCode setting to post Markdown I guess. Why not try it out and tell me your results? For now let’s be content with HTML and BBCode.

Let’s see at the input form:

<html>
<head>
        <title>Post to RedMatrix</title>
        <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
</head>
<body>

<form name="Inputform" action="statusupdate.php" method="post">
<table>
<tr><th>Hub address without trailing slash:</th><th><input type="text" name="hub"/></th></tr>
<tr><th>Channel name:</th><th><input type="text" name="channel"/></th></tr>
<tr><th>User Name:</th><th><input type="text" name="user"/></th></tr>
<tr><th>Password:</th><th><input type="password" name="pass"/></th></tr>
<tr><th>Title of Posting:</th><th><input type="text" name="title"/></th></tr>
<tr><th>Comma-seperated list of categories:</th><th><input type="text" name="category"/></th></tr>
<tr><th>Content of posting in either HTML or BBCode:</th><th><textarea cols="40" rows="10" name="content"></textarea>
<br />
   <input type="radio" name="ctype" value="htmlstatus"/>Content is HTML
   <input type="radio" name="ctype" value="status" checked="checked"/>Content is BBCode
</th></tr></table><br />
   <input type="submit" value="SUBMIT" />
</form>

</body>
</html>

I know using tables for formatting is not the best thing to do, but it gets the stuff in a certain order, and that’s all I wanted there.

You can see what we ask for in the form is what we spoke of before: The hub address, the channel name, username and password, a title for the status update, a list of categories (you just comma-seperate them like in WordPress) and the content of the status update you want to send. It can be in HTML or BBCode, you can choose by radio button how you want your content interpreted. BBCode, being the standard in the RedMatrix, is checked by default.

Once you’ve filled in all the needed data and push submit, statusupdate.php will be called. This is what it looks like:

<html>
<head>
        <title>Post to RedMatrix</title>
        <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
</head>
<body>
<?php
$user = $_POST['user'];
$pass = $_POST['pass'];
$hub = $_POST['hub'];
$content = $_POST['content'];
$ctype = $_POST['ctype'];
$title = $_POST['title'];
$channel = $_POST['channel'];
$category = $_POST['category'];



$I = new HttpRequest($hub.'/api/statuses/update.xml',HttpRequest::METH_POST);
$I->setOptions(array('httpauth' => $user.':'.$pass));
$I->addPostFields(array(
                        $ctype => $content,
                        'title' => $title,
                        'channel' => $channel,
                        'category' => $category));

try { echo $I->send()->getBody();}
catch (HttpException $ex){ echo $ex;}
?>
<br />
<a href="form.php">Back</a>
</body>
</html>

For ease of reading we first get all the data from the form into variables. Then we instantiate a new HttpRequest object and put it in $I. The HttpRequest is to call the above mentioned address, consisting of the hub address and “/api/statuses/update”. Not that here we have added “.xml”. This affects the output format we get delivered back. We can add either “.xml” or “.json”. If we leave it without ending, it defaults to “.json”.

Then we use the setOptions method to set username and password. I should note that you can also authenticate by oAuth, but I don’t know how this would be done as I have never used oAuth. As most hubs use ssl (https), I guess the login credentials are safe enough…

Next we add the array with the actual data we want submitted. We have the keys ‘title‘, ‘channel‘ and ‘category‘ and a fourth one that was picked in the form by the radiobuttons. It can be either ‘staus‘ for BBCode content or ‘htmlstatus‘ for HTML content. There might be more keys available, I think I remember about some that address crossposting to other networks, but I am not aware of them and in how far they are still working. As I didn’t need them, I didn’t bother about them…

Then we send the request and print out what we get back or the errors we run into. In the end we have a link back to the form.

I hope this was helpful for you. Don’t forget to chekc out the RedMatrix. And if you want to check the little program you just read about, here you can find it running.

Update: I also put it on github meanwhile.










Share 'Some PHP for posting to the Red Matrix' on LinkedIn
Share 'Some PHP for posting to the Red Matrix' on meinVZ
Share 'Some PHP for posting to the Red Matrix' on MySpace
Share 'Some PHP for posting to the Red Matrix' on Pinterest
Share 'Some PHP for posting to the Red Matrix' on Posterous
Share 'Some PHP for posting to the Red Matrix' on reddit
Share 'Some PHP for posting to the Red Matrix' on Scoop.it
Share 'Some PHP for posting to the Red Matrix' on Soup.io
Share 'Some PHP for posting to the Red Matrix' on studiVZ
Share 'Some PHP for posting to the Red Matrix' on Tumblr
Share 'Some PHP for posting to the Red Matrix' on Twitter
Share 'Some PHP for posting to the Red Matrix' on WordPress
Share 'Some PHP for posting to the Red Matrix' on XING
Share 'Some PHP for posting to the Red Matrix' on Email
Share 'Some PHP for posting to the Red Matrix' on Gmail
Share 'Some PHP for posting to the Red Matrix' on Google Translate
Share 'Some PHP for posting to the Red Matrix' on HootSuite

From:: Some PHP for posting to the Red Matrix

About the author

Chrblocom

Click here to add a comment

Leave a comment: