XML-RPC, WordPress & Python
WordPress beschikt over een XML-RPC-interface. Da's handig, want daarmee kan ik een headless installatie in Python doen. Althans, in theorie. De praktijk valt waarschijnlijk tegen: XML-RPC is slechts beperkt toepasbaar.
Mogelijkheden
XML-RPC WordPress API
XML-RPC WordPress API is de standaard XML-RPC-API voor interactie met een WordPress-site. Er bestaan clients voor verschillende programmeertalen, waaronder de python-wordpress-xmlrpc-client
- Hier is een overzicht van de componenten. Ik geloof dat dit niet compleet is. Je kunt waarschijnlijk geen complete headless WordPress-site bouwen mbv. XMP-RPC
- Waarschijnlijk kun je met de beschikbare componenten, ook WooCommerce-producten lezen en schrijven [1]. Dat zijn immers toch ook posts?
- Voorbeeld van een beperking: Ik geloof dat je categorieën kunt lezen, maar dat je die niet kunt schrijven.
WooCommerce-XML-RPC-API
In 2013 heeft Maxrice deze WooCommerce-XML-RPC-API ontwikkeld. Daarmee kun je orderstatussen lezen en schrijven.
Installatie python-wordpress-xmlrpc
python-wordpress-xmlrpc is een Python-library die als interface functioneert tussen Python en Wordpress's XML-RPC-API. Installatie:
- Je kunt deze bibliotheek installeren via
pup
ofeasy_install
. Pip heb ik geprobeerd te installeren, maar die liep vast in z'n eigen foutmeldingen - Installatie easy_install:
sudo apt install python-setuptools
- Installatie bibliotheek:
sudo easy_install python-wordpress-xmlrpc
.
Mijn eerste XML-RPC-call (okt. 2018)
Dit werkte, nadat ik de witregel-foutmelding hieronder had verholpen.
#! /usr/bin/python print(">>> 01.py") from wordpress_xmlrpc import Client, WordPressPost from wordpress_xmlrpc.methods.posts import GetPosts, NewPost from wordpress_xmlrpc.methods.users import GetUserInfo wp = Client('http://example.com/xmlrpc.php', 'rpc-account', 'rpc-wachtwoord') wp.call(GetPosts()) # Doesn't do anything wp.call(GetUserInfo()) # Doesn't do anything post = WordPressPost() post.title = 'My new title' post.content = 'This is the body of my new post.' post.terms_names = { 'post_tag': ['test', 'firstpost'], 'category': ['Introductions', 'Tests'] } wp.call(NewPost(post)) print("<<< 01.py")
Foutmelding: ExpatError: XML or text declaration not at start of entity (okt. 2018)
Code:
#! /usr/bin/python from wordpress_xmlrpc import Client, WordPressPost from wordpress_xmlrpc.methods.posts import GetPosts, NewPost from wordpress_xmlrpc.methods.users import GetUserInfo wp = Client('http://bsp.dvb/xmlrpc.php', 'rpc_account', 'rpc_wachtwoord')
Geeft foutmelding:
xml.parsers.expat.ExpatError: XML or text declaration not at start of entity: line 2, column 0
Het schijnt te komen door een onbedoelde witregel doordat WordPress iets doet met dubbele openingstags. Oplossing: [2]
Zie ook
Bronnen
- https://codex.wordpress.org/XML-RPC_WordPress_API
- https://codex.wordpress.org/XML-RPC_Support
- https://python-wordpress-xmlrpc.readthedocs.io/en/latest/
- https://github.com/maxcutler/python-wordpress-xmlrpc/issues/12
- https://github.com/syednasir/woocommerce-xml-rpc-api
- https://cycloneos.net/adding-editing-products-woocommerce-via-xml-rpc/ - WooCommerce benaderen via de standaard WordPress-XML-RPC-API
- https://wordpress.stackexchange.com/questions/144376/using-xml-rpc-to-import-data-into-wordpress