简体   繁体   中英

how to send form data as xml using http post in php

i want to send data from my for as an xml post request in php but i am getting an error.

//taking form data into custom variables
$customer_state =  $_POST['customer_state'];
$lastname = $_POST['lastname'];
$firstname = $_POST['firstname'];
// creating xml from form data
$xml = ' <?xml version="1.0" encoding="UTF-8"?>
<crcloud>
              <lead>
                <type>trim($customer_state);</type>
                <firstname>trim($firstname);</firstname>
                <lastname>trim($lastname);</lastname>
                </lead>
            </crcloud>';
//trying to send the xml as a post request
$url was pre defined here

    $stream_options = array(
'http' => array(
'method'  => 'POST',
'header'  => 'Content-type: application/x-www-form-urlencoded' . "\r\n",
'content' =>  $xml));

$context  = stream_context_create($stream_options);
$response = file_get_contents($url, null, $context);
echo $response;

The response I get is "Disallowed Key Characters."

You can't interpolate variables in single-quoted strings, nor can you execute functions like trim in a string context. Also, your Content-type is incorrect for sending XML.

Try it this way

$xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><crcloud/>');
$lead = $xml->addChild('lead');
$lead->addChild('type', trim($customer_state));
$lead->addChild('firstname', trim($firstname));
$lead->addChild('lastname', trim($lastname));

and

'header'  => "Content-type: text/xml\r\n",
'content' => $xml->asXML()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM