使用Unirest

时间:2017-12-20 15:57:56

标签: php moodle unirest moodle-api

我想与Moodle 2.9的安装进行沟通。

以下示例客户端稍作修改: https://github.com/moodlehq/sample-ws-clients/blob/master/PHP-REST/client.php 使用Unirest而不是Curl和JSON而不是XML:

        $token = 'd1c74d6466daaaaad59b5d99906bfc84';
        $domainname = 'http://moodle.example.com';
        $functionname = 'core_user_create_users';
        // REST RETURNED VALUES FORMAT
        $restformat = 'json'; 

        $user1 = new \stdClass();
        $user1->username = 'testusername1';
        $user1->password = 'testpassword1';
        $user1->firstname = 'testfirstname1';
        $user1->lastname = 'testlastname1';
        $user1->email = 'testemail1@moodle.com';
        $user1->auth = 'manual';
        $user1->idnumber = 'testidnumber1';
        $user1->lang = 'en';
        $user1->theme = 'standard';
        $user1->timezone = '-12.5';
        $user1->mailformat = 0;
        $user1->description = 'Hello World!';
        $user1->city = 'testcity1';
        $user1->country = 'au';
        $preferencename1 = 'preference1';
        $preferencename2 = 'preference2';
        $user1->preferences = array(
            array('type' => $preferencename1, 'value' => 'preferencevalue1'),
            array('type' => $preferencename2, 'value' => 'preferencevalue2'));

        $user2 = new \stdClass();
        $user2->username = 'testusername2';
        $user2->password = 'testpassword2';
        $user2->firstname = 'testfirstname2';
        $user2->lastname = 'testlastname2';
        $user2->email = 'testemail2@moodle.com';
        $user2->timezone = 'Pacific/Port_Moresby';
        $users = array($user1, $user2);
        $params = array('users' => $users);
        /// REST CALL

        $serverurl = $domainname . '/webservice/rest/server.php' . '?wstoken=' . $token . '&wsfunction=' . $functionname;

        //if rest format == 'xml', then we do not add the param for backward compatibility with Moodle < 2.2
        $restformat = ($restformat == 'json') ? '&moodlewsrestformat=' . $restformat : '';
        $headers = array('Accept' => 'application/json');
        $response = UnirestRequest::post($serverurl . $restformat, $headers, json_encode($params));

执行时我收到错误:

  

&#34;注意:数组转换为字符串&#34;

据称,由于参数在体内传播。所以,我想我必须在发送之前序列化身体,但是当我尝试时:

$response = UnirestRequest::post($serverurl . $restformat, $headers, json_encode($params));

我从Moodle回来了:

  

{&#34;例外&#34;:&#34; invalid_parameter_exception&#34;&#34;错误代码&#34;:&#34;参数无效&#34;&#34;消息&#34;:& #34; Detectado   valor de par \ u00e1metro no v \ u00e1lido&#34;,&#34; debuginfo&#34;:&#34;缺少必需的   单一结构中的关键:用户&#34;}◀&#34;

必须有一些我不了解POST请求必须如何的样子。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

Moodle希望帖子的主体能够进行URL编码,因此必须构建您的身体 使用http_build_query($params)(或任何其他方法对数据进行编码),例如:

 $convertedpostdata = implode('&', $params);//where $params is  an array

至于为什么,我真的不记得,我记得前一段时间挣扎着实施,你可以查看  [your_moodle_installation.com]/admin/webservice/documentation.php了解更多文档,此外您还可以参考我的例子:

https://gist.github.com/Scott972/5d9e9495c1397a2ad728b66288ce1d42