I’m trying to send some value from ‘1.php’ page to ‘3.php’ page by using ‘curl’.
Heres my code-
1.php
<?php
$data = array();
$data['first_name'] = 'hello';
$data['last_name'] = 'Thind';
$data['password'] = 'secret';
$data['email'] = 'me@abc.com';
$post_str ='';
foreach($data as $key=>$val)
{ $post_str .= $key.'='.$val.'&'; }
$post_str = substr($post_str, 0, -1);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, '3.php' );
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_str);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
curl_close($ch);
echo $result;
?>
3.php
<?php
$host="localhost";
$dbname="mr";
// mysql code for table= create table m(name varchar(10));
$username="";
$password="";
$con=mysql_connect("$host","$username","$password")or die("cannot connect");
mysql_select_db($dbname) or DIE('cannot select db');
if(isset($_POST['first_name'])){
$name=$_POST['first_name'];
$sql="insert into m
values('$name')";
$sq=mysql_query($sql);
echo"ok";
}
mysql_close($con);
?>
My problem is “No data is inserted into database cause no data is transfered to 3.php from 1.php” .I checked database code in ‘3.php’ by giving some value and that was inserted correctly into database.So code in ‘3.php’ works well.So it seems the code in ‘1.php’ not working.
(Note: php_curl.dll is activated in php.ini file. I’m using windows xp professional service pack-2, xampp-win32 version-1.8.1)
Can anybody please tell me whats wrong there ?
-Thanks.
curl_setopt($ch, CURLOPT_URL, '3.php' );This requests the URL
3.php…You must give it some “hints” as cURL is an external program that will simply call the URL as is.
Change it to something like
http://mysite.whatever/3.phpOn a side note, cURL is a command-line utility and it’s way easier to debug if used that way. You should first test-it command line and then, if it’s workign, implement it on your PHP code.
Mode on cURL here: http://curl.haxx.se/docs/manpage.html