如何在php中使用http post以xml形式发送表单数据

如何在php中使用http post以xml形式发送表单数据,php,xml,http,post,Php,Xml,Http,Post,我想在php中以xml post请求的形式从我的for发送数据,但出现了一个错误 //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=

我想在php中以xml post请求的形式从我的for发送数据,但出现了一个错误

//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;
//将表单数据转换为自定义变量
$customer\u state=$\u POST['customer\u state'];
$lastname=$\u POST['lastname'];
$firstname=$_POST['firstname'];
//从表单数据创建xml
$xml='0
修剪(客户/州);
特里姆(名字);
修剪(姓氏);
';
//尝试将xml作为post请求发送
$url已在此处预定义
$stream\u options=数组(
“http'=>数组(
'方法'=>'发布',
'header'=>'内容类型:application/x-www-form-urlencoded'。“\r\n”,
'内容'=>$xml));
$context=stream\u context\u create($stream\u选项);
$response=file\u get\u contents($url,null,$context);
回音$应答;

我得到的响应是“不允许的关键字符”。

不能在单引号字符串中插入变量,也不能在字符串上下文中执行trim之类的函数。另外,您的
内容类型不适合发送XML

这样试试看

$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));

不能在单引号字符串中插入变量,也不能在字符串上下文中执行trim之类的函数。另外,您的
内容类型不适合发送XML

这样试试看

$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));