Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

curl_init() function not working

Tags:

php

lampp

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.

like image 588
Santosh Linkha Avatar asked Dec 18 '10 09:12

Santosh Linkha


People also ask

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

What is the use of Curl_init?

cURL is a PHP library and command-line tool (similar to wget) that allows you to send and receive files over HTTP and FTP. You can use proxies, pass data over SSL connections, set cookies, and even get files that are protected by a login.


2 Answers

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.

like image 150
RusAlex Avatar answered Oct 08 '22 21:10

RusAlex


For Windows, if anybody is interested, uncomment the following line (by removing the ;) from php.ini

;extension=php_curl.dll

Restart apache server.

like image 26
Kanda Avatar answered Oct 08 '22 21:10

Kanda