Skip to content

Commit

Permalink
Merge pull request #18 from kaushikindianic/00-setAccountPicture-enha…
Browse files Browse the repository at this point in the history
…ncement

Ehnace SetAccountPicture functionality
  • Loading branch information
joostfaassen committed Nov 1, 2019
2 parents e55118b + fc1fb19 commit a9cf33e
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 4 deletions.
8 changes: 4 additions & 4 deletions examples/account_picture.php
@@ -1,13 +1,13 @@
<?php

require_once('common.php');
require_once 'common.php';

try {
$accountName = 'qwe';
$filename = 'test.png';
$accountName = 'xxx';
$filename = base64_encode(file_get_contents('/image/path'));

$res = $client->setAccountPicture($accountName, $filename);
print_r($res);
} catch (Exception $e) {
echo "Exception " . $e->getMessage() . "\n";
echo 'Exception '.$e->getMessage()."\n";
}
74 changes: 74 additions & 0 deletions src/Client.php
Expand Up @@ -320,6 +320,8 @@ public function setAccountProperty($accountName, $propertyName, $propertyValue)

public function setAccountPicture($accountName, $filename)
{
return $this->uploadPhoto('/accounts/'.$accountName.'/setPicture', $filename);

$data = $this->getData('/accounts/'.$accountName.'/setPicture');

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

return $data;
}

public function uploadPhoto($uri, $file)
{
$fields = array();
$files['file'] = base64_decode($file);
$boundary = uniqid();
$delimiter = '-------------'.$boundary;

$postData = $this->buildDataFiles($boundary, $fields, $files);

$url = $this->baseUrl.$uri;
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_USERPWD => $this->username.':'.$this->password,
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => $postData,
CURLOPT_HTTPHEADER => array(
'Content-Type: multipart/form-data; boundary='.$delimiter,
'Content-Length: '.strlen($postData),
),
));

$json = curl_exec($ch);
$info = curl_getinfo($ch);
$code = $this->getStatusCode($ch);
if ($this->timeDataCollector) {
$this->timeDataCollector->stopMeasure('getData');
}
if (200 != $code) {
throw new RuntimeException('HTTP Status code: '.$code);
}
$data = @json_decode($json, true);
curl_close($ch);

return $data;
}

/**
* @return string file content and post data
*/
public function buildDataFiles($boundary, $fields, $files)
{
$data = '';
$eol = PHP_EOL;

$delimiter = '-------------'.$boundary;

foreach ($fields as $name => $content) {
$data .= '--'.$delimiter.$eol
.'Content-Disposition: form-data; name="'.$name.'"'.$eol.$eol
.$content.$eol;
}

foreach ($files as $name => $content) {
$data .= '--'.$delimiter.$eol
.'Content-Disposition: form-data; name="'.$name.'"; filename="'.$name.'"'.$eol
//. 'Content-Type: image/png'.$eol
.'Content-Transfer-Encoding: binary'.$eol
;

$data .= $eol;
$data .= $content.$eol;
}
$data .= '--'.$delimiter.'--'.$eol;

return $data;
}
}

0 comments on commit a9cf33e

Please sign in to comment.