【问题标题】:Post JSON with PHP CURL to a Tin Can API LRS将带有 PHP CURL 的 JSON 发布到 Tin Can API LRS
【发布时间】:2012-05-22 06:11:36
【问题描述】:

抱歉,我只能发布 2 个超链接,所以我将不得不删除 http://

背景 我正在尝试在这里转换代码:https://github.com/RusticiSoftware/TinCan_Prototypes/blob/92969623efebe2588fdbf723dd9f33165694970c/ClientPrototypes/StatementIssuer/StatementIssuer.java

进入 PHP,特别是 makeRequest 函数。此代码将数据发布到符合锡罐标准的学习者记录存储。

我的 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 在 PHP 中将数据作为 JSON 发布。 Curl 返回 'false' 但没有错误,也没有发布数据。

根据本网站上其他问题的建议,我尝试在 POSTFIELDS 的开头添加 'json=',但由于 Java 和 JavaScript 版本确实有这个,我不确定这是否正确。

谁能建议我如何解决这个问题或者如何从 curl 中得到有用的错误?我的备份是把相关的 JavaScript 输出到用户的浏览器,但是 PHP 应该可以做这个服务器端吗?

非常感谢您的帮助。

安德鲁

【问题讨论】:

    标签: php json post curl


    【解决方案1】:

    至少有一件事是错误的:您不应该在 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
    

    【讨论】:

    • 哇,谢谢你——你已经远远超出了我在这里的要求或预期。从表面上看,你从头开始写了我需要的东西。我可以按原样使用您的代码(带有归属地)吗?
    • 是的,您可以,尽管从您的班级的外观来看,我怀疑您是否能够原封不动地放弃它。这比阅读、测试和调试代码要容易得多。卷曲是……痛苦的。
    • 一些小调整,但不多。这是您在我的类文件中工作的代码:tincanapi.co.uk/wiki/tincanapi.co.uk:MediaWikiTinCan#Code
    【解决方案2】:

    我们现在发布了一个专门用于 PHP 的开源库,它使用与公认答案类似的方法,但也完善了库的其余部分。见:

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

    https://github.com/RusticiSoftware/TinCanPHP

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-06-03
      • 1970-01-01
      • 2021-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-09
      相关资源
      最近更新 更多