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?

0

11 Answers 11

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;
8
  • 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, 2012 at 17:11
  • I expect that sudo service apache2 restart should work just as well?
    – tshepang
    Aug 28, 2013 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. Aug 29, 2013 at 15:43
  • 1
    did the trick for me also with just sudo apt-get install php5-curl .. thanks!
    – mboy
    Sep 9, 2014 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, 2015 at 17:35
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

3
  • +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 9, 2010 at 11:54
  • 9
    That page doesn't say a word about HOW TO install the extension. Any better resources?
    – matteo
    Oct 21, 2014 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 3, 2020 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.

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.

4
  • 1
    The question was tagged as being about Ubuntu and Debian Linux. Your answer does not apply to those environments. Jan 5, 2016 at 16:09
  • 1
    @MarkStosberg Agreed. Updated answer.
    – Vinay
    Jan 6, 2016 at 5:31
  • Not working at all. I do not know whats wrong in this command, but not working. I am using Ubuntu 16.04
    – Banty Roy
    May 18, 2016 at 10:25
  • Yes worked! This should be the accepted answer! At least for linux anyways.
    – Zuks
    Jun 14, 2017 at 7:39
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
1
  • 2
    find your version: $php --version version =7.X then install: $ sudo apt-get install php7.X-curl Nov 22, 2018 at 19:21
4

do this

sudo apt-get install php-curl

and restart server

sudo service apache2 restart
1
  • 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, 2017 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.

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!

0
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

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

1
  • This is not an answer to the question. Jan 5, 2016 at 16:08

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.