0

I tried to sue the post QR image to endpoint url. If using postman the value returns properly. But if I tried using the PHP CURL the params did not send with error: QRcode not present.

Here my HTML form and PHP:

<form id="uploadForm" action="upload.php" method="post">
<div id="targetLayer">No Image</div>
<div id="uploadFormLayer">
<input name="qrcode" type="file" class="inputFile" /><br/>
<input type="submit" value="Submit" class="btnSubmit" />
</form>
$(document).ready(function (e) {
    $("#uploadForm").on('submit',(function(e) {
        e.preventDefault();
        $.ajax({
            url: "upload.php",
            type: "POST",
            data:  new FormData(this),
            contentType: false,
            cache: false,
            processData:false,
            success: function(data)
            {
            $("#targetLayer").html(data);
            },
            error: function() 
            {
            }           
       });
    }));
});

and here the PHP:

if(isset($_POST['qrcode'])){
    $filename = $_POST['qrcode'];
}
if(isset($filename)){ 
    echo $filename;
}
if(is_array($_FILES)) {
if(is_uploaded_file($_FILES['qrcode']['tmp_name'])) {
$sourcePath = $_FILES['qrcode']['tmp_name'];
$targetPath = "images/".$_FILES['qrcode']['name'];
if(move_uploaded_file($sourcePath,$targetPath)) {
    $gambar1 = array('qrcode' => $targetPath);

ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);

define("API_URL", "http://endpoint/img");
define("MULTIPART_BOUNDARY", "----WebKitFormBoundary7MA4YWxkTrZu0gW");

$api_headers = array(
        "Authorization: Basic ZGVuYTpzaWRhcmVqYTg0",
        "Accept: application/json; esl-api-version=11.10",
        "Content-Type: multipart/form-data; boundary=\"" . MULTIPART_BOUNDARY . "\"",
    );

$postdata = "--" . MULTIPART_BOUNDARY . "\r\n";
$postdata .= "Content-Disposition: form-data; name=\"file\"; filename=\"$targetPath\"\r\n\r\n";
$postdata .= file_get_contents("qr.png");
$postdata .= "\r\n\r\n";
$postdata .= "--" . MULTIPART_BOUNDARY . "\r\n";
$postdata .= "Content-Disposition: form-data; name=\"qrcode\"\r\n\r\n";
$postdata .= json_encode($build);
$postdata .= "\r\n\r\n";
$postdata .= "--" . MULTIPART_BOUNDARY . "--\r\n";

$curl = curl_init(API_URL);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST          , true);
curl_setopt($curl, CURLOPT_POSTFIELDS    , $postdata);
curl_setopt($curl, CURLOPT_HTTPHEADER    , $api_headers);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_exec($curl);
$response = curl_multi_getcontent($curl);
curl_close($curl);

echo mb_convert_encoding(print_r(json_decode($response, true), true), 'CP932', 'UTF-8');

I really appreciate it if anyone has clue for my problem, I don't know why the params QRcode not sending to the server since I am really new using CURL.

I am using PHP version 7 for this, also I tried to copy-paste the code generated by Postman but still return the same error QR code not resent

Thanks

Sujal Patel
  • 2,444
  • 1
  • 19
  • 38
  • Tried but got same response – Tongkol Goreng Oct 09 '19 at 08:08
  • You should not attempt to assemble the body content for such a multipart request yourself, but let cURL handle that. – 04FS Oct 09 '19 at 08:10
  • **[You should not switch off `CURLOPT_SSL_VERIFYHOST` or `CURLOPT_SSL_VERIFYPEER`](https://paragonie.com/blog/2017/10/certainty-automated-cacert-pem-management-for-php-software)**. It could be a security risk! [Here is how to get the certificate bundle if your server is missing one](https://stackoverflow.com/a/32095378/1839439) – Dharman Oct 11 '19 at 18:52

0 Answers0