3

I have a question about what would be the best way for a module to accept a posted XML file via a web service. What I am trying to do is receive the file, read through the contents of the file, and update a setting on my site via the contents of the file. Could some one point me in the right direction?

  • What does "posted", mean in this context? – Letharion May 24 '12 at 16:37
  • OH sorry, "posted," means that if I want to POST a file via web service to my Drupal site I would be able to recieve this file and parse it through – AgnosticDev May 24 '12 at 16:45
1

This isn't something I've tried myself, but if you create a form with a file element, you should be able to control the $_POST['id'] that Drupal looks for by manipulating the id of the file-element.

That way the file should get passed on to the forms submit function, and then you can do whatever you want with it's content and react accordingly afterwards.

  • Hello Letharion, thank you for the advice. A very interesting idea. I will give it a try and let you know of my outcome. Thank you. – AgnosticDev May 24 '12 at 17:35
  • Cool, I look forward to hearing how it works out. – Letharion May 24 '12 at 18:40
  • Hello Letharion, I ended up taking a different approach to answer my question. Instead of posting a file, I parsed the XML file into a "postable" string and used cURL to hit a users route in Drupal, log the user in, and post that data to the module I had waiting for my post data on the other side. – AgnosticDev May 25 '12 at 13:11
  • Glad you worked it out. :) Please post your solution as an answer, and accept it, so the question get's removed from the unanswered queue. – Letharion May 25 '12 at 13:36
0

While I haven't used it for your exact situation, you should take a look at the Services module. The module is rather well documented, and has examples. Creating an endpoint and wiring up your callbacks should be fairly easy.

  • This is good module to keep in mind, I had trouble installing it though because Drupal kept telling me I was missing the OAuth module, I tried installing it and it would not install properly? I ended up taking a different approach posting data with cURL though. – AgnosticDev May 25 '12 at 13:15
0

Here is the cURL solution.

//login curl
$crl = curl_init();
$url = "http://************.localhost:8080/user/login";
curl_setopt($crl, CURLOPT_URL, $url);
curl_setopt($crl, CURLOPT_COOKIEFILE, "/tmp/cookie.txt");
curl_setopt($crl, CURLOPT_COOKIEJAR, "/tmp/cookie.txt");
curl_setopt($crl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($crl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($crl, CURLOPT_POST, 1);
//Drupal login credentials
$postdata = array(
  "name" => "restuser", 
  "passw" => "*******",
  "form_id" => "user_login", 
  "op" => "Log in",
);

//Tell cURL to use post data
curl_setopt ($crl, CURLOPT_POSTFIELDS, $postdata);

$result = curl_exec($crl);
$header_code = curl_getinfo($crl, CURLINFO_HTTP_CODE);
if ($header_code != 200) {
    die("Cannot login.");
    echo 'Connection Died';
}else{
       if (file_exists('order.xml')) {
            $xml_data_before_encode = simplexml_load_file('order.xml');
            $xml_data_post_data = preparePostFields($xml_data_before_encode) ;

            $URL = 'http://*******.localhost:8080/users/restuser/12345';
            $crl = curl_init($URL);
            curl_setopt($crl, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($crl, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($crl, CURLOPT_POST, 1);
            curl_setopt($crl, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
            curl_setopt($crl, CURLOPT_POSTFIELDS, "$xml_data_post_data");
            curl_setopt($crl, CURLOPT_RETURNTRANSFER, 1);
            echo $output = curl_exec($crl);
            curl_close($crl);
       }else{
            print 'Second cURL request did not fire.';
       }         

} function preparePostFields($array) {

  $params = array();

  foreach ($array as $key => $value) {
     $params[] = $key . '=' . urlencode($value);
  }
  return implode('&', $params);

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.