2017-02-16 14 views
1

Я хочу отправить данные из моего для запроса на отправку xml в php, но я получаю сообщение об ошибке.как отправить данные формы как xml с помощью http post в php

//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; 

Ответ, который я получил, это «Запрещенные ключевые символы».

ответ

0

Вы не можете интерполировать переменные в одиночных кавычках, а также не выполнять такие функции, как обрезка в контексте строки. Кроме того, для отправки XML неверно Content-type.

Попробуй так

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

и

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