1

I need to be able to integrate an external PHP script with Drupal 7. However because the script also connects to a MySQL database errors are produced when I include the bootstrap.inc, so I'm looking at using the Services module. Will this allow me to determine if the user is logged in to Drupal, and if so get details about the user? If so, could you please show me how this could be done.

Also, is it possible to secure the server/service so that it can only be accessed by scripts on the same host?

1 Answer 1

2

Services module works with the entity system so you should be able to pull some user data out of the box like email, username etc. I'm not exactly sure if Services modules send you any clue about user data that you can use to determine if the user is online or not but you can add a small custom code that alter the read functionality of the Services for User entity to query and return the current session. See code sample below.

Couple of function you might need in the process is:

// you can alter the resources object and use your own 
hook_services_resources_alter()

// implement it as below in your custom module
function YOURMODULE_services_resources_alter(){
  $resources['user']['retrieve']['callback'] = '_YOURMODULE_api_user_resource_retrieve';
}

// then implement the retrive
function _YOURMODULE_api_user_resource_retrieve($uid){
    $account = user_load($uid);
  if (empty($account)) {
    return services_error(t('There is no user with ID @uid.', array('@uid' => $uid)), 404);
  }

  services_remove_user_data($account);
  // DO YOUR SQL TO CHECK IF USER IS ONLINE
  // _check_this_user_online($uid);

  // Everything went right.

  return $account;
}
4
  • Thank you very much for your suggestion. Would it be possible to add some form of authentication also, so that only the external PHP script would be able to make a request to the services. I'm quite new to web services, as you can probably tell, so if this isn't possible please let me know
    – Andy
    Jul 3, 2013 at 19:59
  • Sure, there is an authentication tab where you can enable authentications, it could be user auth, oAuth etc. In your case I think best is create a user account in Drupal and use those creds to make API request. So request will be two step process, first get the authentication token from Drupal(Cookle session) and use it for other CRUD operations.
    – heshanlk
    Jul 3, 2013 at 20:16
  • Ah, I didn't see that authentication tab. What you've said makes sense for me. I was thinking that it should be more secure, but at the end of the day the Admin account only has a password so there's no point it making the Service more secure than the Admin account really! For checking if the user is logged in, wouldn't returning user_is_logged_in() work?
    – Andy
    Jul 5, 2013 at 10:52
  • Yep, that'll work.
    – heshanlk
    Jul 5, 2013 at 12:44

Your Answer

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

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