Tag Archives: article

XML-RPC

This is simply a copy of my old XML-RPC article (2005). I’ve done a bit of content editing when converting the article to this blog format, but still most of the content is unchanged.

1. Introduction

So you’ve decided to learn XML-RPC[1], or did the name just sound “freaky” and raised a little interest? If it did I suggest you read on, if you already know what XML-RPC is, then just skip the next intro part

1.1 What is XML-RPC

XML-RPC is a way to make web services, it packs the requests and responses in XML both the client and the server can understand.
For those who don’t know a web service is a function of some sort placed on the net (it could be a function to convert a string into all upper case letters, which we are going to make in the examples below). The smart thing is that, a web service can be programmed in any kind of language, and any other language can use it. Many big sites have a web service, for example Amazon has a web service that allows you to search books and information.
Another smart thing about XML-RPC (and to my knowledge all other web services), is that they can be run from a web server. Not only is this smart, because you don’t need to install some high-end server dedicated to the purpose. But you don’t have to worry about firewalls.

1.2 Installing XML-RPC

If you’ve installed an extension before, this should be no problem at all, just install the XML-RPC extension. But if this is your first time, the following links will help a bit:
Windows users: http://www.php.net/manual/en/install.windows.extensions.php
Unix users: http://www.php.net/manual/en/install.unix.php
OS X users: http://www.php.net/manual/en/install.macosx.php
For you guys(and gals) who compile PHP: –with-xmlrpc. Read more »

Pic2HTML

BeeThis is simply a copy of my old pic2html article (2005). I’ve done some minor content editing, mostly spelling but there still might be a few lines that doesn’t make any sense.

1. Introduction

A long long time ago, I was a little trip by http://romanm.ch (no longer the same site), and well I must admit pretty impressing, try to take a look your self, especially the movies are well made.
Anyway, this gave me the idea of making an image to pure html code. My theory was that it would be simple to make a picture look good even with nothing but html (and as you’ll see later this was the truth, it’s possible to make 100% replicas), even though it’ll be size heavy.
Before we get going, remember to enable the GD library[1], for PHP. If you don’t know how, consult the PHP manual. I wont be going into details about the different GD lib functions, so you’ll need to read up on the GD functions you’re not familiar with in the manual as well.

2. rgb2html

First off we need a function to change the RGB color code to HTML color code (or simple from decimal to hexadecimal[2]). Lucky for us PHP is filled with a lot of helper functions, that sometimes turns out to be, well useful. This time it’s the great comeback for the dechex function, which convert a decimal number to hexadecimal. I’ve packed this down in a little function:

1
2
3
4
5
6
7
8
9
10
11
12
<?php
function rgb2html($clrR, $clrG, $clrB, $intReturn = 2) {
    $clrR = substr("0" . dechex($clrR), -2);
    $clrG = substr("0" . dechex($clrG), -2);
    $clrB = substr("0" . dechex($clrB), -2);
 
    if ($intReturn == 1)
        return array('red' => $clrR, 'green' => $clrG, 'blue' => $clrB);
    elseif ($intReturn == 2)
        return $clrR . $clrG . $clrB;
}
?>

In our code, all we need is the function which spits out the color code as a string, but I got carried away and made a little extra (return as an array). And you can like any other of my functions in any of my articles, (ab)use them like a mad man, if that’s you desire, just as long as you don’t say you’ve written then your self. Read more »

PHP Ping

pingThis is simply a copy of my old PHP Ping article (2005). I’ve done no content editing, only thing I’ve changed is the layout.

1. Introduction

Notice: I will change between binary, decimal and hexadecimal notations, but if you let your mouse rest above the number, the 2 other notations will apear. Try moving your mouse over this number: 1010 0001

Before you start, let me warn you, that this may seem as some heavy reading, but hang in there.
Remember you have to enable socket programming, if you’re not sure how this is done, referer to the PHP manual, on installing[1] PHP.
This article will walk you through creating a valid ping function in PHP. And in this process I will be covering network programming (shortly, you can always read about that somewhere else) and working with bitwise operations[2]. First off we’ll be discussing how a valid ping (or Echo message, of the Internet Control Message Protocol).

1.1 Echo Message

To make a valid ping to another network device, it’s important you follow the ICMP standard, they can be found in RFC-792[3]. And yes you’re absolutly right RFC documents are just as boring as the dictionary, but sometimes they come in handy. I’ve decided to run through the standard quickly, so we can move on.
It’s build up by 6 fields, which looks like the following:

Type

(8 bit)
Code

(8 bit)
Checksum

(16 bit)
Identifier

(16 bit)
Sequence Number

(16 bit)
Data

(… bit)

Yep, it might seem a little confusing I know, but it’s not that hard to understand. It’s nothing but a single line of bits, starting with type and ending with data, now let me explain them a little deeper:

  • Type: This defines what kind of message we whish to send. What we want to send is an Echo Request, which has the type 1000, there’s a long list[4] of different messages, and their purpose.
  • Code: In our case we set the code to 0000, because the echo message dosn’t have any other options. You can compare the Type with the function and the Code with the parameters.
  • Checksum: The checksum[5] is calculated when then package is assembled, to start with we set the checksum to 0000 0000. Then later the checksum is calculated by one’s Complement. If you’re not use to binary operations, this will be hard to explain, and there’s no easy reading on the net, try to google it[6].
  • Identifier: In the original ping program, this is the UNIX process ID, but in our ping it can be anything. Normaly I just set it to 0000 0000, but it’s all up to you. In some cases it could be smart to make it unique so you can recongnize your ICMP package.
  • Sequence Number: Again just a number, in our case it’s 0000 0000 as the Identifier. But a good use for this, is to increment it if you run more than 1 ping at a time.
  • Data: This can be any data. In our case, we use: “Scarface”

So basicly that is the package we wish to make, for our ping to be correct. The hard thing here is the checksum, we will work with this later on in the article.

2. Sockets

Before we start designing our package (well talking about calculating the checksum), lets talk a little about network programming.
Normally when people talk about network programming, they’re talking about TCP/IP or UDP/IP protocols. But we are going to use the ICMP protocol. But enough about that, let’s start looking at the functions we’ll be using.
Read more »