How to convert a POST to a url? - php

Is possible to send data, that you normal have to use a submit button to send it, by using just the url?
Here is an example of code from a site with a button like that:
<form method="POST" action="Action.php?action=338&n=688&t=mine"><input type="hidden" id="Mine688" value="1" name="duree"><button value="submit" class="boutonsGeneral" type="submit" name="travail"><span title="" class="infoBoutonsGeneral"><span class="boutonsGeneral_separateur"> </span><span class="boutonsGeneral_gain" id="gainMine688"><img width="48" height="24" src="images/items/itemArgent.png">+2,18</span></span>Munca <span class="boutonsGeneral_duree boutonsGeneral_dureeMoins" id="dureeMine688">1 hour</span></button></form>
This is from Live HTTP Header when I click on that button:
ww.kraljevine.com/Action.php?action=338&n=688&t=mine
POST /Action.php?action=338&n=688&t=mine HTTP/1.1
Host: ww.kraljevine.com/
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: ro-ro,ro;q=0.8,en-us;q=0.6,en-gb;q=0.4,en;q=0.2
Accept-Encoding: gzip, deflate
Referer: ww.kraljevine.com/EcranPrincipal.php?l=8
Cookie: PHPSESSID=ec95ed7caf7c28f8a333; KC=account_name; KC2=1502cae30e04f5f55963e93; > > Glup=274; pageTamponVue=1
Connection: keep-alive
Content-Type: application/x-www-form-urlencoded
Content-Length: 22
duree=1&travail=submit
What I want to do is to send the post by url. Something like:
http://www.kraljevine.com/Action.php?action=338&n=688&t=mine (untill here is the link from site, and I want to add this) duree=1&travail=submit
Is not working http://www.kraljevine.com/Action.php?action=338&n=688&t=mine&duree=1&travail=submit and because of this I am wondering if is possible to do this.

Firstly you need to know what POST and GET are used for.
If you want to SEND data you should use POST
If you want to GET data depending on some params then use GET.
There is no sense in sending data with GET there are also some limitations to this. You can read more about limits here.
You can always use CURL to send POST data to the other URL.
Here is good tutorial how to do it and here goes the example based on this tutorial:
<?php
$url = 'http://www.kraljevine.com/Action.php?action=338&n=688&t=mine';
$fields = array(
'duree' => urlencode('1'),
'travail' => urlencode('submit')
);
//$fields can be extended with more params for POST submit always encode them with urlencode for these 2 examples of 1 submit it is not necessary but sometimes is really imporant to do it
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//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);
?>
The last thing is $_REQUEST which is An associative array that by default contains the contents of $_GET, $_POST and $_COOKIE. You can use it in php script and there will be no difference how the variable is passed for php script.
Use it wisely.

make the form method GET and put the variables in hidden fields in your form.
for example:
<form method="GET" action="Action.php">
<input type="hidden" name="action" value="338" />
<input type="hidden" name="n" value="688" />
<input type="hidden" name="t" value="mime" />
...more html/php...
</form>

The short answer is no.
By definition, GET variables are sent in the URL,
and POST variables are sent in the request's body.
So, you can't send your variables JUST by using the URL, as you requested.
You probably can physically catch a possible URL with parameters even in POST request, but I highly recommend you not the takes this approach.
You can read more about it in this post

In my case using spring boot.
I have converted from POST to GET like this :
BEFORE
in jsp( sender ):
in Java (reveiver):
I have converted to :
AFTER
in Jsp:
in Java:
and the result is match to what I want. I have changed only where the arrow is pointed in the picture, it saves my time a lot. All parameters will be composed into object "searchModel" easily.

Related

How to POST data from a textarea form?

Here is my current code:
<?php
$apikey='IGNORE-THIS-VARIABLE';
// All URLS to be sent are hold in an array for example
$urls=array('http://www.site1.com','http://www.site2.com/');
// build the POST query string and join the URLs array with | (single pipe)
$qstring='apikey='.$apikey.'&urls='.urlencode(implode('|',$urls));
// Do the API Request using CURL functions
$ch = curl_init();
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_URL,'http://www.example.com/api.php');
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_TIMEOUT,40);
curl_setopt($ch,CURLOPT_POSTFIELDS,$qstring);
curl_exec($ch);
curl_close($ch);
?>
My PHP code works, but here's the line I'm having issues with:
$urls=array('http://www.site1.com','http://www.site2.com/');
Basically I just want to have a text-area on my website where users can enter a list of URLs (one per line) and then it Posts it using the PHP code above.
My code works when I just have the URLs built into the code like you can see above, but I just can't figure out how to make it work with a text-area... Any help would be appreciated.
You can do it like below (on the same php page):-
<form method = "POST">
<textarea name="urls"></textarea><!-- add a lable that please enter new line separated urls -->
<input type = "submit">
</form>
<?php
if(isset($_POST['urls'])){
$apikey='IGNORE-THIS-VARIABLE';
// All URLS to be sent as new-line separated string and explode it to an array
$urls=explode('\n',$_POST['urls']); //Or $urls=explode('\\n',$_POST['urls']);
// if not worked then
//$urls=explode(PHP_EOL,$_POST['urls']);
// build the POST query string and join the URLs array with | (single pipe)
$qstring='apikey='.$apikey.'&urls='.urlencode(implode('|',$urls));
// Do the API Request using CURL functions
$ch = curl_init();
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_URL,'http://www.example.com/api.php');
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_TIMEOUT,40);
curl_setopt($ch,CURLOPT_POSTFIELDS,$qstring);
curl_exec($ch);
curl_close($ch);
}
?>
Note:- you can separate the form and logic on two different pages. that's seay i think.
You can use text-area and then you have to then explode by \n or if not work then explode by PHP_EOL to the initial url string and rest the code is same as above
You can pass to the form value as in bellow box is. any tag in HTML form have name attribute, assign name for any textarea, input or select tag.
<form method="post" name="first_form">
<input type="text" name="url" value="" />
<textarea name="note"></textarea>
<input type="submit" value="Post" />
</form>
In PHP you can the pass the value using two methods, $_GET or $_POST.
assign the tag name which you give for the HTML tag ($_POST['note'] or $_POST['url']) and also you can make it like an array $_POST.
<?php
if(isset($_POST)){
// display the posted values from the HTML Form
echo $_POST['note'];
echo $_POST['url'];
}
?>
Your Code:
$apikey='IGNORE-THIS-VARIABLE';
// All URLS to be sent are hold in an array for example
$urls=array('http://www.site1.com','http://www.site2.com/');
// build the POST query string and join the URLs array with | (single pipe)
$qstring='apikey='.$apikey.'&urls='.urlencode(implode('|',$urls));
// Do the API Request using CURL functions
$ch = curl_init();
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_URL,'http://www.example.com/api.php');
curl_setopt($ch,CURLOPT_HEADER,0);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_TIMEOUT,40);
curl_setopt($ch,CURLOPT_POSTFIELDS,$qstring);
curl_exec($ch);
curl_close($ch);

Using cURL to POST form to two places

Yesterday I asked a question about how I could go about hijacking my MailChimp contact form to send an additional email (when certain conditions are met).
I wrote changed the action="... from the MailChimp URL to my own process_email.php with code resembling the following:
extract($_POST);
$url = 'http://myMailchimpUser.us2.list-manage.com/subscribe/post';
$fields = array(
'u' => urlencode($u),
'id' => urlencode($id),
'group' => http_build_query ($group),
'MERGE1' => urlencode($MERGE1),
'MERGE2' => urlencode($MERGE2),
'MERGE3' => http_build_query($MERGE3),
'other_more_info_text' => urlencode($other_more_info_text),
'submit' => urlencode($submit)
);
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
$result = curl_exec($ch);
curl_close($ch);
I have yet to add the code which sends the email to myself, but that should be trivial. The problem I'm having with this code is that instead of redirecting me to the MailChimp page, its actually loading within process_email.php (and its loading incorrectly to boot.)
I know I can accomplish what I want to do if I used JavaScript, but it seems to me that isn't the proper way to go about this. Can anyone offer me any help?
If I understand correctly: you want to POST data locally first then let the form POST to Mailchimp. If that is what you are trying to do, then using some JS connected to the form (or a form button) is probably the best way to go. I think it is the proper way to go in your situation.
Something jQuery like below would work to POST the form locally first, and once that request is complete it would submit the form using the given action url (mailchimp).
$(document).ready(function(){
$('#submit-button').click(function(event){
event.preventDefault();
$.post("your_email_parser.php", $('#form-id').serialize(), function(){
$('#form-id').submit();
});
});
});
...
<form id='form-id' action='http://myMailchimpUser.us2.list-manage.com/subscribe/post' method='post'>
...
<input type='submit' id='submit-button' />
</form>

Using curl to submit/retrieve a forms results

I need help in trying to use curl to post data to a page and retrieve the results after the form has been submitted.
I created a simple form:
<form name="test" method="post" action="form.php">
<input type="text" name="name" size="40" />
<input type="text" name="comment" size="40" />
<input type="submit" value="submit" />
</form>
In addition, I have php code to handle this form in the same page. All it does is echo back the form values.
The curl that I have been using is this:
$h = curl_init();
curl_setopt($h, CURLOPT_URL, "path/to/form.php");
curl_setopt($h, CURLOPT_POST, true);
curl_setopt($h, CURLOPT_POSTFIELDS, array(
'name' => 'yes',
'comment' => 'no'
));
curl_setopt($h, CURLOPT_HEADER, false);
curl_setopt($h, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($h);
echo $result;
When I launch the page with the curl code in it, I get the form.php page contents but it doesn't not show
the variables that PHP should have echo'd when the form is submitted.
would appreciate any help with this.
Thanks.
AHHHH after your comment, the problem is clear.
you need to include one more POST variable in your array.
'submitted' => 'submitted'
the submit button also returns a post value if clicked, which you are checking in your form processing PHP.
if ($_POST['submitted']) {
in your curl code however you have left out the 'submitted' post variable.
if this is what you are looking for, please select the check mark next to this answer. thanks!
From reading the other two answers, and the comment by the OP, I have a couple of ideas.
Specifically, the OP Comment was:
Submitting the form manually does produce the right output. The PHP that handles the form is: if(isset($_POST['submitted'])){echo $_POST[name] ..... etc. and that's it
Under standard conditions, your basic form, as noted in the Original Question, would generate a $_POST array like the following:
array(
'name' => 'The Name as Entered in the Form' ,
'comment' => 'The Comment as Entered in the Form' ,
'submit' => 'submit' # From the "Submit" button
);
Your comment suggests that some aspect of your form handler is looking for a $_POST element called "submitted".
1) The basic form, as set out in the question, will always return FALSE for a check for $_POST['submitted'], and, as such, will trigger the ELSE action (if present) for that conditional.
2) Your CURL Action does not set either 'submit' or 'submitted', and, again, will always return FALSE for the conditional.
So, I would suggest that you:
1) Check your Form Handler, and see what fields are essential, what their names are and what their content should be.
2) Check your Basic Form, and ensure that it has the appropriate fields.
3) Check your CURL Action, and ensure that it details every field which is required. (Easy check would be to insert print_r( $_POST );die(); at the top of your Form Handler and submit the basic form. This will show you exactly what the form is sending, so you can recreate it in CURL.

Post and get at the same time in php

Do you have any suggestions with my problem. I need to use get and post at the same time.
Get because I need to output what the user has typed. And post because I need to access the mysql database in relation to that input.
It looks something like this:
<form name="x" method="get" action="x.php">
<input name="year" type="text">
<select name="general" id="general">
<font size="3">
<option value="YEAR">Year</option>
</form>
This will output the contents of mysql depending on what the user will check:
<form name="y" method="post" action"y.php">
<input name="fname" type="checkbox">
</form>
And the form action of those two combined will look something like this:
<?php
if($_POST['general'] == 'YEAR'){
?>
<?php echo $_GET["year"]; ?>
<?php
$result2 = mysql_query("SELECT * FROM student
WHERE student.YEAR='$syear'");
?>
<table border='1'>
<tr>
<?php if ( $ShowLastName ) { ?><th>LASTNAME</th><?php } ?>
<?php if ( $ShowFirstName ) { ?><th>FIRSTNAME</th><?php } ?>
</tr>
<?php while ( $row = mysql_fetch_array($result2) ) {
if (!$result2) {
}
?>
<tr>
<td><?php echo $row['IDNO']?> </td>
<td><?php echo $row['YEAR'] ?> </td>
<?php if ( $ShowLastName ) { echo('<td>'.$row['LASTNAME'].'</td>'); } ?></td>
<?php if ( $ShowFirstName ) { echo('<td>'.$row['FIRSTNAME'].'</td>'); } ?>
I really get lots of undefined errors when I do this. What can you recommend that I should do in order to get the value inputted by the user together with the mysql data.
You can only have one verb (POST, GET, PUT, ...) when doing an HTTP Request. However, you can do
<form name="y" method="post" action="y.php?foo=bar">
and then PHP will populate $_GET['foo'] as well, although the sent Request was POST'ed.
However, your problem seems to be much more that you are trying to send two forms at once, directed at two different scripts. That is impossible within one Request.
You cannot do a GET and POST at the same time.
Combine the two forms into one.
For example combine the forms to one 'post' form. In your code extract whatever you need from $_POST.
And 'YEAR' does not equal 'Year', your sample code also needs work.
As saig by the other answers, you can't do a get and a post request at the same time. But if you want to unify your PHP code in order to read a variable received through a get or post request, maybe you could use $_REQUEST
POST and GET (as HEAD, FILE, DELETE etc.) are HTTP methods. Your browser send an HTTP request to the server with one of them in front of the request so you cannot sent two method at the same time (an example of the request header from a web sniffer):
GET / HTTP/1.1[CRLF]
Host: web-sniffer.net[CRLF]
Connection: close[CRLF]
User-Agent: Web-sniffer/1.0.31 (+http://web-sniffer.net/)[CRLF]
Accept-Encoding: gzip[CRLF]
Accept-Charset: ISO-8859-1,UTF-8;q=0.7,*;q=0.7[CRLF]
Cache-Control: no[CRLF]
Accept-Language: de,en;q=0.7,en-us;q=0.3[CRLF]
Referer: http://web-sniffer.net/[CRLF]
The big difference from GET and POST is that GET retrieve a response from an url and POST send also some content data to that url. When you submit your form, data is collected in a standard format defined by enctype attribute and sent, also this time, to an url.
Also the url is formatted in a standard manner and the portion of string found behind the ? character is called QUERY STRING.
When the server receives data, it communicates these informations to PHP which reads the URL and reads the method, the body (data) of the request and a huge amount of other things. Finally it fills its superglobal arrays with this data to let you know what the user sends ($_SERVER, $_GET and $_POST and a bunch of others);
Important Notice! Also if PHP fill the $_GET superglobal with the query string of the URL and eventually the $_POST superglobal with the data found in the request body, $_POST and $_GET are not related to HTTP.
So, if you want fill $_POST and $_GET in the same time you must send your form in this manner:
<form method="post" action="http://myurl/index.php?mygetvar=1&mygetvar=2">
<input name="year" type="text" />
<imput type="submit" />
</form>
I haven't tested this but it's a possible solution for sending GET variables through a form's action value...
$request_uri = $_SERVER['REQUEST_URI'];
$request_uri = str_replace("&", "?", $request_uri);
$request_args = explode("?", $request_uri);
foreach($request_args as $key => $val) {
if(strpos($val, "=") > 0) {
$nvp_temp = explode("=", $val);
$_GET[$nvp_temp[0]] = $nvp_temp[1];
}
}
It's not completely fool proof, but I ran across an issue with a header("Location:") bug earlier that included get variables not being seen by the server under $_GET with a url such as http://website.com/page?msg=0 but they existed in the $_SERVER['REQUEST_URI'] variable. Hope this helps and good luck!
You can also use a hidden field in the form
<input type="hidden" id="whatever" name="foo" value="bar">
and use
$_POST['foo']
to retrieve the value.

Reading remote files using PHP

can anyone tell me how to read up to 3 remote files and compile the results in a query string which would now be sent to a page by the calling script, using headers.
Let me explain:
page1.php
$rs_1 = result from remote page a;
$rs_2 = result from remote page b;
$rs_3 = result from remote page c;
header("Location: page2.php?r1=".$rs_1."&r2=".$rs_2."&r3=".$rs_3)
You may be able to use file_get_contents, then make sure you urlencode the data when you construct the redirection url
$rs_1 =file_get_contents($urlA);
$rs_2 =file_get_contents($urlB);
$rs_3 =file_get_contents($urlC);
header("Location: page2.php?".
"r1=".urlencode($rs_1).
"&r2=".urlencode($rs_2).
"&r3=".urlencode($rs_3));
Also note this URL should be kept under 2000 characters.
Want to send more than 2000 chars?
If you want to utilize more data than 2000 characters would allow, you will need to POST it. One technique here would be to send some HTML back to the client with a form containing your data, and have javascript automatically submit it when the page loads.
The form could have a default button which says "Click here to continue..." which your JS would change to "please wait...". Thus users without javascript would drive it manually.
In other words, something like this:
<html>
<head>
<title>Please wait...</title>
<script>
function sendform()
{
document.getElementById('go').value="Please wait...";
document.getElementById('autoform').submit();
}
</script>
</head>
<body onload="sendform()">
<form id="autoform" method="POST" action="targetscript.php">
<input type="hidden" name="r1" value="htmlencoded r1data here">
<input type="hidden" name="r2" value="htmlencoded r2data here">
<input type="hidden" name="r3" value="htmlencoded r3data here">
<input type="submit" name="go" id="go" value="Click here to continue">
</form>
</body>
</html>
file_get_contents certainly helps, but for remote scripting CURL is better options.
This works well even if allow_url_include = On (in your php.ini)
$target = "Location: page2.php?";
$urls = array(1=>'url-1', 2=>'url-2', 3=>'url-3');
foreach($urls as $key=>$url) {
// get URL contents
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
$target .= "&r".$key."=".urlencode($output));
}
header("Location: ".$target);
You can use the file_get_contents function.
API documentation: http://no.php.net/manual/en/function.file-get-contents.php
$file_contents = file_get_contents("http://www.foo.com/bar.txt")
Do note that if the file contain multiple lines or very, very long lines you should contemplate using HTTP post instead of a long URL.

Categories