Page 1 of 1

How to post using PHP.

Posted: Fri Jul 08, 2011 11:45 pm
by tommytx
I searched for post and get to try to pull up some stuff on post but the search box kept telling me its too simple a word.. post and get.. so you cannot search for simple stuff....

So please can someone steer me to the best tutorial or help to do a post in php.. I have a file that i need to trap data before it goes to the final destination.. For example I am using a pretty normal form that is sending the data in post format.. not a problem there, but to trap the data I need to divert the normal post action to come to my php form where I will extract the data i need then i need to contine the form to its final posting spot..
So what I am doing is modifying the action to send the post data to another php form which will grab all the data and save it to a file... now I need to set up so the php will send the post on to the original action to complete the process...

So all I need to know to get this done is how to use php to reformat all the data into a POSt and send it on...

Re: How to post using PHP.

Posted: Sat Jul 09, 2011 12:08 am
by tommytx
Not sure it this would be helpful.. but here is some of what I have been trying..

Code: Select all

<?php
 $ch = curl_init('http://search.mydomain.com/idx/10159/userSignup.php');
 curl_setopt ($ch, CURLOPT_POST, 1);
 curl_setopt ($ch, CURLOPT_POSTFIELDS, "hint=newUser&action=add&phoneNumberRequired=n&passwordRequired=n");
 curl_exec ($ch);
 curl_close ($ch);
?>
There is actually about 25 pieces being sent and I just show a few here so you get the idea..
IF this looks ok and should be working.. maybe I need to make sure my curl is working.. I think it is but not sure how to verify.

Re: How to post using PHP.

Posted: Sat Jul 09, 2011 1:45 am
by tommytx
Here is what I have learned and its still kicking my butt..

Code: Select all

<?php  
//create array of data to be posted  
$post_data['hint'] = 'newUser';  
$post_data['action'] = 'add';  
$post_data['leadFirstName'] = 'Tom';  
$post_data['leadLastName'] = 'Chambers';  
$post_data['leadEmail'] = 'tommytx@earthlink.net';  
$post_data['submit'] = 'Submit';  

//traverse array and prepare data for posting (key1=value1)  
foreach ( $post_data as $key => $value) {  
    $post_items[] = $key . '=' . $value;
}  

echo "$post_items[0]<br>";
echo "$post_items[1]<br>";
echo "$post_items[2]<br>";   
echo "$post_items[3]<br>";

//create the final string to be posted using implode()  
$post_string = implode ('&', $post_items);  
echo "$post_string<br><br>";

//create cURL connection  
$curl_connection = curl_init('http://vahud.com/temp1/target.php');  
//set options  
curl_setopt($curl_connection, CURLOPT_CONNECTTIMEOUT, 30);  
curl_setopt($curl_connection, CURLOPT_USERAGENT,  
  "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");  
curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, true);  
curl_setopt($curl_connection, CURLOPT_SSL_VERIFYPEER, false);  
curl_setopt($curl_connection, CURLOPT_FOLLOWLOCATION, 1);  

//set data to be posted  
curl_setopt($curl_connection, CURLOPT_POSTFIELDS, $post_string);  

//perform our request  
$result = curl_exec($curl_connection);  
echo "Result = $result<br>";

//show information regarding the request  
print_r(curl_getinfo($curl_connection));  

echo "<br><br>*****************  Any error codes ****************************<br>";
echo curl_errno($curl_connection) . '-' .  
                curl_error($curl_connection);  
echo "<br>*****************  Any error codes ****************************<br>";
   
//close the connection  

curl_close($curl_connection);  
?> 
This really seems to work... but the data is not going to the target... only one item is transferring and that is action.

Here is the target file
**************************

Code: Select all

<?php
echo "<br><br>Made it to next file...<br>";
// extract($_POST);

$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$action = $_POST['action'];

echo "fname = $fname<br>";
echo "lname = $lname<br>";
echo "email = $email<br>";
echo "action = $action<br>";
?>
If anyone wants to help on this you can see it in action at http://vahud.com/temp1/php_post2.php
The only item actually coming thru is the action item... as you can see under results when you run it..
This is exactly what I need if I can get it to work...

Re: How to post using PHP.

Posted: Sat Jul 09, 2011 1:49 am
by tommytx
Result =
Made it to next file...
fname =
lname =
email =
action = add
when you click on the link above and look at the results, you can see that fname, lname, emial, are not transferring while action=add does come thru..

Re: How to post using PHP.

Posted: Sat Jul 09, 2011 1:00 pm
by McInfo
tommytx wrote:you can see that fname, lname, emial, are not transferring
Those names were not sent. Among the names target.php expected to capture, only 'action' was sent.

Code: Select all

# What was sent:
$post_data['hint']
$post_data['action']
$post_data['leadFirstName']
$post_data['leadLastName']
$post_data['leadEmail']
$post_data['submit']

# What was expected:
$_POST['fname']
$_POST['lname']
$_POST['email']
$_POST['action']