Upload user picture with API and PHP

Hello everyone,

I m trying to upload profile picture for a specific user with file_get_contents() and i have also tried with CURL.

I tried everything and i keep getting error 400.

There’s my code:

$token = “xxxxxxxxxxxxxxxxxxxxxxxxxxx”;
$userid = ‘xxxxxxxxxxxxx’;
$picpath = file_get_contents(“G:/wamp/www/images/awesompicture.png”);
$token = urlencode($token);
$api_url = ‘https://api.zoom.us/v2/’;
$ressource = ‘users/’ . $userid . ‘/picture’;
$request_url = $api_url . $ressource;

define(‘MULTIPART_BOUNDARY’, ‘--------------------------’ . microtime(true));
$header = ‘Content-Type: multipart/form-data; boundary=’ . MULTIPART_BOUNDARY;
$data = array(
“pic_file” => $picpath
);
$tableau = json_encode($data);
$context = stream_context_create(array(
‘http’ => array(
‘method’ => ‘POST’,
‘header’ => 'Authorization: Bearer ’ . “$token”,
$header,
‘content’ => $data
)
));
file_get_contents($request_url, false, $context);

Do you guys know what i’m missing? Thanks in advance.

Have a great day

Hey @lelkamel,

Is there an error message returned with the 400 error?

Thanks,
Tommy

Hello Tommy,

I solved this issue with Curl.
the PHP Code generated by the API doesnt work.
There’s my woking version:

function postavatar($token, $userid, $image, $imagename){
$fields = [
‘pic_file’ => new \CurlFile($image, ‘image/png’, $imagename)
];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => “https://api.zoom.us/v2/users/$userid/picture”,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => “”,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => “POST”,
CURLOPT_POSTFIELDS => $fields ,
CURLOPT_HTTPHEADER => array(
“authorization: Bearer $token”,
“content-type: multipart/form-data”
),
));

$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
  return "NONE";
} else {
  return $response;
}

}

I hope it will help people whot got the same issue

1 Like

Hey @lelkamel, happy to hear you got it working! :slight_smile:

Thanks for posting the solution too!

Thanks,
Tommy