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. A quick search turned up this page: file_get_contents VS CURL, what has better performance?, showing that cURL is faster than FGC, I’ve also done a very simple 1 call test, using the above example options:

1
2
3
4
5
6
7
8
<?php
$s = microtime(true);
$data = curl_exec($ch);
echo "cURL: ". (microtime(true) - $s) ."\n";
$s = microtime(true);
file_get_contents("http://www.example.com");
echo "FGC: ". (microtime(true) - $s) ."\n";
?>
Simple cURL vs FGC benchCURL: 0.41587805747986
FGC: 0.71178197860718
There’s other ways of simply getting the data from an HTTP request, and I’ll try to do a bit more performance tests on these different ways in a future blog post.

Options

There’s to many options to list them all here, but go take a look at the curl_setopt function. I’ll just show one last example of how to use POST in your request, with a simple string:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?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);
curl_setopt($ch, CURLOPT_POST,           1);
curl_setopt($ch, CURLOPT_POSTFIELDS,     "key1=var1&key2=var2");
 
// 3) Retrieve the data:
$data = curl_exec($ch);
 
// 4) Close up:
curl_close($ch);
 
var_dump($data)
?>

It’s possible to pass an array instead of a string in the CURLOPT_POSTFIELDS, this will change the Content-Type to multipart/form-data.
This was just a few examples, cURL support HTTP authentication, FTP, File POST, UserAgent spoof, and much much more.

Leave a comment

0 Comments.

Leave a Reply

[ Ctrl + Enter ]