1

我正在尝试使用路径

POST /d2l/api/lp/(D2LVERSION: version)/users/ 

我遇到了一个错误。我希望文档中没有我遗漏的东西,因为据我所知,这应该可行。

HTTP/1.1 400 Bad request
Cache-Control: private
Content-Length: 0
Server: Microsoft-IIS/7.5
X-XSS-Protection: 0
X-Powered-By: ASP.NET
Date: Wed, 23 Jan 2013 18:37:07 GMT

我发送的数据如下所示:

{
    "OrgDefinedId":"190006002",
    "FirstName":"Jesty",
    "MiddleName":"Q",
    "LastName":"McTest",
    "ExternalEmail":"privateemail@gmail.com",
    "UserName":"190006002",
    "RoleId":115,
    "IsActive":true,
    "SendCreationEmail":true
}

我的 PHP 代码现在看起来像这样:

    $random_hash = "xxBOUNDARYxx";
    $request ="--".$random_hash.
              "\r\nContent-Type: application/json\r\n\r\n$data\r\n\r\n--".
              $random_hash;

    $length=strlen($request);
    $url = $opContext->createAuthenticatedUri($url,"POST");

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url); 
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLINFO_HEADER_OUT, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("HTTP/1.1", "Content-Type: multipart/form-data; boundary=xxBOUNDARYxx","Content-Length:".$length));
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST,true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
    return curl_exec($ch);      
4

1 回答 1

1

好的,经过几个小时的修补,我想通了。我发布的原始代码基于通过 API 发布个人资料图像的代码。仅发布 JSON 的代码要简单得多。这是一个通用的 PHP 函数,用于通过 POST 方法将 JSON 发送到 Valence

/*
*  $opContext is your user context object
*  $url is the route
*  $data is the well formed JSON in a string ( use json_encode() ) 
*  $ct is the count of json fields
*/

static function basic_post($opContext,$url,$data,$ct){
    $url = $opContext->createAuthenticatedUri($url,"POST"); 
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url); 
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLINFO_HEADER_OUT, true);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST,$ct);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
    return curl_exec($ch);  
}
于 2013-01-24T15:28:50.373 回答