Search code examples
phplampp

PHP Fatal error: Call to undefined function curl_init()


I try PHP Post Request inside a POST Request thinking it might be useful for me. My code is given below:

$sub_req_url = "http://localhost/index1.php";

$ch = curl_init($sub_req_url);
$encoded = '';

// include GET as well as POST variables; your needs may vary.
foreach($_GET as $name => $value) {
  $encoded .= urlencode($name).'='.urlencode($value).'&';
}

foreach($_POST as $name => $value) {
  $encoded .= urlencode($name).'='.urlencode($value).'&';
}

// chop off last ampersand
$encoded = substr($encoded, 0, strlen($encoded)-1);

curl_setopt($ch, CURLOPT_POSTFIELDS,  $encoded);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_exec($ch);
curl_close($ch);

from the index.php file and index2.php is another file in the same directory and when I open the page I get the following error in my error.log file:

[Sat Dec 18 15:24:53 2010] [error] [client ::1] PHP Fatal error: Call to undefined function curl_init() in /var/www/testing1/index.php on line 5

What I want to do is to have a reservation form that send post request. Then I want to process post values and send again the post request to paypal.


Solution

  • You need to install CURL support for php.

    In Ubuntu you can install it via

    sudo apt-get install php5-curl
    

    If you're using apt-get then you won't need to edit any PHP configuration, but you will need to restart your Apache.

    sudo /etc/init.d/apache2 restart
    

    If you're still getting issues, then try and use phpinfo() to make sure that CURL is listed as installed. (If it isn't, then you may need to open another question, asking why your packages aren't installing.)

    There is an installation manual in the PHP CURL documentation.