-1

I have installed curl and am trying to use it in a PHP script but I can't seem to get it to work.

I created the following php file

<!DOCTYPE html>
<html>
<body>
    <?php
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "www.google.com")
    curl_exec($ch);
    curl_close($ch);
    ?>
</body>
</html>

and when I try to execute in through the command line and view the page in my browser, I get "Fatal error: Uncaught Error: Call to undefined function curl_init() in 'Location of file'".

However I can run curl perfectly fine in the command line by navigating to the its location and entering commands directly into the command line.

dfewfwef
  • 1
  • 2

2 Answers2

1

You should install and enable curl extension.

For windows

Browse and open these

C:\Program Files\xampp\apache\bin\php.ini
C:\Program Files\xampp\php\php.ini
C:\Program Files\xampp\php\php4\php.ini

Uncomment the following line in your php.ini

;extension=php_curl.dll

Like something below-

extension=php_curl.dll

Restart your Apache server. Check your phpinfo() and Enjoy using curl() library.

For Linux

Ubuntu Open your terminal and execute the followinf command

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

Redhat, CentOS

yum install curl
service httpd restart

Hope you will enjoy cURL library now.

  • What should I do if I'm not using an Apache server? I run the command php -S localhost:8000 and go to localhost:8000 in my browser to view the site. – dfewfwef Jan 23 '17 at 10:50
0

Your server doesn't have cURL enabled / installed.

To enable it, you might want to take a look at these answers on similar questions:

Community
  • 1
  • 1
Wayne Whitty
  • 18,505
  • 4
  • 38
  • 64
  • The thing is that I've already done that. I can run curl perfectly fine in the command line. It's just that it doesn't recognize it in the script that I'm trying to use. – dfewfwef Jan 23 '17 at 10:41
  • When you say command line, what do you mean? Linux command line? PHP Cli? – Wayne Whitty Jan 23 '17 at 16:06