php網站,我想有R訪問一個PHP網站
http://centralgreen.com.sg/login.php?login=9-1501&password=mypassword登錄使用RCurl
我已經通過在URL登錄+密碼的一個例子的內容,但我不知道如何通過url按下登錄按鈕。
如果可能,我想使用R包RCurl。
php網站,我想有R訪問一個PHP網站
http://centralgreen.com.sg/login.php?login=9-1501&password=mypassword登錄使用RCurl
我已經通過在URL登錄+密碼的一個例子的內容,但我不知道如何通過url按下登錄按鈕。
如果可能,我想使用R包RCurl。
表單通過信息提交 - 此時您正在使用獲取請求的外觀,您需要使用信息。
我的猜測是rcurl基於curl - 我知道curl可以做到這一點,所以應該有可能。
我懷疑這個網站將接受與GET密碼。如果他們這樣做是不安全的 – 2011-06-03 07:25:48
我建議使用後 - 有更新的答案更清晰 – benlumley 2011-06-03 07:27:39
我試過r = postForm(「http://centralgreen.com.sg/login.php」,.params = list(login = 「9-1501」,password =「mypassword」))沒有成功(它返回原始php頁面的源代碼) – RockScience 2011-06-03 07:33:16
最近我一直有同樣的問題。在我的情況下,我使用RCurl包(帶POST請求)像這樣解決了這個問題。
在這段代碼中,兩個請求是一個接一個完成的。第一個,是爲了獲得會話cookie(在服務器中開始會話)。我所調用的應用程序期望在檢查登錄憑據時啓動會話(如果您預先發送表單,則不會發生這種情況)。否則會引發一些關於不支持cookie的警告。這可能是提問者的情況(雖然是時間以前)......或者是其他人的情況。
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")
的「執行一些grep的邏輯」說明考慮的事實,因爲你的目標系統沒有設計這種programatical登錄,它不會給你的結果,任何好的暗示的護理嘗試......所以你可能需要解析您收到對一些關鍵句子原始HTML的字符串(如:「錯誤的用戶名或密碼」 ...)
希望它有助於
我想提供的示例那裏http://stackoverflow.com/questions/2388974/how-do-i-use-cookies-with-rcurl – RockScience 2011-06-03 07:36:44