Fatal error: Call to undefined function curl_init()

Thew picture Thew · Dec 9, 2010 · Viewed 162.2k times · Source
<?php
$filename = "xx.gif";
$handle = fopen($filename, "r");
$data = fread($handle, filesize($filename));

// $data is file data
$pvars   = array('image' => base64_encode($data), 'key' => IMGUR_API_KEY);
$timeout = 30;
$curl    = curl_init();

curl_setopt($curl, CURLOPT_URL, 'http://api.imgur.com/2/upload.xml');
curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $pvars);

$xml = curl_exec($curl);

curl_close ($curl);

var_dump($xml);
?>

I'm playing with the Imgur API, but it doesn't seems to work. PHP.net says that curl_init() is in PHP5, but my host says it isn't. How can I make this work?

Answer

Mark Stosberg picture Mark Stosberg · Nov 20, 2011

On old versions of Debian and Ubuntu, you solved this by installing the Curl extension for PHP, and restarting the webserver. Assuming the webserver is Apache 2:

sudo apt-get install php5-curl
sudo service apache2 restart

On newer versions, the package name as changed:

sudo apt install php-curl

It's possible you'll need to install more:

sudo apt-get install curl libcurl3 libcurl3-dev;