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

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
Thew
  • 15,789
  • 18
  • 59
  • 100

11 Answers11

131

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;
Mark Stosberg
  • 12,961
  • 6
  • 44
  • 49
  • 1
    I had to install more in ubuntu: "sudo apt-get install curl libcurl3 libcurl3-dev php5-curl; You'll know if it works because phpinfo() will get a new section with Curl info." – Kzqai Apr 25 '12 at 17:11
  • I expect that `sudo service apache2 restart` should work just as well? – tshepang Aug 28 '13 at 20:21
  • It's logical to assume that restart = stop + start, but with years of experience with Apache, it hasn't always in the case. I learned to use an explicit stop and then start to be safe. Sorry, I can't remember the specific potential problems, but you are welcome to use `restart` and see if it works for you. – Mark Stosberg Aug 29 '13 at 15:43
  • 1
    did the trick for me also with just sudo apt-get install php5-curl .. thanks! – mboy Sep 09 '14 at 11:48
  • `sudo service apache2 restart` merely restarts Apache. It is necessary to run this [following the CURL install] in order for Aache/PHP to pick up the added CURL packages, so they will work when the CURL infused PHP script is next run. But, I also found that I needed to include `php5-curl` to get satisfaction ;) – ReverseEMF Jun 21 '15 at 17:35
  • php5-curl Unable to locate packages after installing `curl libcurl3 libcurl3-dev`. what i am missing ? – captainchhala Dec 22 '17 at 11:43
  • @captainchhala Time has marched on since I answered this question 6 years ago. Are you sure you still need to installing PHP 5 and not PHP 7? – Mark Stosberg Dec 23 '17 at 01:12
  • @MarkStosberg i have solved the probelm with `sudo apt install php-curl` and by the way i didn't look at the timestamp :). – captainchhala Dec 23 '17 at 05:23
42

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

fire
  • 21,383
  • 17
  • 79
  • 114
  • +1 And if you can't install the curl extension, you can try using shell_exec (or similar) with the curl command. Or you can probably "fake it" with fsockopen. – bogeymin Dec 09 '10 at 11:54
  • 9
    That page doesn't say a word about HOW TO install the extension. Any better resources? – matteo Oct 21 '14 at 12:01
  • Once you've installed the curl package, have a look in the php.ini file (within /etc). Search for curl. In mine (OpenBSD), I had to remove the comment on the line that said *extension=curl* – moo Apr 03 '20 at 13:30
14

Don't have enough reputation to comment yet. Using Ubuntu and a simple:

sudo apt-get install php5-curl
sudo /etc/init.d/apache2 restart

Did NOT work for me.

For some reason curl.so was installed in a location not picked up by default. I checked the extension_dir in my php.ini and copied over the curl.so to my extension_dir

cp /usr/lib/php5/20090626/curl.so  /usr/local/lib/php/extensions/no-debug-non-zts-20090626

Hope this helps someone, adjust your path locations accordingly.

Michael
  • 3,568
  • 3
  • 37
  • 50
13

For Ubuntu

Install Curl extension for PHP & restart apache server.

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

For Windows

Problem arises because of not including the lib_curl.dll to PHP. also load following extension if not working,so those extension in php.ini or remove comment if already exist in php.ini file then remove comment.

extension=php_bz2.dll
extension=php_curl.dll
extension=php_dba.dll

Now restart apache server. If you get an error "Unable to Load php_curl.dll", copy SSLEAY32.PHP, libEAY32.dll (OpenSSL) Libraries to the System32 folder.

Vinay
  • 2,564
  • 4
  • 26
  • 35
5

for php 7.0 on ubuntu use

sudo apt-get install php7.0-curl

And finally,

sudo service apache2 restart 

or

sudo service nginx restart
Ewomazino Ukah
  • 2,286
  • 4
  • 24
  • 40
4

do this

sudo apt-get install php-curl

and restart server

sudo service apache2 restart
Benjamin Gakami
  • 1,610
  • 2
  • 16
  • 22
  • Thanks man.. was working in a competetion and I am not comfortable php, curl. Your answer helped me overcome a problem, that took me half a day. – jegadeesh Dec 21 '17 at 17:36
3

Experienced this on ubuntu 16.04 running PHP 7.1.14. To resolve,first put this in a script and execute. It will return true if curl is installed and enabled,otherwise, it will return false.

<?php
    var_dump(extension_loaded('curl'));
?>

If it returns false, run

sudo apt-get install php7.1-curl

And finally,

sudo service apache2 restart

After the restart, the script will return true and hopefully, your error should be resolved.

The Sammie
  • 1,218
  • 15
  • 25
2

There is solution with all necessary details for Windows 7 x64:

http://www.youtube.com/watch?v=7qNTi1sEfE8

It is in French, but you can understand everything! I solved same problem, even don't speak French. :-)

Many answers forget to mention that you need to add new version of php_curl.dll file from this location: http://www.anindya.com/php-5-4-3-and-php-5-3-13-x64-64-bit-for-windows/

I added new version of php_curl.dll from archive php_curl-5.4.3-VC9-x64.zip to folders: C:\wamp\bin\php\php5.4.3\ext and C:\Windows\System32 and everything was fine!

Nemanja Vujacic
  • 925
  • 5
  • 14
  • 25
0

For Windows this worked for me. Go to php.ini file and remove the comment from the following lines

extension=bz2
extension=curl

Just remove the ; before each line to uncomment. Restart the server and this error will go away

Omair Munir
  • 135
  • 8
-2

This is from the official website. php.net

After installation of PHP.

Windows

Move to Windows\system32 folder: libssh2.dll, php_curl.dll, ssleay32.dll, libeay32.dll

Linux

Move to Apache24\bin folder libssh2.dll

Then uncomment extension=php_curl.dll in php.ini

-3
$ch = curl_init('http://api.imgur.com/2/upload.xml'); //initialising our url

curl_setopt($ch, CURLOPT_MUTE, 1);

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);  //used for https headers 

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);  //used for https headers

curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, urlencode($pvars));  
//the value we post

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //waiting for reponse, default value in php is zero ie, curls normally do not wait for a response

curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0'); 

$output = curl_exec($ch); //the real execution 
curl_close($ch);

echo $output;

Try this code. I am not sure about it. But i used it to send xml data to a remote url.

Marek Grzenkowicz
  • 17,024
  • 9
  • 81
  • 111
praveenmon
  • 141
  • 2
  • 15