Login to WordPress using RCurl

2019-07-04 22:43发布

问题:

I would like to login into a WordPress website using R's RCurl package in order to install a WordPress plugin (probably use postForm on some options pages in WordPress). Since the website is password protected, I ask for your help in how to authenticate my R session.

I found the following three links relevant, but do not know how to use them for WordPress:

  • login to mediawiki using RCurl
  • Login Wordpress using HttpWebRequest
  • http://r.789695.n4.nabble.com/RCurl-HTTP-Post-td3311942.html

Any suggestions?

Thanks.

回答1:

I've dealt with something similar using REDCap - might be useful for you too. This short example shows how I used the REDCap API to grab data.

library(RCurl)
out <- postForm("https://redcap.url.org/redcap/api/",
                 token="INSERT TOKEN HERE",
                 content="record",
                 type="flat",
                 format="csv",
                 .opts=curlOptions(ssl.verifypeer=FALSE))
write(out,file="C:/wherever/out.csv")

Note that this doesn't do proper checking for SSL - I've improved that in later versions. The more lengthy explanation is on my Google+ profile: https://plus.google.com/106259574970597769926/posts/U3fVCTV4EdQ

It looks like you can use cURL to log into Wordpress by passing the right parameters, as described here: http://w4dev.com/wp/login-into-wp-using-curl/

It looks like the URL in question is http://localhost/wordpress/wp-login.php and the parameters that matter can be found at the page above. Basically, you need to define the user, their password, and where you want to redirect them to on the site afterwards. These are how the parameters are described in that PHP example

"log=". $login_user .
"&pwd=" . $login_pass .
"&wp-submit=Log%20In&redirect_to=" . $visit_url;

Basically, it's just constructing a string to post, which looks something like:

http://yourwordpress.fake/wp-login.php?log=trehman&pwd=abc123456&wp-submit=Log%20In&redirect_to=http://yourwordpress.fake/pageyouwant

So, you could just change the postForm above to have a different URL and parameters, and it should get you authenticated and then redirect you to the page you want. I'm not an expert here, but I'm pretty sure that you can redirect to another long URL with parameters, which would let you "submit" a form or something similar.



回答2:

You can use the XMLRPC package to access wordpress

library(XMLRPC)
service <- "http://myblog.wordpress.com/xmlrpc.php"
xml.rpc(service, "wp.getUsersBlogs", "my@email", "secretpassword", .convert = TRUE)

The functions supported by this API are documented here. Using wp.uploadFile it might be possible to load photos into the server.