Sending POST data using cURL, by scanning QR code
QR codes cannot execute code. The only executable type of data you can put in a QR code is a URL. That is why using google.com as a URL opens a web browser to that URL. The QR code itself does not render anything.
What your code is doing is fetching the check.php
page when the QR code is generated and then storing the output as the raw data. It isn't a webpage, it is a string like you are seeing in your question. You may be able to pass a javascript URL similar to a bookmarklet but its execution would depend on the QR code reader being used.
bookmarklet example
<?php
function passData() {
// javascript code in a heredoc, you may need to url encode it
return <<<JS
javascript:(function() {
//Statements returning a non-undefined type, e.g. assignments
})();
JS;
}
A better way to do it would be to have your QR code generate a URL like: http://your-site.com/check.php?name=John&surname=Doe
and host check.php
on your machine. You can use the $_GET
data to populate your form and then use javascript to automatically post it as Jah mentioned.
Comments
-
JK87 almost 2 years
I'm trying to pass some data (
JSON
) to another page by scanning a QR code. The page where the data is send to, contains aHTML
form. I want to use that form as a last chance to correct the data before sending it to the database.I found here at S.O. a way to pass the data using cURL: (https://stackoverflow.com/a/15643608/2131419)
QR code library: http://phpqrcode.sourceforge.net
I use the QR code execute this function:
function passData () { $url = 'check.php'; $data = array('name' => 'John', 'surname' => 'Doe'); $ch = curl_init( $url ); # Setup request to send json via POST. $payload = json_encode($data); curl_setopt( $ch, CURLOPT_POSTFIELDS, $payload ); curl_setopt( $ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); # Return response instead of printing. curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); # Send request. $result = curl_exec($ch); curl_exec($ch); curl_close($ch); # Print response. return $result; }
Create QR code:
QRcode::png(passData(), $tempDir.'007_4.png', QR_ECLEVEL_L, 4); echo '<img src="'.$tempDir.'007_4.png" />';
Check.php
<?php $data = json_decode(file_get_contents("php://input"), true); ?> <form method="post" action="handle.php"> <input type="text" name="name" value="<?php echo $data['name'];?>" /><br /> <input type="text" name="surname" value="<?php echo $data['surname'];?>" /><br /> <input type="submit" /> </form>
Problem:
I can pass the data to check.php, but it's returning plain text instead of a useable
HTML
form.Hope someone can help!
EDIT
Some clarification:
What I actually want is, to scan the QR code, which executes the passData() function. Then the 'QR code scanner app', needs to open a browser, which shows
check.php
with the form AND the passed data as thevalues
of theinput
fields.Now, I get only the response of
check.php
(plain text). When I pass anURL
instead of thepassData()
function like:
QRcode::png("http://www.google.com", $tempDir.'007_4.png', QR_ECLEVEL_L, 4);
The app asks if I want to go to http://www.google.com.