...

View Full Version : Using Post



markman641
02-01-2012, 02:18 AM
Alright.. this seems simple but I can't figure it out.

I made a <form> that uses POST into an external URL.. but how would i make it so that i can POST directly into the URL?

this is my system: When a user on my site completes an offer, it goes to my Postback URL and inserts it into my sql database.. then I want it to take those same values I used to insert into my SQL to POST it into an external website. I have all the values for the POST, I just need to know how to be able to post into that site automatically without the use of a button so it can just do everything automatically.

I am drawing a complete blank. Can i send a POST without a form?

BluePanther
02-01-2012, 02:43 AM
Alright.. this seems simple but I can't figure it out.

I made a <form> that uses POST into an external URL.. but how would i make it so that i can POST directly into the URL?

this is my system: it calls on a script to do a bunch of SQL tasks, then at the end of that it takes the values and uses POST into the URL... how would I make it automatically POST without having to use an ok/send/submit button?

I am drawing a complete blank. Can i send a POST without a form?

Wrong forum btw :thumbsup:

To answer, POST is just a method of passing values. Using a form is the most common method, but you can send POST data several other ways - via AJAX or PHP libraries (cURL as one). Which one you choose is up to you.

Sounds like AJAX is probably your best bet.

markman641
02-01-2012, 02:46 AM
Wrong forum btw :thumbsup:

To answer, POST is just a method of passing values. Using a form is the most common method, but you can send POST data several other ways - via AJAX or PHP libraries (cURL as one). Which one you choose is up to you.

Sounds like AJAX is probably your best bet.

I'm glad that made sense to you. I updated the OP to make a little more sense. Ill look up on google how to do AJAX. Could this be added to my postback script so it wont take it off the page?

BluePanther
02-01-2012, 03:09 AM
Now you've edited your post, cURL sounds like a better option. Tonnes of examples and info on the php manual.

markman641
02-01-2012, 03:20 AM
Now you've edited your post, cURL sounds like a better option. Tonnes of examples and info on the php manual.

I dont know cURL at all.. ill try my best tho.

BluePanther
02-01-2012, 03:26 AM
It's pretty simple - the PHP manual has plenty of discussion and examples on it.

markman641
02-01-2012, 03:29 AM
It's pretty simple - the PHP manual has plenty of discussion and examples on it.

what PHP manual?

markman641
02-01-2012, 03:37 AM
It's pretty simple - the PHP manual has plenty of discussion and examples on it.

What's wrong with this?


<?

//set POST variables
$url = 'http://www3.cbox.ws/box/index.php?boxid=3239282&boxtag=wcwcp8&sec=submit';
$message1 = 'testinggg';
$key = '1983ffec3c997fd3739fef1d4cc46f99';
$nme = 'MoneyBot';
$pic = 'http://cdn.techsling.com/wp-content/uploads/2011/02/windowssecuritycentre1.png';
$ekey = 'e6eba3f75fec9100f4e33dfe609c89ff';
$lvl = '4';
$fields = array(
'key'=>urlencode($key),
'nme'=>urlencode($nme),
'pic'=>urlencode($pic),
'ekey'=>urlencode($ekey),
'lvl'=>urlencode($lvl),
'pst'=>urlencode($message1),
);

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&amp;'; }
rtrim($fields_string,'&amp;');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

?>

BluePanther
02-01-2012, 03:47 AM
What's wrong with this?


<<?

//set POST variables
$url = 'http://www3.cbox.ws/box/index.php?boxid=3239282&boxtag=wcwcp8&sec=submit';
$message1 = 'testinggg';
$key = '1983ffec3c997fd3739fef1d4cc46f99';
$nme = 'MoneyBot';
$pic = 'http://cdn.techsling.com/wp-content/uploads/2011/02/windowssecuritycentre1.png';
$ekey = 'e6eba3f75fec9100f4e33dfe609c89ff';
$lvl = '4';
$fields = array(
'key'=>urlencode($key),
'nme'=>urlencode($nme),
'pic'=>urlencode($pic),
'ekey'=>urlencode($ekey),
'lvl'=>urlencode($lvl),
'pst'=>urlencode($message1),
);

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&amp;'; }
rtrim($fields_string,'&amp;');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

?>

What do you mean? Is it giving you any error? You should check for a false $result, then do something with curl_error($ch).

markman641
02-01-2012, 03:50 AM
What do you mean? Is it giving you any error? You should check for a false $result, then do something with curl_error($ch).

it just gives me a blank white page.

markman641
02-01-2012, 03:57 AM
here is the code of the FORM that i made..


<body onload="$('cbox').submit()">
<form name="cbox" target="cboxmain3-3239282" action="http://www3.cbox.ws/box/index.php?boxid=3239282&boxtag=wcwcp8&sec=submit" method="post" class="cfrm" onsubmit="return do_post();" _lpchecked="1">
<table border="0" cellpadding="0" cellspacing="0" width="100%" height="100%">
<tbody><tr>
<td align="right"><a href="index.php?boxid=3239282&amp;boxtag=wcwcp8&amp;sec=main" target="cboxmain3-3239282" onclick="return do_refresh();" id="rf">refresh</a>&nbsp;</td>
</tr><tr>
<td colspan="2" height="100%" id="tblmid" align="center" valign="center">
<input type="hidden" name="key" value="1983ffec3c997fd3739fef1d4cc46f99"> <input type="text" name="nme" value="MoneyBot">
<input type="hidden" name="eml" value=""> <input type="hidden" name="pic" value="http://cdn.techsling.com/wp-content/uploads/2011/02/windowssecuritycentre1.png"><input type="hidden" name="ekey" value="e6eba3f75fec9100f4e33dfe609c89ff">
<input type="hidden" name="lvl" value="4">
<input type="text" maxlength="500" name="pst" autocomplete="off" size="9" value="message" class="frmtb" onfocus="frmfocus(this, 'message')" onblur="frmblur(this, 'message')" style="width: 185px; "><input type="submit" value="Go" name="sub" class="frmbtn" style="height: 19px; width: 23px; "></td></tr>
<tr><td align="center" colspan="2">
<div align="right">
<div style="position:relative;width: 16px; height: 11px; overflow: hidden;float: left;cursor:pointer;" onclick="togglesnd()"><img src="snd2.png" id="sndctrl" width="48" height="11" style="position: relative; left: -32px; "></div>&nbsp;&nbsp;
<a href="JavaScript:pop('help', 320, 300, 1)">help</a>
</div></td></tr>
</tbody></table>
</form>
</body>

then i took those input values and put it in the cURL

BluePanther
02-01-2012, 04:17 AM
it just gives me a blank white page.

Did you check for a false $result, and then echo (or kill the script with die(curl_error($ch))) curl_error($ch)? That will, like mysql_error(), hold the error message from the cURL transaction if there was one - PHP will not report it implicitly.

markman641
02-01-2012, 04:25 AM
Did you check for a false $result, and then echo (or kill the script with die(curl_error($ch))) curl_error($ch)? That will, like mysql_error(), hold the error message from the cURL transaction if there was one - PHP will not report it implicitly.

Like I said i have no clue about any of this -.- I'm sorry i just dont understand.
It seems like it would be SO easy just to POST to an external URL with my values without a form.
could i just make it go to the form page, fill the values using get and have an autosubmit form?
i really dont know i cant think straight :P

BluePanther
02-01-2012, 05:08 AM
Like I said i have no clue about any of this -.- I'm sorry i just dont understand.
It seems like it would be SO easy just to POST to an external URL with my values without a form.
could i just make it go to the form page, fill the values using get and have an autosubmit form?
i really dont know i cant think straight :P

Lol, you just need to read what I'm saying properly :). I'm asking you to put some error reporting in, so we can see what's going wrong.


Rest of your code up to this point

//execute post
$result = curl_exec($ch);

// Do some error reporting. $result will be FALSE if curl_exec() failed for some reason
if(!$result){
echo 'cURL Error: '.curl_error($ch);
}

//close connection
curl_close($ch);

?>

markman641
02-01-2012, 05:13 AM
Lol, you just need to read what I'm saying properly :). I'm asking you to put some error reporting in, so we can see what's going wrong.


Rest of your code up to this point

//execute post
$result = curl_exec($ch);

// Do some error reporting. $result will be FALSE if curl_exec() failed for some reason
if(!$result){
echo 'cURL Error: '.curl_error($ch);
}

//close connection
curl_close($ch);

?>



Came up with a blank page again.

BluePanther
02-01-2012, 05:25 AM
Have you got PHP error reporting on? You won't get any output from this result unless you echo something. Echo something just before curl_close().

markman641
02-01-2012, 05:54 AM
Have you got PHP error reporting on? You won't get any output from this result unless you echo something. Echo something just before curl_close().

I don't think i have error reporting on nor do i know how.

kbluhm
02-01-2012, 02:51 PM
I don't think i have error reporting on nor do i know how.

http://www.lmgtfy.com/?q=php+error+reporting :thumbsup:

markman641
02-01-2012, 11:54 PM
I got the error:


Notice: Undefined variable: fields_string in /home/moneyeve/public_html/posttest.php on line 22



<?
ini_set('display_errors',1);
error_reporting(E_ALL);
//set POST variables
$url = 'http://www3.cbox.ws/box/index.php?boxid=3239282&boxtag=wcwcp8&sec=submit';
$message1 = 'testinggg';
$key = '1983ffec3c997fd3739fef1d4cc46f99';
$nme = 'MoneyBot';
$pic = 'http://cdn.techsling.com/wp-content/uploads/2011/02/windowssecuritycentre1.png';
$ekey = 'e6eba3f75fec9100f4e33dfe609c89ff';
$lvl = '4';
$fields = array(
'key'=>urlencode($key),
'nme'=>urlencode($nme),
'pic'=>urlencode($pic),
'ekey'=>urlencode($ekey),
'lvl'=>urlencode($lvl),
'pst'=>urlencode($message1),
);

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&amp;'; }
rtrim($fields_string,'&amp;');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string);

//execute post
$result = curl_exec($ch);

// Do some error reporting. $result will be FALSE if curl_exec() failed for some reason
if(!$result){
echo 'cURL Error: '.curl_error($ch);
}

//close connection
curl_close($ch);

?>

markman641
02-02-2012, 03:12 AM
UPDATE: it turns out it IS working although it is giving me that error.

Everyone that helped got thanks!
Thank you everyone SO much :)

BluePanther
02-02-2012, 09:46 AM
It's a notice, not an error. It's because the first mention of $fields_string is a concatenation of itself ($fields_string .= $key.'='.$value.'&amp;'). Really, $fields_string should be defined as an empty string before any concatenation occurs:


//url-ify the data for the POST
$fields_string = '';
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&amp;'; }
rtrim($fields_string,'&amp;');



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum