Using cURL to POST form to two places - php

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>

Related

php page gives blank page or Null Value [duplicate]

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 5 years ago.
I have an html forum with 3 fields Naam Achternaam Email
After the user fills in the forum and clicks submit, it wil call a php script called action_page.php.
Taht script should be posting back to the screen the given information form the html forum.
At this point it only gives back 3 NULL values
Naam:
Achternaam:
Email:
This is the html code
how can i get the PHP script to take the filled in fields from the html page and show then on screen.
<?php
// what post fields?
$fields = array(
'Naam' => $Naam,
'Achternaam' => $Achternaam,
'Email' => $Email,
);
// build the urlencoded data
$postvars = http_build_query($fields);
// 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, $postvars);
// execute post
$result = curl_exec($ch);
// close connection
curl_close($ch);
?>
This is the php code
The html page has 3 fields
<html>
<body>
<form action="/action_page.php" method="post">
Naam: <input type="text"name="Namm"><br>
Achternaam: <input type="text" name="Achternaam"><br>
Email: <input type="text" name="Email"><br>
<td><input type='submit' name='post' value='post'></td>
<td><input type='reset' name='Rest' value='Reset'></td>
</form>
</body>
</html>
but after i click op sumbit the action_page returns a blank page or for each filled field it echo NULL on the screen.
What i am trying to make is an php page that prints the 3 filled field from the html page that should post them to the php page so that can print them to the screen.
am i dooing something wrong here or mistyped in the code some where?
Not sure if this is what you are looking for, but to get posted values, you'll use the $_POST superglobal
$fields = array(
'Naam' => $_POST["Naam"],
'Achternaam' => $_POST["Achternaam"],
'Email' => $_POST["Email"],
);

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);

How to convert a POST to a url?

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.

POSTing variables to an external page and reading it

Say we have page 1:
<?php
$text=$_POST['text'];
echo 'You wrote "' . $text . '".';
?>
and page 2, which takes the user input, POSTs it to Page1, and gets the page (You wrote (input).). I'm not really sure about how to do this: googling (or better, stackoverflowing) a while, I found this question, which explains how to POST a variable to another page, but not how to get the code of the page. So, how to do both?
EDIT: An example of the actual situation.
The user inputs the number 245. I get the number, pass it to this external page, get the page, retrieve the result (5*7*7) and show it.
In italics you see the part I need.
I'd suggest using curl to POST the data, and then you'll probably need to parse the returned HTML.
Here's a blog post about how to use cURL to post. I'll leave using google to find an example of parsing HTML with PHP as an exercise for the reader . . .
page 1:
<form method="post" action="page2">
<input name="input" type="text"/>
<input type="submit" value="sendtopage2"/>
</form>
page 2:
<?php
$text=$_POST['input'];
echo 'You wrote "' . $text . '".';
?>
Working example with cURL:
$function="sin(x)";
$var="x";
$url='http://www.numberempire.com/integralcalculator.php';
echo '<base href="' . $url . '">'; // Fixes broken relative links
$Curl_Session = curl_init($url);
curl_setopt ($Curl_Session, CURLOPT_POST, 1);
curl_setopt ($Curl_Session, CURLOPT_POSTFIELDS, "function=$function&var=$var");
curl_setopt ($Curl_Session, CURLOPT_FOLLOWLOCATION, 0);
$content=curl_exec($Curl_Session);
curl_close ($Curl_Session);

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.

Categories