2011-08-02 13 views
0

こんにちは私はログインしてWebページをダウンロードする際にいくつか問題があります。 私がやっていることは私の学校のムードルサイトにログオンし、メインページを文字列で保存することです。パーサを使って特定の情報を取り出し、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にはwebserviceスタックが組み込まれています。hereの指示に従って有効にしてください。 Moodle 1.9ではプラグインを追加する必要があります。その中にはmodulesとpluginsディレクトリにいくつかのプラグインがあります。 OKTech Webservicesがおそらく最高です。

実際に最初のページからどのような情報が得られますか?

関連する問題