By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,949 Members | 1,824 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,949 IT Pros & Developers. It's quick & easy.

transferring data between PHP files using CURL

P: 10
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
Share this Question
Share on Google+
13 Replies


Motoma
Expert 2.5K+
P: 3,235
Try hard coding values for your cURL call to see narrow down where the error is happening.
May 10 '07 #2

P: 93
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

P: 10
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

P: 93
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

P: 93
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

P: 10
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
Expert 2.5K+
P: 3,235
Would you PLEASE use the PHP tags?
May 14 '07 #8

P: 10
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
Expert 2.5K+
P: 3,235
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

P: 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
Expert 2.5K+
P: 3,235
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

P: 93
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

P: 10
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

Post your reply

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