473,404 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

transferring data between PHP files using CURL

hi, iam just a beginner with php and curl.i search a lot but not able to find out the find out the problem i am gating.
I am first sending data to a php a file then using CURL on the php file iam sending data to another php file.but its not working.PLZ help me .

THIS ism y html form by this i am posint some data to a php file named curlpostv.php

curlpostv.html
Expand|Select|Wrap|Line Numbers
  1.  
  2. <html> 
  3. <body>
  4. <form name="f1" method="post" action="curlpostv.php">
  5. Name:<input type="textbox" name="name"><br>
  6. Sex: MALE<input type="radio" name=r1 value="MALE">
  7. FEMALE<input type="radio" name=r1 value="FEMALE"> <br>
  8. MBA <input type="checkbox" name=c1 value="MBA"> 
  9. MCA <input type="checkbox" name=c2 value="MCA"> <br>
  10. <input type="submit" value="click"> 
  11.  
  12. </form>
  13. </body>
  14. </html>
  15.  
curlpostv.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $name=$_POST["name"];
  3. $sex=$_POST["r1"];
  4.  
  5. if(isset($_POST["c1"])==true && isset($_POST["c2"])==true)
  6. {
  7. $qua=$_POST["c1"]+$_POST["c2"];
  8. }
  9. elseif(isset($_POST["c1"])==true)
  10. $qua=$_POST["c1"];
  11. elseif(isset($_POST["c2"])==true)
  12. {
  13. $qua=$_POST["c2"];
  14.  
  15. }
  16.  
  17. //echo $name.$sex,$qua;
  18.  
  19. $ch=curl_init();
  20. curl_setopt($ch,CURLOPT_URL,"http://localhost/curl/curlpostval/cgrabval.php");
  21. curl_setopt($ch,CURLOPT_POST,1);
  22. curl_setopt($ch,CURL_POSTFIELDS,'N=$name&S=$sex&Q=$qua');
  23.  
  24. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  25. curl_exec($ch);
  26. echo curl_error($ch);
  27. curl_close($ch);
  28. ?>
  29.  

cgrabval.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. echo "You are".$_POST["N"];
  3. echo $_POST["S"];
  4. echo $_POST["Q"];
  5. ?>
  6.  

i should see the outputs here but nothing shown

i am new to this community so no idea how i be informed if someone post a reply
emailremoved@noemailsplease.com this is my email address plz help me or send a offline msg
May 10 '07 #1
13 3842
Motoma
3,237 Expert 2GB
Try hard coding values for your cURL call to see narrow down where the error is happening.
May 10 '07 #2
This is not causing a problem, but I think just:
$qua=$_POST["c1"]+$_POST["c2"];
would do the same job as all your: if(isset($_POST....

I know it's commented out, but the line:
//echo $name.$sex,$qua;
has got a comma in it. Surely that will cause an error?

And now to get to the point!
Your problem might be that you need to urlencode() your form data.
When you send form data using cURL you need to replace any characters that are not letters or number with their 'special url' equivilents.
Your web browser does this for you, so if you typed
"Joe Bloggs" into the 'name' field, your web browser actually sends
"name=Joe+Bloggs". cURL does not do this for you, so you need to use the
urlencode() function to do this for you;

$name = urlencode($_POST['name']);

You don't need to do it for the others because they are radio buttons/checkboxes and they don't have any non-alpha-numeric characters in the values.

Try that and see if it works
May 10 '07 #3
It still not working
i use the urlencode still not working
Another thing u said i donot have to use ulrencode() for radio button/checkbox so atleast i should see those value.

curlpostval.php

<?php

header('Content-Type: text/html');
$name=urlencode($_POST["name"]);
$sex=$_POST["r1"];

if(isset($_POST["c1"])==true && isset($_POST["c2"])==true)
{
$qua=$_POST["c1"]+$_POST["c2"];
}
elseif(isset($_POST["c1"])==true)
{
$qua=$_POST["c1"];
}
elseif(isset($_POST["c2"])==true)
{
$qua=$_POST["c2"];

}
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,"http://localhost/curl/curlpostval/cgrabval.php");
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURL_POSTFIELDS,'N=$name&S=$sex&Q= $qua');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_exec($ch);
echo curl_error($ch);
curl_close($ch);
?>



cgrabval.php

<?php
echo "You are".$_POST["N"];
echo $_POST["S"];
echo $_POST["Q"];
?>
May 11 '07 #4
Ah. Try this:
In this line:
curl_setopt($ch,CURL_POSTFIELDS,'N=$name&S=$sex&Q= $qua');
You've got the variables ($name,$sex,$qua) inside single quotation marks. So php wont put the values of $name,$sex etc into the post fields it will literally put in "$name" and "$sex". I'm surprised that when you print out the information in
cgrabval.php it doesn't print "You are$name". Try chaging your code to this:
curl_setopt($ch,CURL_POSTFIELDS,'N='. $name .'&S='. $sex .'&Q='. $qua);
See what that does.


I don't think you need the line;
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
You would only need that if cgrabval.php was going to redirect you to somewhere else.

Actually, no I'm not surprised that cgrabval.php doesn't print anything out because if you were to urlencode "$" it would get turned into "%24", so having $ in your post field is confusing the hell out of it!
May 13 '07 #5
Another thought: are you sure that cgrabval.php is successfully being called by cURL? I tried doing:
curl_setopt($ch,CURLOPT_URL,"http://localhost/myfile.php");
a couple of days ago and I couldn't get it to work. Not sure which operating system you're on, I'm on Max OS X (10.4.8)

Change curlpostval.php to just this:
[PHP]$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,"http://localhost/curl/curlpostval/cgrabval.php");
curl_setopt($ch,CURLOPT_HTTPGET,1);
curl_exec($ch);
echo curl_error($ch);
curl_close($ch);[/PHP]

And change cgrabval.php to just;
[PHP]echo "this is cgrabval.php";[/PHP]
That way you've taken the $_POST stuff out of the equation completely. When you're sure that cURL is successfully connecting to cgrabval.php, then try putting the $_POST stuff back.
May 13 '07 #6
Thanks for the response again but its not working again.
Is this a php bug?
here is the output what iam gating

Cgrabval.php called
You are

i am using wamp5 in xpsp2 .
i havenot test it to linux yet but as other curl programming running successfully it should work here.
my other program of fatching a page by curl working successfully
Just this posting value not working.

this is what make change as u suggested
curlpostval.php

<?php

header('Content-Type: text/html');
$name=$_POST["name"];
$name=urlencode($name);
$sex=$_POST["r1"];

if(isset($_POST["c1"])==true && isset($_POST["c2"])==true)
{
$qua=$_POST["c1"]+$_POST["c2"];
}
elseif(isset($_POST["c1"])==true)
{
$qua=$_POST["c1"];
}
elseif(isset($_POST["c2"])==true)
{
$qua=$_POST["c2"];

}



$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,"http://localhost/curl/curlpostval/cgrabval.php");
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURL_POSTFIELDS,'N='.$name.'&S='.$ sex.'&Q='.$qua);
curl_exec($ch);
echo curl_error($ch);
curl_close($ch);
?>

cgrabval.php

<?php
echo "Cgrabval.php called"."<br>";
echo "You are".$_POST['N'];
echo $_POST['S'];
echo $_POST['Q'];
?>

curlpostv.html
<html>
<body>
<form name="f1" method="post" action="curlpostv.php">
Name:<input type="textbox" name="name"><br>
Sex: MALE<input type="radio" name=r1 value="MALE">
FEMALE<input type="radio" name=r1 value="FEMALE"> <br>
MBA <input type="checkbox" name=c1 value="MBA">
MCA <input type="checkbox" name=c2 value="MCA"> <br>
<input type="submit" value="click">

</form>
</body>
</html>
May 13 '07 #7
Motoma
3,237 Expert 2GB
Would you PLEASE use the PHP tags?
May 14 '07 #8
very sorry but
i am using php tags
<?php

?>

what u mean by tag.i am very new to php and this kind of community so no idea.
May 14 '07 #9
Motoma
3,237 Expert 2GB
If you read through the Posting Guidelines, it states quite clearly that when you post code to the forum, you are to use appropriate code tags, to enable color, syntax highlighting, and proper tab spacing. You can do this in the PHP forum by placing [PHP ] [/PHP] around your code, or by utilizing the PHP button at the top of the text entry field.

<?php
$phrase = "This is what inappropriately tagged PHP code looks like";
print $phrase;
?>

[PHP]
<?php
$phrase = "This is what appropriately tagged PHP code looks like";
print $phrase;
?>
[/PHP]


Doing this will help us without webserver nearby debug your code.
May 14 '07 #10
i got the solution it was a silly mistake

it should be
[PHP]
curl_setopt($ch,CURLOPT_POSTFIELDS,'N='.$name.'&S= '.$sex.'&Q='.$qua);
[/PHP]
instade of
[PHP]
curl_setopt($ch,CURL_POSTFIELDS,'N='.$name.'&S='.$ sex.'&Q='.$qua);
[/PHP]

Thanks for the support
May 14 '07 #11
Motoma
3,237 Expert 2GB
Good eye! I looked over that line at least 5 times and did not see it.

Thanks for posting the solution back to the forum!
May 14 '07 #12
I had to read that at least 5 times before I saw the mistake too!!

Have you considered that the line
[PHP]$qua=$_POST["c1"]+$_POST["c2"];[/PHP]
will add the value of $_POST[c1'] to the value of $_POST[c2']. I.e. it will convert "MBA" to a number and then convert "MCA" to a number and then add them together. The result being something fairly meaningless (probably 0). If you want to stick them together so that you get "MBAMCA" you need to do:
[PHP]$qua=$_POST["c1"] . $_POST["c2"];[/PHP]
I'm not entirely sure what MBA or MCA means, or what you're trying to do with them, but my guess is that you're not trying to add them together numerically.
May 15 '07 #13
Thanks admalton for correcting that , but surprisingly it does not show me 0 if i select both the box, any the right thing should follow always as u directed.
Those MBA and MCA are meant by master degree in computer application and business administration.

Thanks to all. I shall soon post another problem.
May 23 '07 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Peter Valdemar M?rch | last post by:
Hi, In short, how to modify selected tags/sections of a HTML file, using PHP as the "modifier"/filter? I would have thought this was a very common usage for PHP... I have a set of existing...
6
by: benji | last post by:
I have set up a system to download datafeeds in pain text or zipped. The download part of this system uses the curl extension to download the files. All was well when I tested it with various...
2
by: jimb | last post by:
I need some advice on how to securely transfer data between two servers. Here is the situation. We have two sql servers that hold student data. I have full access to my sql server, but only write...
2
by: Tommaso Caldarola | last post by:
I'm using a Remoting to manage small set of data, now the customer wants to transfer big binary files (up to 10 Gb) between 2 computers. It's better to use socket or other technology or I can...
5
by: meetalps | last post by:
Hi All, Can you please help me with a step by step procedure to transfer files from my PC to DB2-AIX and vice versa. I am new to both. Example transferring a sql file to run from PC to DB2 on AIX....
0
by: Raj | last post by:
Hi, I am newbie to programming with libcurl. the problem is i want to send my gmail username and password outside the browser and get access to my mail. this i am doing with LibCurl. Though i dont...
0
by: Raj | last post by:
Hi, I am newbie to programming with libcurl. the problem is i want to send my gmail username and password outside the browser and get access to my mail. this i am doing with LibCurl. Though i dont...
2
by: Norman Peelman | last post by:
From: "Norman Peelman" <npeelman@cfl.rr.com> Subject: Re: posting form data to two php scripts at once Date: Sunday, December 17, 2006 12:06 AM <one.1more@gmail.comwrote in message...
2
by: cbs81 | last post by:
hi all, problem: transferring data from workbooks that are stored in a particular directory from excel to a table in access. when done, the workbooks that have been processed are automatically...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.