Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fatal error: Call to undefined function curl_init()

<?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?

like image 518
Thew Avatar asked Dec 09 '10 09:12

Thew


People also ask

What is Curl_init?

The curl_init() function will initialize a new session and return a cURL handle. curl_exec($ch) function should be called after initialize a cURL session and all the options for the session are set. Its purpose is simply to execute the predefined CURL session (given by ch).

How check CURL enabled or not in PHP?

php // Script to test if the CURL extension is installed on this server // Define function to test function _is_curl_installed() { if (in_array ('curl', get_loaded_extensions())) { return true; } else { return false; } } // Ouput text to user based on test if (_is_curl_installed()) { echo "cURL is <span style=\"color: ...


2 Answers

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; 
like image 94
Mark Stosberg Avatar answered Sep 28 '22 10:09

Mark Stosberg


curl is an extension that needs to be installed, it's got nothing to do with the PHP version.

http://www.php.net/manual/en/curl.setup.php

like image 44
fire Avatar answered Sep 28 '22 10:09

fire