2

I would like to access with R to the content of a php website
http://centralgreen.com.sg/login.php?login=9-1501&password=mypassword

I have passed an example of login + password in the url, but I don't know how to press the login button through the url.

I would like to use the R package RCurl if possible.

RockScience
  • 17,932
  • 26
  • 89
  • 125
  • I am trying the example provided there http://stackoverflow.com/questions/2388974/how-do-i-use-cookies-with-rcurl – RockScience Jun 03 '11 at 07:36

2 Answers2

0

recently I've been having the same problem. In my case I solved it like this, using RCurl package (with a POST request).

In this code two requests are done one after the other. The fist one, is in order to gain a session cookie (start session in the server). The application I was calling expected the session to be started by the time it checked the login credentials (this won't happen if you send the form upfront). Otherwise some warning about not having cookie support was raised. This might be the case of the asker (though it was time ago)... or someone else's.

login <- function (xxxx_user, xxxx_pass) {

  url_login <- 'http://centralgreen.com.sg/login.php'

  curlhand <- getCurlHandle()

  curlSetOpt(
     .opts = list(cainfo = system.file("CurlSSL", "cacert.pem", package = "RCurl")),
      cookiefile = "cookies.txt",
      useragent = 'YOUR R-PACKAGE NAME',
      followlocation = TRUE,
      # might need this in case the server checks for the referer..
      httpheader = "Referer: http://centralgreen.com.sg",
      curl = curlhand)

  # (1) first call to initializate session. you get the session cookie
  getURL(url_login, curl = curlhand)

  params<- list( login = xxxx_user, password = xxxx_pass )
  # might need to add some other hidden form param in case there are..

  # (2) second call, sends the form, along with a session cookie
  html = postForm(url_login, 
    .params = params, 
    curl = curlhand, 
    style="POST")

  # ... perform some grep logic with 'html' to find out weather you are connected 
}

# you call the function...
login("yourusername", "yourpass")

The 'perform some grep logic' note takes care of the fact that since you are targeting a system not designed for this kind of programatical log in, it's not going to give you any nice hint on the result of the attempt ... so you might need to parse the raw html string you receive against some key sentences (eg: 'wrong username or password' ...)

hope it helps

javigzz
  • 942
  • 2
  • 11
  • 20
0

The form submits by post - you are using a get request at the moment by the looks of things, you need to use post.

My guess is that rcurl is based on curl - and I know curl can do this, so should be possible.

benlumley
  • 11,370
  • 2
  • 40
  • 39
  • 1
    I doubt this website will accept passwords with GET. If they do is unsecure – Elzo Valugi Jun 03 '11 at 07:25
  • 1
    I was suggesting use of post - have updated answer to be clearer – benlumley Jun 03 '11 at 07:27
  • I have tried r=postForm("http://centralgreen.com.sg/login.php", .params = list(login="9-1501", password="mypassword")) with no success (it returns the source of the original php page) – RockScience Jun 03 '11 at 07:33
  • Analyze a little the headers. The post is sending other params as well. login:9-1501 | password:acb | Button_DoLogin.x:38 | Button_DoLogin.y:8 | Button_DoLogin:Login. Maybe they are required. – Elzo Valugi Jun 03 '11 at 09:13