Edit File: B2Client.inc
<?php namespace B2\Http; use GuzzleHttp\Client; use B2\Exceptions\B2Exception; use GuzzleHttp\Exception\ClientException; /** * Author: Shlomi * Date: Sep 3, 2017 6:04:19 PM * Founder: Shlomi Bazak (SBZsoft) (c) * File: B2Client.inc * * Description: * */ // No direct access. defined("__NSS__") or die("Restrected Access."); class B2Client extends Client{ const HTTP_STATUS_OK = 200; const METHOD_GET = "GET"; const METHOD_POST = "POST"; private $_auth; /** * * @param string $accountId * @param string $applicationKey */ public function __construct($accountId, $applicationKey){ parent::__construct(); $this->_auth = new Auth($accountId, $applicationKey, $this); } /** * * @param string $method One of B2Client::METHOD_* consts of this class * @param string $uri request url * @param array $options options of the Request * @see \GuzzleHttp\Client * @return mixed|array the response data */ public function _requestJson($method, $uri, $options){ try { $response = $this->request($method, $uri, $options); }catch(ClientException $e){ $decoded = json_decode($e->getResponse()->getBody(), true); throw B2Exception::genException($decoded); } $decoded = json_decode($response->getBody(), true); if($response->getStatusCode() !== self::HTTP_STATUS_OK) throw B2Exception::genException($decoded); return $decoded; } /** * * @param string $apiCall use one of B2Api consts * @param array $params the parametes listed in the B2Api consts documentation * @return mixed|array the response data */ public function requestApi($apiCall, $params){ $apiUrl = $this->_auth->getApiUrl() . $apiCall; $options = [ 'headers' => ['Authorization' => $this->_auth->getAuthToken()], 'json' => $params ]; return $this->_requestJson(self::METHOD_POST, $apiUrl, $options); } /** * * @param string $pathToFile the path on B2 servers including the bucket ie "BUCKET_NAME/PATH/TO/FILE.txt" * @param string $saveTo local path to save the downloaded file to * @return array data */ public function requestDownload($pathToFile, $saveTo=null){ $options = [ 'headers' => ['Authorization' => $this->_auth->getAuthToken()], 'sink' => $saveTo ]; $uri = sprintf('%s/file/%s', $this->getDownloadUrl(), $pathToFile); return $this->request(self::METHOD_GET, $uri, $options); } /** * Get the aaccount ID of B2 account connected * @return string */ public function getAccountId(){ return $this->_auth->getAccountId(); } /** * Get Download Url of B2 account connected * @return string */ public function getDownloadUrl() { return $this->_auth->getDownloadUrl(); } }