Author Archives: Philip Birk-Jensen - Page 2

A quick look at the cURL Library

It was only until recently I discovered the PHP: cURL Library. I’ve been using homegrown functions for fetching HTTP requests, but with the cURL it’s not as much a hassle as it used to be. Remember that the cURL isn’t a PHP standard, and you need to install the extension.

The basic functions

I do have a tendency to forget the most basic cURL functions, so I’ve decided to make a quick post here I can use for references my self. It’s basically a small cURL tutorial.
I’ll start out with listing, a few basic functions, sorted in the order in which you use them.

Function Description
curl_init This creates a curl handle, which is passed with the rest of the functions. You can set the URL as an argument to this function, although I prefer to use the curl_setopt function.
curl_setopt Sets the options of the cURL handle (returned from curl_init), an option could be the URL, POST vars, header and so on. There’s many options, so refer to the constants list in the PHP manual.
curl_exec Execute your cURL with all the settings. Depending on the CURLOPT_RETURNTRANSFER option, it will return the content or, true or false.
curl_close Free up the handle when you’re done..

Simple HTTP get

This will use HTTP to call a website, and get the content. Basically this is just a file_get_contents, except you have all the extra options cURL give you:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
// 1) Create the curl handle:
$ch = curl_init();
 
// 2) Set the options you want for the current handle:
curl_setopt($ch, CURLOPT_URL,            "http://www.example.com/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
 
// 3) Retrieve the data:
$data = curl_exec($ch);
 
// 4) Close up:
curl_close($ch);
 
var_dump($data)
?>
Snippet of the data output<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>

</HTML>

Like mentioned we could use var_dump(file_get_contents(“http://www.example.com”)) to get the exact same output, but as an added bonus (on top of the vast set of options) cURL actually performs better than FGC. 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 »

Hello World

Just a quick hi there.
This is my first attempt on a blog, and for starters I will be converting my previous PHP artices into blog posts. This will allow for comments and questions on the articles, and I’ve long been hungry for some feedback. But still to lazy to write a simple comment feature for my articles.

Anyway, that was it for starters, just wanted to have a post I could play a bit around with.