How to send data by XML file through an API integration to the server

2

I am using the Framework Codeigniter and trying to do a system integration via API using XML.

I have to make a file upload with XML parameters to a server via post. Basically the server has to receive a string with date = xml content.

I tested with the Advanced Rest Client (chrome extension that simulates a POST sending) and it worked perfectly, ie the server is OK.

When playing in PHP code it can not send XML and returns error 400 (bad request).

Does anyone have any tips?

Follow the code below:

<?php
$query = $this -> db -> query('SELECT token FROM configuracao LIMIT 1');
$row = $query -> row_array();
$token = $row['token'];

$conteudoXML= "data=";
$conteudoXML.= "<schedule>";
$conteudoXML.= "<alternativeIdentifier>1234567</alternativeIdentifier>";
$conteudoXML.= "<observation>1234567</observation>";
$conteudoXML.= "<agent><id>220876</id></agent>";
$conteudoXML.= "<serviceLocal><alternativeIdentifier>teste</alternativeIdentifier></serviceLocal>";
$conteudoXML.= "<activitiesOrigin>4</activitiesOrigin>";
$conteudoXML.= "<date>2015-11-25</date>";
$conteudoXML.= "<hour>00:00</hour>";
$conteudoXML.= "<activityRelationship>";
$conteudoXML.= "<activity><alternativeIdentifier>corretiva</alternativeIdentifier></activity>";
$conteudoXML.= "</activityRelationship>";
$conteudoXML.= "</schedule>";

$url = "http://api.umov.me/CenterWeb/api/$token/schedule.xml";
$xml_str = $conteudoXML;


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

$context  = stream_context_create($stream_options);
$response = file_get_contents($url, null, $context);
?>
    
asked by anonymous 26.11.2015 / 14:03

1 answer

1

I was able to resolve ...

I changed the method to Curl (I had to install because on my server it was not available).

In the end it looks like this:

//setting the curl parameters.
$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,$URL);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);

    if (curl_errno($ch)) 
{
    // moving to display page to display curl errors
      echo curl_errno($ch) ;
      echo curl_error($ch);
} 
else 
{
    //getting response from server
    $response = curl_exec($ch);
     print_r($response);
     curl_close($ch);
}
    
26.11.2015 / 15:05