簡體   English   中英

將帶有PHP CURL的JSON發布到Tin Can API LRS

[英]Post JSON with PHP CURL to a Tin Can API LRS

對不起,我只能發布2個超鏈接,所以我將不得不刪除http://

背景我正試圖在這里轉換代碼: https//github.com/RusticiSoftware/TinCan_Prototypes/blob/92969623efebe2588fdbf723dd9f33165694970c/ClientPrototypes/StatementIssuer/StatementIssuer.java

進入PHP,特別是makeRequest函數。 此代碼將數據發布到Tin Can Compliant Learner Record Store。

我的PHP代碼的當前版本在這里:tincanapi.co.uk/wiki/tincanapi.co.uk:MediaWikiTinCan

所有應符合的Tin Can API規范如下:scorm.com/wp-content/assets/tincandocs/TinCanAPI.pdf

還有一個工作的java腳本函數,在這里以正確的格式發布數據(參見我認為的XHR_request函數): https//github.com/RusticiSoftware/TinCan_Prototypes/blob/92969623efebe2588fdbf723dd9f33165694970c/ClientPrototypes/GolfExample_TCAPI/scripts/TCDriver.js

我無法訪問我要發布的代碼或服務器,但最終結果應該是輸出:beta.projecttincan.com/ClientPrototypes/ReportSample/index.html

問題我正在嘗試使用Curl將數據作為JSON在PHP中發布。 Curl返回'false'但沒有錯誤,也沒有發布數據。

根據本網站上其他問題的建議,我嘗試在POSTFIELDS的開頭添加'json =',但由於Java和JavaScript版本確實有這個,我不確定這是對的。

任何人都可以建議我如何解決這個問題或者如何從卷曲中獲得有用的錯誤? 我的備份是將相關的JavaScript輸出到用戶的瀏覽器,但是PHP應該能夠做到這個服務器端嗎?

非常感謝任何幫助。

安德魯

至少有一件事是錯誤的:您不應該在Authorization標頭值上使用rawurlencode

考慮使用php流和json_encode()json_decode()代替。 以下代碼有效。

function fopen_request_json($data, $url)
{
    $streamopt = array(
        'ssl' => array(
            'verify-peer' => false,
        ),
        'http' => array(
            'method' => 'POST',
            'ignore_errors' => true,
            'header' =>  array(
                'Authorization: Basic VGVzdFVzZXI6cGFzc3dvcmQ=',
                'Content-Type: application/json',
                'Accept: application/json, */*; q=0.01',
            ),
            'content' => json_encode($data),
        ),
    );

    $context = stream_context_create($streamopt);
    $stream = fopen($url, 'rb', false, $context);
    $ret = stream_get_contents($stream);
    $meta = stream_get_meta_data($stream);
    if ($ret) {
        $ret = json_decode($ret);
    }
    return array($ret, $meta);
}

function make_request()
{
    $url = 'https://cloud.scorm.com/ScormEngineInterface/TCAPI/public/statements';

    $statements = array(
        array(
            'actor' => array(
                'name' => array('Example Name'),
                'mbox'  => array('mailto:example@example.com'),
                'objectType' => 'Person',
            ),
            'verb' => 'experienced',
            'object' => array(
                'objectType' => 'Activity',
                'id'=> 'http://www.thincanapi.co.uk/wiki/index.php?Main_Page',
                'definition' => array(
                    'name' => array('en-US'=>'TinCanAPI.co.uk-tincanapi.co.uk'),
                    'description' => array('en-US'=> 'TinCanAPI.co.uk-tincanapi.co.uk'),
                ),
            ),
        ),
    );
    return fopen_request_json($statements, $url);

}

list($resp, $meta) =  make_request();

var_export($resp); // Returned headers, including errors, are in $meta

我們現在已經發布了一個專門針對PHP的開源庫,它使用了與接受的答案類似的方法,但也完善了庫的其余部分。 看到:

http://rusticisoftware.github.io/TinCanPHP/

https://github.com/RusticiSoftware/TinCanPHP

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM