0

I am building a application in php and this is the form that i have built:

This is the html form

 <form method="POST" action="verifystate.php" enctype="multipart/form-data" >
    <select class="form-select" aria-label="Default select example" name="xxx">
    <option selected>Selectati aaa</option>
    <option>U</option>
    <option>C</option>
    </select>
    <select class="form-select" aria-label="Default select example" name="yyy">
    <option selected>Selectati bbb</option>
    <option >1111</option>
    <option >188</option>
    <option >7243</option>
    </select>
    <div class="input-group mb-3">
    <span class="input-group-text" id="inputGroup-sizing-default">Serial</span>
    <input type="text" class="form-control" aria-label="Sizing example input" aria- 
 describedby="inputGroup-sizing-default" id="serial" name="serial" value="200">
    </div>
     File: <input type="file" name="file"><br><br>
      <br/>
      <input type="submit" value="submit"  name="submit">
    </form>

This is the php script that processes the file:

<?php

$fileContent = file_get_contents($_FILES['file']['tmp_name']);
//echo $fileContent;

$standard = $_POST['standard'];
//echo $standard;

//post request to upload api 

$url = 'http://restapiurl? 
     standard=' . _POST[yyy] . '&cif=' . $_POST['xxx'];
//The URL that you want to send your XML to.

//Initiate cURL
$curl = curl_init($url);

$headers = [
    "Content-Type: text/xml",
    "serial_certificate:" . $_POST['serial']
];

//Set the Content-Type to text/xml.
//curl_setopt ($curl, CURLOPT_HTTPHEADER, array("Content-Type: text / xml","certificate:200"));
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
//Set CURLOPT_POST to true to send a POST request.
curl_setopt($curl, CURLOPT_POST, true);

//Attach the XML string to the body of our request.
curl_setopt($curl, CURLOPT_POSTFIELDS, $fileContent);

//Tell cURL that we want the response to be returned as
//a string instead of being dumped to the output.
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);

//Execute the POST request and send our XML.
$result = curl_exec($curl);

//Do some basic error checking.
if (curl_errno($curl)) {
    throw new Exception(curl_error($curl));
}

//Close the cURL handle.
curl_close($curl);

//Print out the response output.
echo $result;

?>

I searched on the internet but found nothing new... i included the headers the xml file in the body the api rest url address is correct. I am a newbie in PHP so if you can help me I would be very happy. The problems is that the echo $result does not return anything. It should return a number

waterloomatt
  • 3,662
  • 1
  • 19
  • 25
JohnNewman
  • 29
  • 1
  • 10
  • Make sure to turn on error reporting (https://stackoverflow.com/a/21429652/296555) at the top of your PHP file. Fix, or report back any errors you find. – waterloomatt Feb 02 '22 at 13:34
  • Syntax errors in the URL `$url = 'http://restapiurl? standard='._POST[yyy].'&cif='.$_POST['xxx'];`. Notice `_POST[yyy]`. What's that? – waterloomatt Feb 02 '22 at 13:35
  • I hid the real names because it is a job project – JohnNewman Feb 02 '22 at 13:41
  • Perhaps start by debugging your CURL request. If you get stuck, post back the results of that so we can help dig in - https://stackoverflow.com/a/26611776/296555. – waterloomatt Feb 02 '22 at 13:48
  • I debugged my curl request and I have no errors. The response can be seen in developer tools in network in response tab but i need it on screen. With echo $result or print $result it does not print anything on the screen – JohnNewman Feb 02 '22 at 14:17
  • Are you showing us all the code? Not submitting the form via AJAX or doing any redirects? What does the page look like after submitting the form? White, blank page, or does it redirect back to the form? – waterloomatt Feb 02 '22 at 14:25
  • The form is on page a.php and its action is to b.php it goes to b.php and it is white blank and stays on b.php – JohnNewman Feb 02 '22 at 14:28
  • Good. OK please try `var_dump($result);` instead of `echo` or `print`. Also, turn on error reporting as my first comments suggests. – waterloomatt Feb 02 '22 at 14:32
  • it says on the screen - string(185) " " – JohnNewman Feb 02 '22 at 14:35
  • The response coming back is a string that has a length of 185. I really can't say much more without seeing the code that CURL hits. Please edit your question with all these details as the comments section is getting long. – waterloomatt Feb 02 '22 at 14:46

0 Answers0