Using CURL in PHP

In Python, to read a URL, I do the following:

import urllib
data = urllib.urlopen('').read()

PHP links against cURL, which lets you accomplish the same thing … in five lines of code.

If this is something you plan to do more than, say, once, you’ll want an easier way of doing this. Here’s the function I use:

function readUrl($url) {
	$curlHandle = curl_init();
	curl_setopt($curlHandle, CURLOPT_URL, $url);
	curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
	$data = curl_exec($curlHandle);
	if (!$data) {
		print curl_errno($curlHandle) . "  " . curl_error($curlHandle);
		return false;


	return $data;

(keywords: curl php wrapper example)

Leave a Reply

Your email address will not be published. Required fields are marked *