Sending POST data using cURL, by scanning QR code

时间:2015-11-12 12:11:36

标签: php curl qr-code

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 a HTML 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.

enter image description here

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 the values of the input fields.

Now, I get only the response of check.php (plain text). When I pass an URL instead of the passData() 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.

2 个答案:

答案 0 :(得分:3)

QR码无法执行代码。您可以在QR码中放入的唯一可执行数据类型是URL。这就是为什么使用google.com作为URL打开到该URL的Web浏览器。 QR码本身不会呈现任何内容。

您的代码正在做的是在生成QR代码时获取check.php页面,然后将输出存储为原始数据。它不是一个网页,它是一个像你在问题中看到的字符串。您可以传递类似于书签的javascript URL,但其执行将取决于所使用的QR码阅读器。

bookmarklet示例

<?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;
}

更好的方法是让您的QR码在您的计算机上生成如下网址:http://your-site.com/check.php?name=John&surname=Doe和托管check.php。您可以使用$_GET数据填充表单,然后使用javascript自动将其发布为Jah提及的内容。

答案 1 :(得分:0)

不是最好的方法,但你可以做这样的事情。

<强> Check.php:

 <?php
$data = '<form method="post" action="handle.php">
    <input type="text" name="name" value="name" /><br />
    <input type="text" name="surname" value="surname" /><br />
    <input type="submit" />
</form>';

$html = str_replace(PHP_EOL, ' ', $data);
$html = preg_replace('/[\r\n]+/', "\n", $html);
$html = preg_replace('/[ \t]+/', ' ', $html);
$html = str_replace('> <', '><', $html);
?>
<div id="placeholder">
    Write HTML here
</div>

<script type="text/javascript">
    function write_html(id,data){
        var formHtml = data;
        document.getElementById(id).innerHTML = formHtml;
    }
</script>