How to get page body after POST request - php

I'm trying to make a PHP page that lets you upload a document to telegram servers and than retrives a file_id which is stored in the json of the redirect page. After some tutorials on YT this is the code:
<?php
$botToken = "1099xxxxxx:AxxxE-g9qDI2Uxxxxxxxxxxxxxxxxxxxxxxxx";
$website = "https://api.telegram.org/bot".$botToken;
?>
<form action="<?php echo $website.'/sendPhoto' ?>" method="post" enctype="multipart/form-data">
<input type="text" name="chat_id" value="mychatid">
<input type="file" name="photo">
<input type="submit" value="send">
</form>
Redirect after submit
The goal is to retrive the file_id of the file and save it in a variable, possibly without opening another tab.
Thanks!

You've making the request to the Telegram API from the browser by submitting a form directly to it.
There is no way your PHP can get any information from that API that way.
As a side effect, you are giving your token (which should be kept secret) to every visitor who uses the form.
You need the browser to submit the form to a PHP program you control and then make the request to the Telegram API from your PHP and not from the browser.
The usual way to do this is with the cURL library.

Related

Cab PHP create an Input field?

I'm trying to create a cookie for a web page. The cookie value will vary based on the users name. Does PHP have an input type function? I just want to add an input field to the page an then the PHP will use that to define the users name for the page. I have the create cookie code, just can't figure out how to get the name from the screen and insert it to the cookie code. Appreciate any suggestions. This is on a WP website.
Not natively because php does not execute in browser, it executes on your server, but it can be used to write an HTML input.
The syntax would look something like this:
echo '<input type="text" name="myinput">';
or
?>
<input type="text" name="myinput">
<?php
You would then use a form post, CURL, or AJAX function to send the data back to the server where a second PHP script would process the input.
That said, it would help to post your create cookie code, since you may not even need to send it back to the server, but just handle it all in the browser using Javascript in which case your submit button only needs to pass the input to a Javascript function instead of posting it.
Is this something you are looking for?
Here it just takes the value user input from the browser and set it as a cookie
<?php
if(isset($_POST['name']) && !empty($_POST['name'])){
setcookie('setcookie_name',$_POST['name']); // setting cookie
}
?>
<form action="" method="post">
<input name="name" value="" placeholder="Enter your name" />
<input name="submit" type="submit" value="Submit"/>
</form>

Get JSON response from iframe and save in database

I have a form with file upload option. The action of the form is a 3rd party domain. I am uploading the file to another server. After upload success, I get a json response. How can I use those json response to save it in my database?
I don't have any control over the server i am uploading the file. I am just using a 3rd party service. they provide flash uploader. But i am working on a fallback(for apple handheld devices). If no flash, then i am using an iframe in which i am putting the form. this is my sample code:
The main file
<html>
<body>
<iframe src="non-flash-upload.php"></iframe>
</body>
</html>
non-flash-upload.php
<form action="some-3rd-party-domain" enctype="multipart/formdata">
<input type="file" name="userfile" />
<input type="submit">
</form>
When i submit this form, i get some json response with the information of uploaded file. these i want to save in my database. Is this possible? I tried with jquery to fetch them. but the iframe is going to some external domain and there it is printing the json response.
thanks a lot.

Making an HTTP Request in HTML Form Resolution

I'm trying to make a registration page for an Openfire XMPP server. The easiest route seems to be to use the user service plugin to register accounts, which lets you register users with HTTP requests.
Essentially, I need to make HTTP requests like
http://hostname:9090/plugins/userService/userservice?type=add&secret=passcode&username=kafka&password=drowssap&name=franz&email=franz#kafka.com
Which will register user kafka with password drowssap, name franz, etc.
So it seems to me the best method would be to create an HTML form which collects the user information, then makes the HTTP request. This seems simple enough, but I'm not sure where the best place to start is. PHP? Python? Wget? Lynx? I'm not quite sure how to use those from within an HTML form.
Thanks.
Don't EVER include sensible data that way. That's a GET request. You need a POST request (which doesn't include data in the URL).
Your HTML should be like:
<form action="saveData.php" method="post">
<input type="text" name="username" />
<input type="password" name="password" />
<!-- other inputs here -->
<input type="submit" value="Create user" />
</form>
That form sends POST data to the saveData.php script. That script should process the parameters and redirect to another page.
<?php
// Here process the data the way you want (using data inside $_POST array, i.e. $_POST['username'], $_POST['password'], etc...
// Usually you'd want to save to a database
// When done, redirect to "success" page
header("Location: success.php");
?>
Your success.php page can contain anything:
<?php
echo "User created successfully!";
?>

Allow user to paste in "Tab-delimited" data into a textarea

My question is how do I allow a user to Paste in tab delimited data into a textarea on a website and then store and parse that data to run some action against it.
For example at http://www.batchgeo.com you can paste in your address location and from that a map is generated via the google maps api. I'm not looking for a step by step answer just what is happening on the server side of things to store and parse the data.
To receive data into a PHP script, just set up a form that POSTs to it:
<form action="script.php" method="post">
<textarea name="data" wrap="virtual"></textarea>
<input type="submit" value="Go!" />
</form>
Next, create the PHP script to receive the data:
<?php
$data = $_POST['data'];
# do something with $data...
?>
What comes next depends on what you want to do with that data.

Get POST response from a url and print response to page?

I'm trying to get a POST response from a url and I can not get the response to print to my html page instead it just redirects me to the url in the action with the response.
Is there a way to grab the response with html? php?
Code of html page i'm using
<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<form
method="post"
action="http://poster.decaptcher.com/"
enctype="multipart/form-data">
<input type="hidden" name="function" value="login">
<input type="text" name="username" value="client">
<input type="text" name="password" value="qwerty">
<input type="file" name="upload">
<input type="text" name="upload_to" value="0">
<input type="text" name="upload_type" value="0">
<input type="submit" value="Send">
</form>
</head><body></body></html>
Note: The url in the action will only show the response and nothing else is shown on the page.
Let's see if I can give this a try, because you seem to be a bit confused about how an HTML form works.
First and foremost, your website looks like so, correct?
<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<form
method="post"
action="http://poster.decaptcher.com/"
enctype="multipart/form-data">
<input type="hidden" name="function" value="login">
<input type="text" name="username" value="client">
<input type="text" name="password" value="qwerty">
<input type="file" name="upload">
<input type="text" name="upload_to" value="0">
<input type="text" name="upload_type" value="0">
<input type="submit" value="Send">
</form>
</head><body></body></html>
One thing to point out before we explain an HTML form, is that you have your form in the <head> of the webpage. Any element which is supposed to be seen by the user (or anything that you want to appear within the browser's main viewing area) should be in the <body>. Failure to do this puts the browser into a "quirks mode", where it actually doesn't know what you're talking about and it makes its best guess to try and build the website that it thinks you wanted. Mind you that modern browsers are very good guessers, but you should still re-write it as:
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<form
method="post"
action="http://poster.decaptcher.com/"
enctype="multipart/form-data">
<input type="hidden" name="function" value="login">
<input type="text" name="username" value="client">
<input type="text" name="password" value="qwerty">
<input type="file" name="upload">
<input type="text" name="upload_to" value="0">
<input type="text" name="upload_type" value="0">
<input type="submit" value="Send">
</form>
</body>
</html>
As far as explaining the <form> tag... When you submit a form in HTML, it actually loads the other website. It doesn't secretly send data in the background, it will take you away from the page you're viewing and take you to the page that you are sending the data to. At first this may sound silly. Why should it take you away from the page you're viewing just to send the data to another website? If you wanted to be redirected after sending the data, you'd redirect them there after sending the data.
The reason it's done this way is to greatly simplify the HTTP protocol. Whenever you load any website, you send and HTTP request. This request contains butt-loads of information. Among this information is:
Your IP address
What browser you're using
The page you were last visiting
How you accessed this page (clicked a link or typed the URL into the address bar)
The page you want to view (is it index.html or mysite.html?)
Any cookies related to that server
Any POST information (extra information which the server may or may not have asked for)
Every time the server receives one of these requests, it looks at all of the information and decides what to do. Usually a server will just look at the page you want to view and send it to you. Sometimes the page you want to view will need some extra work before it's ready to show, though. For instance, if a page ends in .php then it will search through the page for <?php, and everything after that point will be executed as a script. Only the output of the script is sent to the person who requested the page, not the script itself.
If you were to send your POST information to a website, wait 10 minutes, THEN go to the website, it would have no way of remembering that it was you who sent the post information before or what information you sent. Web servers have a very short attention span. For that reason if you sent a form to log into a website, then waited 10 minutes, then tried to view a member's only page- it would forget that you were logged in. For this reason it sends you the page as you're submitting the form. It does it while it still remembers that you're logged in, before it has a chance to forget. There's a good chance that the page it sends you will include a cookie which you can use to remind the server you were logged in next time you request a page.
If this made sense, then you should understand what happens when you submit a form. It doesn't just take your information and give it to the server. It sends that information to the server as part of an entire request, then the server sends you back a webpage and your browser displays that webpage. There is really only one way to send data to a server without redirecting you to that server afterwards. There are multiple ways to do this trick, however. You have to send a "dummy request", requesting a webpage with certain POST data, but ignoring the webpage that's returned.
In your example, you wanted to send data to http://poster.decaptcher.com. To do this without redirecting the user to http://poster.decaptcher.com, your easiest solution would be to use javascript and AJAX. Javascript has certain functions that allow you to send an HTTP request without reloading the page, then you let the javascript determine what to do with the page that's returned.
This is generally used when you want to reload a part of a webpage without reloading the whole thing. For instance, if you have a chat program and you want to update the chat window without refreshing the entire page. The javascript would request a webpage which contains ONLY the new lines of chat, minus any <html>, <head>, or <body> tags. It then takes those lines and displays them in the chat window.
You can, however, use AJAX to request a page and then ignore what's returned instead of display it on the page. By doing this you will have sent the POST data but not redirect the user.
Another option is to send the request to a third website, which can then send its own dummy request. For instance, submit the form to a PHP page that you own. The PHP script can then tell your server to send a dummy request to http://poster.decaptcher.com and ignore the response, then you can send them a webpage containing whatever you want.
Now that I've described both of these processes in adequate detail, I'll leave it as an exercise to the reader to figure out exactly how to do these. =)
The page refresh on submitted form is the default behavior of HTML.
For people who need to display the response into the same page without refresh, they will want to use Ajax. Here is how it could be done with jQuery:
$('#the_form').submit(function (e) {
e.preventDefault();
the_form = $(this);
$('#response_container').load(
the_form.attr('action')
, the_form.serialize()
);
})
the action defines the redirect to that page. If you want to catch the response, make your own script and place it in between the two. This is a bad way of doing it though. We developers call it hack coding. lol.
Not quite sure what you want to do. If you want to show the POST content on the page, just do this:
print_r($_POST);
If you want to see what is getting POSTed to the action URL, and you don't have access to that URL, just use the HTTP Headers plugin for Firefox.
action should go to a PHP file belonging to you! ie - action="/ProcessMyForm.php"
On that file, simply use $_POST and those form elements are in there, indexed by name, in an associative array.
Also - it may have been accidental, but post parameters dont go up in the URL like get, they are "behind the scenes" (invisible to the user) and also capable of being far larger.
PS - if you want to go to that other site afterwards, use header("Redirect: other-website-here.com")
First of all, mention your question specifically. If you want to fetch data from a URL than you can't use the form method="post". If you want to fetch data from URL, you have to use method "get". Calling print_r($_GET) can be used to retrieve data from HTML page to controller page.

Categories