Skip to content

Instantly share code, notes, and snippets.

@devloe
Created March 12, 2018 16:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save devloe/6b3746ff1b2cdfcc6c7caf539e0ed55f to your computer and use it in GitHub Desktop.
Save devloe/6b3746ff1b2cdfcc6c7caf539e0ed55f to your computer and use it in GitHub Desktop.
<?php
public static function upload($httpRequest, $filedata,$filesize, $filename){
$authToken = Helper::getAuthToken($httpRequest);
$userId = Helper::getUserId($httpRequest);
// if (!is_null($authToken)&&!is_null($userId)){
$method = '?externUserId=' . $userId . '&authToken=' . $authToken .'&type=ATTACHMENT&filename='.$filename;
// } else {
// $method = '/'.$service.'?externUserId='.$request->getDeviceId();
// }
if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
$navLang = strtolower(substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2));
$navLang = Helper::getAppLanguage($httpRequest);
}
$request_url = config('mediktor.IMAGE_UPLOAD_URL').$method;
$fileContents = fopen($filedata,'rb');
$session = curl_init($request_url);
$apiUser = Helper::getSpecificConfig($httpRequest, 'mediktor.API_USER');
$apiPassword = Helper::getSpecificConfig($httpRequest, 'mediktor.API_PASSWORD');
if(!config('mediktor.API_SSL_CHECKS')){
curl_setopt($session, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($session, CURLOPT_SSL_VERIFYPEER, false);
}
curl_setopt ($session, CURLOPT_PUT, true);
curl_setopt ($session, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($session, CURLOPT_HEADER, true);
curl_setopt ($session, CURLOPT_INFILESIZE, $filesize);
curl_setopt ($session, CURLOPT_INFILE, $fileContents);
$header = array('Connection: keep-alive','Content-Type: application/octet-stream','Authorization: Basic '.base64_encode($apiUser.':'.$apiPassword));
curl_setopt($session, CURLOPT_HTTPHEADER, $header);
curl_setopt($session, CURLOPT_FAILONERROR, true);
$response = curl_exec($session);
Helper::makeLog('RESPONSE ('.$method.') : '.$response);
$http_status = curl_getinfo($session, CURLINFO_HTTP_CODE);
$curl_errno= curl_errno($session);
if($curl_errno){
$errorCode = 'CURL'.$curl_errno;
$errorDesc = curl_error($session);
throw MediktorException::fromCodeAndDescription($errorCode,$errorDesc);
// trigger_error('CURL Error ('.$curl_errno.'):' . curl_error($session),E_USER_ERROR);
}
curl_close($session);
if(!$response || $http_status != 200){
//Log error al conectar
// Log::warning('Error de connexió a '. get_called_class()."->".__FUNCTION__.' DeviceId: '. $request->getDeviceId());
throw MediktorException::fromCodeAndDescription('MW01',\App\Libraries\Helper::getText('generics.error_generic'));
$errorDescription = 'Algo ha pasado. Hable con el departamento de sistemas.';
$response = Helper::getResponseErrorTemplateWithAllVariables($httpRequest, $errorDescription);
return $response;
} else {
return $http_status;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment