我正在尝试更新D2L Valence中的个人资料信息。我已成功使用GET请求下载信息。但我不确定如何格式化我的请求以完成简单的更新。
在这种情况下,我已经下载了配置文件json块的副本并更改了昵称字段。不要介意在第一行调用d2l类。它获取json配置文件块,然后我改变它,如果我打印出来,那么,我得到
{"Nickname":"Johnny","Birthday":null,"HomeTown":"","Email":"","HomePage":"","HomePhone":null,"BusinessPhone":null,"MobilePhone":null,"FaxNumber":null,"Address1":null,"Address2":null,"City":null,"Province":null,"PostalCode":null,"Country":null,"Company":null,"JobTitle":"","HighSchool":null,"University":null,"Hobbies":"","FavMusic":"","FavTVShows":"","FavMovies":"","FavBooks":"","FavQuotations":"","FavWebSites":"","FutureGoals":"","FavMemory":"","SocialMediaUrls":[{"Name":"Facebook","Url":""},{"Name":"Twitter","Url":""},{"Name":"Google+","Url":""},{"Name":"LinkedIn","Url":""}]}
这是我的PHP
$output = d2l::get_user_profile($usercontext,"real_profile_id",true);
$output = str_replace("\"Nickname\":\"\"", "\"Nickname\":\"Johnny\"",$output);
$random_hash = md5(date('r', time()));
$url = $usercontext->createAuthenticatedUri("/d2l/api/lp/1.1/profile/real_profile_id","PUT");
$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=".$random_hash));
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$output);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_CAINFO, '/var/www/cacert.pem');
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
$response = curl_exec($ch);
print_r(curl_getinfo($ch))
从这段代码中,我得到了这个回复
Array
(
[url] => https://myurl.edu:443/d2l/api/lp/1.1/profile/myProfile?x_a=mykey&x_c=mykey&x_d=mykey&x_t=1349798248
[content_type] => text/html; charset=utf-8
[http_code] => 403
[header_size] => 218
[request_size] => 941
[filetime] => -1
[ssl_verify_result] => 20
[redirect_count] => 0
[total_time] => 0.171832
[namelookup_time] => 1.9E-5
[connect_time] => 0.033028
[pretransfer_time] => 0.105724
[size_upload] => 582
[size_download] => 13
[speed_download] => 75
[speed_upload] => 3387
[download_content_length] => 13
[upload_content_length] => 0
[starttransfer_time] => 0.171811
[redirect_time] => 0
[request_header] => POST /d2l/api/lp/1.1/profile/myProfile?x_a=mykey&x_c=mykey&x_d=mykey&x_t=1349798248 HTTP/1.1
Host: myurl.edu
Accept: */*
Content-Type: multipart/form-data; boundary=95b842a110e529794822be6870f2585c
Content-Length: 582
)
我可能错过了一些明显的东西。请告诉或指出我正确的方向。
答案 0 :(得分:0)
您已在后续评论中回答了自己的问题,但myProfile update route使用HTTP PUT而非POST:大多数Valence API通常都是这种情况(创建路由使用POST并更新路由使用PUT )。
答案 1 :(得分:0)
这是一段用于执行更新的PHP代码块,适用于需要它的任何其他人。
$ data是json
文本的字符串$ opContext是使用$ authContext-> createUserContext()创建的有效用户上下文。
$ url是您要发送到
的REST路径我确实意识到
存在问题curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
但我现在和它一起生活。
这是代码
$putData = fopen('php://temp/maxmemory:256000', 'w');
if (!$putData) {
die('could not open temp memory data');
}
fwrite($putData, $data);
fseek($putData, 0);
$url = $opContext->createAuthenticatedUri($url,"PUT");
$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: application/json"));
curl_setopt($ch, CURLOPT_PUT,true);
curl_setopt($ch, CURLOPT_INFILE,$putData);
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($data));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
$response = curl_exec($ch);
fclose($putData);
return $response;