By jsp_1983 on
Hi... I'm trying to find out how I can automatically generate nodes from data I want to import from a 3rd party API. Say, for example, create nodes based on a 'title' field or similar for each item passed from the 3rd party and also then incorporate data, such as prices and stock availability, on those nodes which can be updated dynamically from the 3rd party.
There appear to be several modules that deal with APIs, but I'm not very clear as to which are useful to me.
Can anybody point me in the right direction?
Comments
Services module
Hello,
I would suggest you to use the services module. Using that module you can insert data into Drupal nodes (either Drupal or new created CCK content types that you may need)
Regards,
Sergio
Great, thank you, Sergio!
Great, thank you, Sergio!
Can you recommend any tutorials for it? The documentation on the module page isn't very clear to me/ seems to require a more advanced understanding than I have.
CCK + Services
Hello,
(This is going to be a long post, but hopefully it will have all information required for you to get your job done! :-) )
I will assume your trouble is with services module (creating a new content type with CCK is very easy: just go to "Content Management" --> "Content types" --> "Add content type").
For this "tutorial" I will assume that your created content type is called "drupal_content" (Important when assigning permissions below).
The services module is a bit more complicated, and I certainly agree that documentation could be better for beginners. I will try to lead you through the most important steps I had to realize to get it working a couple of months ago (there are a lot of steps, but are very quick to perform):
Wow! Now you have the basic infrastructure working... and it is time to create your web service client.
Some things to take into consideration: Drupal makes a big effort regarding security when using services.
Thus, it makes sense to do what has been explained above: have an unique user who will insert data nodes into your system (maybe you can use several users, or even roles, but the above schema will work for a basic scenario).
Any user willing to use Drupal web services will need a key to access them (that's the last steps defined above). Every key is associated to a domain, and does only allow some methods to be accessed. Trying to access the service from a different IP / domain name will not work.
Now comes the tricky part:
If you want to use any Drupal service, you will always have to use the following schema:
That is very important, as otherwise you will not be able to use services. All of this is done for security reasons, and to prevent spam. (But yes, it means that you will always invoke 3 + the number of services you really want to invoke)
How to use it? Well...
IMPORTANT: Many web services will need additional parameters. Required parameters are described (and can be tested) by clicking on each method name in "Site building" --> "Services" --> "Browse".
For example, the "node.save" method requires 6 arguments: "hash", "domain_name", "domain_time_stamp", "nonce", "sessid", "node". The required types are also described on that page.
The hash is a concatenation of (Using ; to concatenate):
Note: "service_invoked" would be "user.login", "node.save", "user.logout", etc. depending on each call.
The domain_name is the same used in the key created above (IP / domain name of your desktop application computer).
The domain_time_stamp is the timestamp of the desktop application computer.
The nonce is a random value that must be unique every time a service is invoked.
The sessid is the session ID that is retrieved when loggin the user.
Finally, the node is the content that you really want to create in Drupal (your "drupal_content").
Keeping the ideas above together, I would suggest you to look at some good examples of web service clients (how to create hash, nonce, etc...):
One last thing to keep in mind: CCK fields are special fields. Even if they have a single value, they are inside a new array/structure. Look at the following link for a clear example:
http://acquia.com/blog/migrating-drupal-way-part-i-creating-node
Hope it helps! :-)
Regards,
Sergio
Wow... Quite a bit to go at
Wow... Quite a bit to go at there! Thanks... I'll give it a try and see how much of it sinks in ;)
Thanks a bunch Sergio! Great
Thanks a bunch Sergio!
Great stuff - I'm about to do sth similar and your post is outstanding helpful!
Fantastic!
Thanks a million Sergio. The time you spent creating this post is extremely appreciated. I'll be using it in about 3 weeks, and hopefully nothing significant has changed from your post date till then. Thanks again for this outstanding contribution.