Php 使用LTI将等级数据发送回LMS

Php 使用LTI将等级数据发送回LMS,php,lti,Php,Lti,我创建了一个支持LTI的工具,在将数据发送回LMS(如Desire2Learn)时遇到了一些问题。仅根据我的理解,您就生成了一个包含成绩项目本身的XML负载,并使用cURL通过POST将其发送回LMS。我遇到的问题是,我的$result变量返回false,我现在无法理解它。这是我的密码: session_start(); require_once 'blti/blti.php'; require_once 'blti/blti_util.php'; define('OAUTH_CONSUME

我创建了一个支持LTI的工具,在将数据发送回LMS(如Desire2Learn)时遇到了一些问题。仅根据我的理解,您就生成了一个包含成绩项目本身的XML负载,并使用cURL通过POST将其发送回LMS。我遇到的问题是,我的$result变量返回false,我现在无法理解它。这是我的密码:

session_start();

require_once 'blti/blti.php';
require_once 'blti/blti_util.php';

define('OAUTH_CONSUMER_KEY', 'key');
define('OAUTH_CONSUMER_SECRET', 'secret');

$blti = new BLTI(OAUTH_CONSUMER_SECRET, true, false);
if ($blti->valid) {
    $_SESSION['lis_outcome_service_url'] = $_REQUEST['lis_outcome_service_url'];
    $_SESSION['lis_result_sourcedid']    = $_REQUEST['lis_result_sourcedid'];
    $_SESSION['lis_person_name_given']   = $_REQUEST['lis_person_name_given'];
    $_SESSION['oauth_consumer_key']      = $_REQUEST['oauth_consumer_key'];
    $_SESSION['oauth_consumer_secret']   = OAUTH_CONSUMER_SECRET;

    sendGradeBackToLMS();
}else {
    exit($blti->message);
}

function sendGradeBackToLMS() {
    $ch = curl_init();
    if (!$ch) { exit('curl is not supported!'); }

    $url = 'http://localhost/endpoint.php';

    $xmldata = getPOXRequest();
    $xml = str_replace(
        array('MESSAGE_IDENTIFIER', 'SOURCEDID', 'GRADE'),
        array(uniqid(), $_SESSION['lis_result_sourcedid'], 0.75),
        $xmldata);
    $request_headers = array();
    $request_headers[] = 'User-Agent: '. $_SERVER['HTTP_USER_AGENT'];
    $request_headers[] = 'Accept:     text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';

    curl_setopt_array($ch, array(
        CURLOPT_URL => $url,
        CURLOPT_HTTPHEADER => $request_headers,
        CURLOPT_POST => true,
        CURLOPT_POSTFIELDS => $xml
    ));

    $results = curl_exec($ch);
    curl_close($ch);

    echo $results;
}

function getPOXRequest() {
    return <<<XML
    <?xml version = "1.0" encoding = "UTF-8"?>
    <imsx_POXEnvelopeRequest xmlns = "http://www.imsglobal.org/services/ltiv1p1/xsd/imsoms_v1p0">
      <imsx_POXHeader>
        <imsx_POXRequestHeaderInfo>
          <imsx_version>V1.0</imsx_version>
          <imsx_messageIdentifier>MESSAGE_IDENTIFIER</imsx_messageIdentifier>
        </imsx_POXRequestHeaderInfo>
      </imsx_POXHeader>
      <imsx_POXBody>
        <replaceResultRequest>
          <resultRecord>
            <sourcedGUID>
              <sourcedId>SOURCEDID</sourcedId>
            </sourcedGUID>
            <result>
              <resultScore>
                <language>en</language>
                <textString>GRADE</textString>
              </resultScore>
            </result>
          </resultRecord>
        </replaceResultRequest>
      </imsx_POXBody>
    </imsx_POXEnvelopeRequest>
XML;
}
总结一下,$blti是一个实例,用于检查它是否是有效的基本LTI启动。如果这是真的,我将通过将LTI请求变量存储到会话变量中来初始化会话。接下来,在sendGradeBackToLMS函数中,我将包含grade项的XML数据块发送到$url中给定的路径

任何形式的输入都会有帮助! 谢谢

看看这个


git clone

@epipawin感谢您的回复。然而,我不确定你放在这里的一些东西。1.在第3行中,什么是ims blti/OAuthBody.php?第二,在第15行更改$sourcedid有什么意义?谢谢
Try this code,

require_once("ims-blti/OAuthBody.php");

if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
 error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);
} else { 
 error_reporting(E_ALL & ~E_WARNING & ~E_NOTICE);
}

$method="POST";
$oauth_consumer_secret = $_REQUEST['secret'];

$sourcedid = $_REQUEST['lis_result_sourcedid'];
if (get_magic_quotes_gpc()) $sourcedid = stripslashes($sourcedid);
$oauth_consumer_key = $_REQUEST['key'];
$endpoint = $_REQUEST['lis_outcome_service_url'];
$content_type = "application/xml";

$body = '<?xml version = "1.0" encoding = "UTF-8"?>  
<imsx_POXEnvelopeRequest xmlns = "http://www.imsglobal.org/lis/oms1p0/pox">      
    <imsx_POXHeader>         
        <imsx_POXRequestHeaderInfo>            
            <imsx_version>V1.0</imsx_version>  
            <imsx_messageIdentifier>MESSAGE</imsx_messageIdentifier>         
        </imsx_POXRequestHeaderInfo>      
    </imsx_POXHeader>      
    <imsx_POXBody>         
        <OPERATION>            
            <resultRecord>
                <sourcedGUID>
                    <sourcedId>SOURCEDID</sourcedId>
                </sourcedGUID>
                <result>
                    <resultScore>
                        <language>en-us</language>
                        <textString>GRADE</textString>
                    </resultScore>
                </result>
            </resultRecord>       
        </OPERATION>      
    </imsx_POXBody>   
</imsx_POXEnvelopeRequest>';

if (isset($_REQUEST['grade'])) {
    $operation = 'replaceResultRequest';
    $postBody = str_replace(
    array('SOURCEDID', 'GRADE', 'OPERATION','MESSAGE'), 
    array($sourcedid, $_REQUEST['grade'], $operation, uniqid()),
    $body);
} else {
    exit();
}

$response = sendOAuthBodyPOST($method, $endpoint, $oauth_consumer_key, $oauth_consumer_secret, $content_type, $postBody);