/** * Makes a call to a Cloud function * * @param string $name Cloud function name * @param array $data Parameters to pass * @param boolean $useMasterKey Whether to use the Master Key * * @return mixed */ public static function run($name, $data = array(), $useMasterKey = false) { $sessionToken = null; if (AVUser::getCurrentUser()) { $sessionToken = AVUser::getCurrentUser()->getSessionToken(); } $response = AVClient::_request('POST', '/functions/' . $name, $sessionToken, json_encode(AVClient::_encode($data, null, false)), $useMasterKey); return AVClient::_decode($response['result']); }
/** * Sends a push notification. * * @param array $data The data of the push notification. Valid fields * are: * channels - An Array of channels to push to. * push_time - A Date object for when to send the push. * expiration_time - A Date object for when to expire * the push. * expiration_interval - The seconds from now to expire the push. * where - A AVQuery over AVInstallation that is used to match * a set of installations to push to. * data - The data to send as part of the push * @param boolean $useMasterKey Whether to use the Master Key for the request * * @throws \Exception, AVException * @return mixed */ public static function send($data, $useMasterKey = false) { if (isset($data['expiration_time']) && isset($data['expiration_interval'])) { throw new \Exception('Both expiration_time and expiration_interval can\'t be set.'); } if (isset($data['where'])) { if ($data['where'] instanceof AVQuery) { $data['where'] = $data['where']->_getOptions()['where']; } else { throw new \Exception('Where parameter for Avos Push must be of type AVQuery'); } } if (isset($data['push_time'])) { //Local push date format is different from iso format generally used in Avos //Schedule does not work if date format not correct $data['push_time'] = AVClient::getLocalPushDateFormat($data['push_time']); } if (isset($data['expiration_time'])) { $data['expiration_time'] = AVClient::_encode($data['expiration_time'], false)['iso']; } return AVClient::_request('POST', '/push', null, json_encode($data), $useMasterKey); }
/** * Execute a find query and return the results. * * @param boolean $useMasterKey * * @return array */ public function find($useMasterKey = false) { $sessionToken = null; if (AVUser::getCurrentUser()) { $sessionToken = AVUser::getCurrentUser()->getSessionToken(); } $queryString = $this->buildQueryString($this->_getOptions()); $result = AVClient::_request('GET', '/classes/' . $this->className . '?' . $queryString, $sessionToken, null, $useMasterKey); $output = array(); foreach ($result['results'] as $row) { $obj = AVObject::create($this->className, $row['objectId']); $obj->_mergeAfterFetchWithSelectedKeys($row, $this->selectedKeys); $output[] = $obj; } return $output; }
private function upload() { $fileParts = explode('.', $this->getName()); $extension = array_pop($fileParts); $mimeType = $this->mimeType ?: $this->getMimeTypeForExtension($extension); $headers = AVClient::_getRequestHeaders(null, false); $url = AVClient::HOST_NAME . '/files/' . $this->getName(); $rest = curl_init(); curl_setopt($rest, CURLOPT_URL, $url); curl_setopt($rest, CURLOPT_RETURNTRANSFER, 1); curl_setopt($rest, CURLOPT_BINARYTRANSFER, 1); $headers[] = 'Content-Type: ' . $mimeType; curl_setopt($rest, CURLOPT_POST, 1); curl_setopt($rest, CURLOPT_POSTFIELDS, $this->getData()); curl_setopt($rest, CURLOPT_HTTPHEADER, $headers); $response = curl_exec($rest); $contentType = curl_getinfo($rest, CURLINFO_CONTENT_TYPE); if (curl_errno($rest)) { throw new AVException(curl_error($rest), curl_errno($rest)); } curl_close($rest); if (strpos($contentType, 'text/html') !== false) { throw new AVException('Bad Request', -1); } $decoded = json_decode($response, true); if (isset($decoded['error'])) { throw new AVException($decoded['error'], isset($decoded['code']) ? $decoded['code'] : 0); } return $decoded; }
/** * @param $verifyCode * @param $newPassword * @throws AVException */ public static function resetPasswordBySmsCode($verifyCode, $newPassword) { $json = json_encode(array('password' => $newPassword)); AVClient::_request('PUT', '/resetPasswordBySmsCode/' . $verifyCode, null, $json); }