Error uploading user profile picture from webservice

Error uploading user profile picture from webservice

by Deepak Kumar -
Number of replies: 2

Hello All 

Is there anyone who can help  regarding the error 

Here is my code 

if(isset($_POST['submit'])){

     $imagename = $_FILES["image"]["name"]; 

   $params = array('component' => 'user','filearea' => 'draft', 'itemid' => 0,'filename' => $imagename,'filepath' => '/','filecontent' => base64_encode($imagename), 'contextlevel' => 'user', 'instanceid' =>2);    

$ch = curl_init();

curl_setopt($ch, CURLOPT_HEADER, 0); 

curl_setopt($ch, CURLOPT_VERBOSE, 0); 

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_URL, $url . '/webservice/rest/server.php?wstoken='.$token.'&wsfunction=core_files_upload&moodlewsrestformat=json');

curl_setopt($ch, CURLOPT_POST, true);

curl_setopt($ch, CURLOPT_POSTFIELDS, $params);

$response = curl_exec($ch);

$result=json_decode($response, true);

//echo $result['itemid'];

//print_r($result);


$params2 = array( 'draftitemid' => $result['itemid'],'userid' => 385217);

$ch2 = curl_init(); 

curl_setopt($ch2, CURLOPT_HEADER, 0);

curl_setopt($ch2, CURLOPT_VERBOSE, 0); 

curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch2, CURLOPT_URL, $url . '/webservice/rest/server.php?wstoken='.$token.'&wsfunction=core_user_update_picture&moodlewsrestformat=json');

curl_setopt($ch2, CURLOPT_POST, true);

curl_setopt($ch2, CURLOPT_POSTFIELDS, $params2);

$response2 = curl_exec($ch2);

print_r($response2);

 }

Output showing this 
{"success":false,"warnings":[]}

Thanks 
Deepak

Average of ratings: -
In reply to Deepak Kumar

Re: Error uploading user profile picture from webservice

by Dominique Palumbo -
Picture of Particularly helpful Moodlers Picture of Plugin developers
Hi,

which error do you get and what is the size of the image ?
And do you initialise $url ?

And if you use php 7.x
some tips here

https://stackoverflow.com/questions/3433542/curl-php-send-image

Dominique.
In reply to Dominique Palumbo

Re: Error uploading user profile picture from webservice

by Deepak Kumar -
Hello Dominique

Thanks for your reply,

Yes I initiated the $url and $token above

error is nothing just return this
{"success":false,"warnings":[]}

It happens every time whether the image size is in kb or anything. So I think size doesn't matter.