0

I am working on Canvas LMS and have access token. I need to create an user account using web service in PHP. I have tried to do it using CURL (post method) but getting an error in response. However GET is working fine.

Like if I need to get information about course etc, it's working fine but account creation not working using CURL (post). Below is my code.

$url = "https://xxxxx.com/api/v1/accounts/2/users";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt( $curl, CURLOPT_HTTPHEADER, array( 'Authorization: Bearer ' .$token ) );
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'POST'); 
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POSTFIELDS, array(
    'name' => 'vaue',
    'short_name' => 'value',
    'unique_id' => '1121',
));
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($curl, CURLOPT_ENCODING, "");
$curlData = curl_exec($curl);
curl_close($curl);

Error:

   stdClass Object
    (
    [errors] => Array
    (
    [0] => stdClass Object
    (
    [message] => An error occurred.
    [error_code] => internal_server_error
    )

)

[error_report_id] => 1124
)
halfer
  • 19,824
  • 17
  • 99
  • 186

2 Answers2

1

I resolved my issue. The reason of "internal server error" was not sending required fields. Here are the required fields if someone need to know.

'user[name]' => '',
'user[terms_of_use]' => 'true',
'pseudonym[unique_id]' => '',//i.e valid email
'pseudonym[send_confirmation]'=>'true'

Now my CURL request is working fine and I am able to create an account successfully.

halfer
  • 19,824
  • 17
  • 99
  • 186
0

It looks like the keys for your arguments are incorrect. They should be:

'user[name]' => 'vaue',
'user[short_name]' => 'value',
'pseudonym[unique_id]' => '1121',

You can find the docs for your canvas install at: "https://{your canvas domain}/doc/api/index.html" or if you are using cloud hosted canvas at "api.instructure.com"

Nathan
  • 196
  • 1
  • 4
  • Thanks Nathan for the suggestion but unfortunately ‘user[name]’ => 'vaue', not working . documentation for cloud hosted and self hosted is same and I already check. they just have.... POST /api/v1/accounts/:account_id/users – Irphan khan Aug 05 '15 at 21:10
  • Nathan, are you sure about the oblique apostrophes? – halfer Aug 06 '15 at 22:06