0

can I connect to my Drupal website using web services and pass a variable (with post method) to it? I know that forms in Drupal actually have hidden elements so that it is not possible to submit the form from external sources.

There is also a service module but I don't think I can achieve what I am trying to do with it.

The following is what I am trying to do with Drupal:

When I connect to www.drupal-site.com/web-service (web-service is just an example), I would like to be able to post variables to it and perform complex database queries. I would also like to return the result of the queries in json (using echo maybe). I would also like to be able to remove the headers and footers of my drupal site.

I really need some direction on how this can be achieved.

1
  • Do you need authentication or is the service publicly available?
    – Clive
    Commented Dec 11, 2011 at 15:52

2 Answers 2

2

Your use case can be achieved using Services module easily.

All you need to do is write a hook_service()

refer http://drupal.org/node/118126 Or just look into the services module's any child module, may be user_services.

Do this,

  1. Write a service that exposes all your query parameters as fields.
  2. Accept these parameters into your web service callback and perform your business logic.
  3. Return your results, this is just a return statement, no JSON conversion here. ex: return $output. //$output contains your array or data structure that you have as a result of your queries.

Now, how to convert the data to JSON. ?

Services module will provide you intermediate servers, XML-RPC, JSON. (these are by default, there are more avaialable on drupal.org). These servers function as end points to whom you can connect using external applications or entities. ex: http://www.domain.com/services/json.

Thats it, the flow will be,

  1. You connect to the endpoint, URL http://www.domain.com/services/json. Pass the data in JSON.
  2. the JSONserver will convert that into PHP and will pass it to your web service callback.
  3. You do the processing and return the data to the web service in PHP.
  4. The server converts the data into JSON again.
  5. This is handed over to the HTTP request that requested the processing in the first place.
0

It is possible to submit a form from another site out of the Drupal, this is the code I use on an external site to display a Drupal login form and it works fine, after submitting the form, the user will be logged-in on the remote site.

<form id="loginForm" action="http://mydrupalsite.com/node/60?" method="post" name="login">    
  <div class="login-datas">
    <div class="login-field">
      <label for="name">Login: </label>
      <input type="text" name="name" id="name" class="txt">
    </div>
    <div class="login-field">
      <label for="pass">Password: </label>
      <input type="password" name="pass" class="txt" id="pass">
    </div>
  </div>
  <div class="login-send">
    <input name="Submit" type="submit" id="submit" value="Enter">
    <input name="form_id" id="edit-user-login-block" value="user_login_block" type="hidden">
  </div>
</form>
3
  • One possible problem I see with this approach is that we are missing the hidden form id. By removing it we are circumventing a Drupal security mechanism? Commented Dec 14, 2011 at 22:11
  • Do you have some more information about this security mechanism ? I'm not very sure how it is circumvented by my very trivial method ;)
    – tostinni
    Commented Dec 14, 2011 at 23:47
  • I'll find the links tomorrow. Too late for copy pasta now =\ Commented Dec 15, 2011 at 3:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.