2011-08-02 33 views
0

嗨我在登錄和下載網頁時遇到了一些麻煩。 我想要做的是登錄到我學校的moodle網站,並將主頁保存在一個字符串中,這樣我就可以使用解析器來挑選某些信息並構建一個gwt應用程序。使用cURL下載moodle主頁

這是我到目前爲止有: 在moodleLogin.html

<html> 
<body> 

    <form name="lms" action="moodlephp.php" method="post"> 
    <input type="text" name="username" id="username" /><br/> 
    <input type="password" name="password" id="password" /> 
    <input type="hidden" name="domain" id="domain" value="students.ltu.edu.au" /> 
    <input type="submit" name="Login" id="Login" /> 
    </form> 
    <div id="test"></div> 
</body> 
</html> 

在moodlephp.php

<?php 

    $username = $_POST["username"]; 
    $password = $_POST["password"]; 
    $domain = $_POST["domain"]; 

    $postfields = array("username" => $username, "password" => $password, "domain" => $domain); 
    $working_folder = "\cookies"; 

    $cookiefile = tempnam($working_folder, "cookies"); 
    $urlbase = "https://www.latrobe.edu.au/lms"; 
    $agent = $_SERVER['HTTP_USER_AGENT']; 
    $debug = ""; 
    $debughandle = ""; 


    // get session cookies to set up the Moodle interaction 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_USERAGENT, $agent); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile); 
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile); 
    curl_setopt($ch, CURLOPT_VERBOSE, $debug); 
    if ($debug) curl_setopt($ch, CURLOPT_STDERR, $debughandle); 

    curl_setopt($ch, CURLOPT_URL, $urlbase . "/login/index.php"); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
    $result = curl_exec ($ch); 
    curl_close ($ch); 


    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_USERAGENT, $agent); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile); 
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile); 
    if(!$cookiefile) echo('empty'); 
    else echo($cookiefile); 
    curl_setopt($ch, CURLOPT_VERBOSE, $debug); 
    if ($debug) curl_setopt($ch, CURLOPT_STDERR, $debughandle); 

    curl_setopt($ch, CURLOPT_URL, $urlbase . '/login/index.php'); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($postfields)); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); 
    curl_setopt($ch, CURLOPT_HEADER, 1); 
    $result = curl_exec ($ch); 
    curl_close ($ch); 

    if (!$result || !preg_match("/HTTP\/1.1 303 See Other/", $result)) 
    { 
    unlink($cookiefile); 
    header("HTTP/1.0 403 Forbidden"); 
    die("Username/password incorrect.\n"); 

    } 

    // get session key 

    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_USERAGENT, $agent); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookiefile); 
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookiefile); 
    curl_setopt($ch, CURLOPT_VERBOSE, $debug); 
    if ($debug) curl_setopt($ch, CURLOPT_STDERR, $debughandle); 

    curl_setopt($ch, CURLOPT_URL, $urlbase . "/question/import.php?courseid=" . $courseid); 
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); 
    $result = curl_exec ($ch); 
    curl_close ($ch); 

    if (!preg_match("/sesskey=(\w*)/", $result, $matches)) 
    { 
    unlink($cookiefile); 
    header("HTTP/1.0 500 Internal Server Error"); 
    die("Could not determine sesskey.\n"); 
    } 

    $sesskey = $matches[1]; 

    echo $result; 
?> 

任何幫助將不勝感激。目前我收到的是「用戶名/密碼不正確」。

我一直在嘗試按照這裏發佈的代碼 - >http://moodle.org/mod/forum/discuss.php?d=153580 在此先感謝。

回答

0

我想我明白你在做什麼,但這是一種非常低效的方式。更好的方法是使用Web服務使用XMLRPC或SOAP進行連接(使用PHP的內置庫非常容易)並以這種方式請求所需的信息。 Moodle 2.0內置了一個web服務棧 - 只需按照說明here啓用即可。 Moodle 1.9將需要你添加一個插件,其中有幾個模塊和插件目錄。 OKTech Webservices也許是最好的。

你真的想從首頁得到什麼信息?