/** * @param $username * @param $password * @return array Success { * @var string AccessId * @var string SecretKey * @var string UserId * @var string EmployeeId * } * @throws MPAuthException */ public function authenticate($username, $password) { $data = ['Login' => $username, 'Password' => md5($password)]; $this->prepareRequest($data); try { $response = $this->curl->post($this->url . $this->authRelativeUrl, true); try { $decodedResponse = Json::decode($response, true); switch ($decodedResponse['status']['code']) { case 'ok': break; case 'error': throw new MPAuthException('Invalid username or password.'); break; default: throw new MPAuthException('Unknown response status.'); } return $decodedResponse['data']; } catch (InvalidParamException $e) { throw new MPAuthException('Error decoding server response. Raw response: ' . var_export($response, true)); } } catch (Exception $e) { throw new MPAuthException('Error requesting host:' . $e->getMessage() . $e->getCode()); } }
public function actionSend() { if (Yii::$app->request->post("recipient_id")) { $recipient_id = Yii::$app->request->post("recipient_id"); $text = Yii::$app->request->post("message"); $sender_id = Yii::$app->request->post("sender_id"); $recipientUser = Salamiuser::find()->where(['id' => $recipient_id])->one(); $senderUser = Salamiuser::find()->where(['id' => $sender_id])->one(); $message = new Messages(); $message->sender_id = $senderUser->id; $message->recipient_id = $recipientUser->id; $message->text = $text; $message->state = "new"; $saved = $message->save(); $data = array('user_ids' => array($recipientUser->facebook_id), 'notification' => array("alert" => $text, "android" => array('payload' => array("sender" => $senderUser->facebook_id)))); $ionicAppId = "70f2a984"; $ionicApiSecret = "ec4839bfb9ea2daa2271b0149a0c18003a3672178f4e6485"; $url = "https://push.ionic.io/api/v1/push"; $content = json_encode($data); $curl = new curl\Curl(); $curl->setOption(CURLOPT_SSL_VERIFYPEER, false); $curl->setOption(CURLOPT_POSTFIELDS, $content); $curl->setOption(CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'X-Ionic-Application-Id: ' . $ionicAppId, 'Content-Length: ' . strlen($content), 'Authorization: Basic ' . base64_encode($ionicApiSecret))); $response = $curl->post('https://push.ionic.io/api/v1/push'); return $response; } return '{"error": "Invalid request parameters"}'; }
/** * cURL advanced GET example with HTTP status codes */ public function actionGetAdvancedExample() { //Init curl $curl = new curl\Curl(); //get http://example.com/ $response = $curl->post('http://example.com/'); // List of status codes here http://en.wikipedia.org/wiki/List_of_HTTP_status_codes switch ($curl->responseCode) { case 200: //success logic here break; case 404: //404 Error logic here break; } }
public function subscribe() { $url = Yii::$app->params['getdrip']['apiUrl'] . Yii::$app->params['getdrip']['accountId'] . '/' . 'campaigns/' . Yii::$app->params['getdrip']['signup_campaignId'] . '/subscribers'; $subscribe = json_encode(['subscribers' => [["email" => $this->email, "utc_offset" => 660, "double_optin" => true, "starting_email_index" => 0]]]); $curl = new curl\Curl(); $curl->reset(); $curl->setOption(CURLOPT_FRESH_CONNECT, true); $curl->setOption(CURLOPT_FORBID_REUSE, true); $curl->setOption(CURLOPT_RETURNTRANSFER, true); $curl->setOption(CURLOPT_FOLLOWLOCATION, true); $curl->setOption(CURLOPT_SSL_VERIFYPEER, false); $curl->setOption(CURLOPT_SSL_VERIFYHOST, false); $curl->setOption(CURLOPT_USERPWD, Yii::$app->params['getdrip']['apiToken'] . ":" . ''); $curl->setOption(CURLOPT_POSTFIELDS, $subscribe); $curl->setOption(CURLOPT_CUSTOMREQUEST, "POST"); $curl->setOption(CURLOPT_HTTPHEADER, array('Accept:application/json, text/javascript, */*; q=0.01', 'Content-Type: application/vnd.api+json')); $response = $curl->post($url); }