Skip to content

Commit fc1fb19

Browse files
author
kaushik.prajapati
committed
Ehnace SetAccountPicture functionality
1 parent 36036d5 commit fc1fb19

2 files changed

Lines changed: 78 additions & 4 deletions

File tree

examples/account_picture.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<?php
22

3-
require_once('common.php');
3+
require_once 'common.php';
44

55
try {
6-
$accountName = 'qwe';
7-
$filename = 'test.png';
6+
$accountName = 'xxx';
7+
$filename = base64_encode(file_get_contents('/image/path'));
88

99
$res = $client->setAccountPicture($accountName, $filename);
1010
print_r($res);
1111
} catch (Exception $e) {
12-
echo "Exception " . $e->getMessage() . "\n";
12+
echo 'Exception '.$e->getMessage()."\n";
1313
}

src/Client.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,8 @@ public function setAccountProperty($accountName, $propertyName, $propertyValue)
320320

321321
public function setAccountPicture($accountName, $filename)
322322
{
323+
return $this->uploadPhoto('/accounts/'.$accountName.'/setPicture', $filename);
324+
323325
$data = $this->getData('/accounts/'.$accountName.'/setPicture');
324326

325327
return $data;
@@ -412,4 +414,76 @@ public function sendPushMessage($username, $message, $data = [])
412414

413415
return $data;
414416
}
417+
418+
public function uploadPhoto($uri, $file)
419+
{
420+
$fields = array();
421+
$files['file'] = base64_decode($file);
422+
$boundary = uniqid();
423+
$delimiter = '-------------'.$boundary;
424+
425+
$postData = $this->buildDataFiles($boundary, $fields, $files);
426+
427+
$url = $this->baseUrl.$uri;
428+
$ch = curl_init();
429+
curl_setopt_array($ch, array(
430+
CURLOPT_USERPWD => $this->username.':'.$this->password,
431+
CURLOPT_URL => $url,
432+
CURLOPT_RETURNTRANSFER => 1,
433+
CURLOPT_MAXREDIRS => 10,
434+
CURLOPT_TIMEOUT => 30,
435+
CURLOPT_CUSTOMREQUEST => 'POST',
436+
CURLOPT_POST => 1,
437+
CURLOPT_POSTFIELDS => $postData,
438+
CURLOPT_HTTPHEADER => array(
439+
'Content-Type: multipart/form-data; boundary='.$delimiter,
440+
'Content-Length: '.strlen($postData),
441+
),
442+
));
443+
444+
$json = curl_exec($ch);
445+
$info = curl_getinfo($ch);
446+
$code = $this->getStatusCode($ch);
447+
if ($this->timeDataCollector) {
448+
$this->timeDataCollector->stopMeasure('getData');
449+
}
450+
if (200 != $code) {
451+
throw new RuntimeException('HTTP Status code: '.$code);
452+
}
453+
$data = @json_decode($json, true);
454+
curl_close($ch);
455+
456+
return $data;
457+
}
458+
459+
/**
460+
* @return string file content and post data
461+
*/
462+
public function buildDataFiles($boundary, $fields, $files)
463+
{
464+
$data = '';
465+
$eol = PHP_EOL;
466+
467+
$delimiter = '-------------'.$boundary;
468+
469+
foreach ($fields as $name => $content) {
470+
$data .= '--'.$delimiter.$eol
471+
.'Content-Disposition: form-data; name="'.$name.'"'.$eol.$eol
472+
.$content.$eol;
473+
}
474+
475+
foreach ($files as $name => $content) {
476+
$data .= '--'.$delimiter.$eol
477+
.'Content-Disposition: form-data; name="'.$name.'"; filename="'.$name.'"'.$eol
478+
//. 'Content-Type: image/png'.$eol
479+
.'Content-Transfer-Encoding: binary'.$eol
480+
;
481+
482+
$data .= $eol;
483+
$data .= $content.$eol;
484+
}
485+
$data .= '--'.$delimiter.'--'.$eol;
486+
487+
return $data;
488+
}
415489
}

0 commit comments

Comments
 (0)