Search code examples
phpdesire2learn

desire2learn simple PUT update in PHP


I am trying to update profile information in D2L Valence. I have been successful in downloading information using GET requests. But I am unsure how to format my requests in order to complete a simple update.

In this case I have downloaded a copy of a profile json block and changed the nickname field. Don't mind the d2l class called on the first line. It gets the json profile block, then I alter it and if I print it out then, I get

{"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":""}]}

This is my 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))              

From this code, I get this response

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


)

I've probably missed something obvious. Please advise or point me in the right direction.


Solution

  • Here is a block of PHP code that works for doing an update, for anyone else who needs it.

    $data is a string the text of the json

    $opContext is a valid user context created with $authContext->createUserContext().

    $url is the REST path you want to send to

    I do realize there is an issue with

    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    

    But I am living with it for the moment.

    Here is the code

    $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;