Getting a response xml and redirect with php curl -
i having problem getting xml response server, first time have coded xml in php using curl (posting parsed xml server, , receiving response xml) - code below:
html:
<html> <head> <title>test</title> </head> <body> <form action="3fieldsample.php" method="post" enctype="multipart/form-data" > <div class="form-field title"> <div class="input select"><label for="title">title</label> <select name="title" id="title" value="mr"> <option value="mr">mr</option> <option value="mrs">mrs</option> <option value="dr">dr</option> <option value="miss">miss</option> <option value="ms">ms</option> </select></div> </div> <div class="form-field float-left"> <div class="input select"><label for="fname">first name</label> <input name="fname" value="" maxlength="150" width="100%" type="text" id="fname" /></div> </div> <div class="form-field float-left"> <div class="input text"><label for="applicationpaydayapplastname">last name</label> <input name="lname" value="" maxlength="150" type="text" id="lname"/></div> </div> <div class="submit"> <input type="image" src = "submit.jpg" width="25%" alt="submit" id="submit"> </div> </form> </body> </html>
=============================================== the processing:
<?php header('content-type: text/xml'); $xmldoc = new domdocument( '1.0' ); $xmldoc->preservewhitespace = false; $xmldoc->formatoutput = true; $url = "http://thesite_2_post_2.com/process.php"; $title = $_post['title']; $fname = $_post['fname']; $lname = $_post['lname']; $post_string = '<?xml version="1.0" encoding="utf-8"?> <lead> <applicant> <title>'. $title.'</title> <fname>'. $fname.'</fname> <lname>'. $lname.'</lname> </applicant> </lead>'; $header = "post http/1.0 \r\n"; $header .= "content-type: text/xml \r\n"; //#$header .= "content-type: text/html \r\n"; $header .= "content-length: ".strlen($post_string)." \r\n"; $header .= "content-transfer-encoding: text \r\n"; #$header .= "connection: close \r\n\r\n"; $header .= $post_string; print ($post_string); $ch = curl_init(); curl_setopt($ch, curlopt_header, 0); curl_setopt($ch, curlopt_url, $url); curl_setopt($ch, curlopt_post, true); curl_setopt($ch, curlopt_returntransfer, true); curl_setopt($ch, curlopt_postfields, $header); ###curl_setopt($ch, curlopt_postfields, $post_string); curl_setopt($ch, curlopt_ssl_verifypeer, false); $data = curl_exec($ch); curl_close($ch); $objresult = json_decode($data); # # illustration of how i'm supposed deal response? # # $_session['price'] = 0; if ($objresult->purchased == 1) { $_session['price'] = $objresult->price; $url = urlencode($objresult->redirect_url); header('location: '.$url); print $objresult; } else { echo "something went wrong"; } ?>
the lastly snippet above illustration of how i'm supposed deal response i'm not getting. output getting : this page contains next errors: error on line 8 @ column 8: content @ end of document. below rendering of page first error.
mr peter griffin
when view source output (without leading hashtags):
#<?xml version="1.0" encoding="utf-8"?> #<lead> #<applicant> #<title>mr</title> #<fname>peter</fname> #<lname>griffin</lname> #</applicant> #</lead>something went wrong
any help in getting response xml server appreciated.
curl handles many details of http request you, including headers. sending xml document simplified this:
[...] $cu = curl_init($url); curl_setopt($cu, curlopt_postfields, $post_fields); // implies curlopt_post = true. curl_setopt($cu, curlopt_returntransfer, true); curl_setopt($cu, curlopt_httpheader, [ "content-type: text/xml" ]); $data = curl_exec($cu); [...]
you printing xml document , later executing header function. won't work, because headers must set before output done.
as side note, should cautious inserting post parameters without validation in xml. code:
$title = $_post['title'];
poses vulnerability, because attacker can send in http requests. if xml sent value?
php xml curl
No comments:
Post a Comment