Beispiel #1
0
 public function send()
 {
     if (!$this->verifyRequest()) {
         throw new \Exception("Error: Malformed request!");
     } else {
         $header = array();
         foreach ($this->headers as $key => $value) {
             $header[] = $key . ': ' . $value;
         }
         $ch = curl_init('http://' . $this->server . $this->path);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         if ($this->method == 'GET') {
             curl_setopt($ch, CURLOPT_HTTPGET, true);
             $header[] = 'User-Agent: ' . SONIC_REQUEST__USERAGENT;
             $header[] = 'Connection: close';
             curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
         } elseif ($this->method == 'POST') {
             curl_setopt($ch, CURLOPT_POST, true);
             $header[] = 'User-Agent: ' . SONIC_REQUEST__USERAGENT;
             $header[] = 'Content-type: application/json';
             $header[] = 'Connection: close';
             $header[] = 'Content-Length: ' . strlen($this->body);
             $header[] = 'Expect: ';
             // explicitly un-setting Expect header
             curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
             curl_setopt($ch, CURLOPT_POSTFIELDS, $this->body);
         } elseif ($this->method == 'PUT') {
             curl_setopt($ch, CURLOPT_POST, true);
             $header[] = 'User-Agent: ' . SONIC_REQUEST__USERAGENT;
             $header[] = 'Content-type: application/json';
             $header[] = 'Connection: close';
             $header[] = 'Content-Length: ' . strlen($this->body);
             $header[] = 'Expect: ';
             // explicitly un-setting Expect header
             curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
             curl_setopt($ch, CURLOPT_POSTFIELDS, $this->body);
             curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
             //curl PUT only accepts files
         } elseif ($this->method == 'DELETE') {
             curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
             $header[] = 'User-Agent: ' . SONIC_REQUEST__USERAGENT;
             $header[] = 'Connection: close';
             $header[] = 'Expect: ';
             // explicitly un-setting Expect header
             curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
         }
         if (Configuration::getCurlVerbose() >= 1) {
             curl_setopt($ch, CURLOPT_VERBOSE, 1);
         }
         curl_setopt($ch, CURLOPT_HEADER, true);
         $response = curl_exec($ch);
         $header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
         $header = substr($response, 0, $header_size);
         $body = substr($response, $header_size);
         if (curl_errno($ch) != CURLE_OK) {
             throw new \Exception('Connection error: ' . curl_error($ch));
         }
         curl_close($ch);
         return array('headers' => $header, 'body' => $body);
     }
 }
Beispiel #2
0
 /**
  * Pushes an update for a SocialRecord to the GSLS. The SocialRecord will be transformed into a signed JWT, which is then stored in the GSLS
  * 
  * @param $sr The SocialRecord
  * @param $personalPrivateKey The private key to sign the JWT
  * 
  * @throws Exception
  * 
  * @return result json string
  */
 public static function putSocialRecord(SocialRecord $sr, $personalPrivateKey)
 {
     if (!$sr->verify()) {
         throw new \Excetion("Error: Invalid Social Record");
     }
     // create and sign JWT
     $signer = new Sha512();
     $personalPrivateKey = PrivateKey::formatPEM($personalPrivateKey);
     $token = (new Builder())->set('socialRecord', base64_encode($sr->getJSONString()))->sign($signer, $personalPrivateKey)->getToken();
     $ch = curl_init(Configuration::getPrimaryGSLSNode());
     if (Configuration::getCurlVerbose() >= 2) {
         curl_setopt($ch, CURLOPT_VERBOSE, 1);
     }
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_POST, true);
     curl_setopt($ch, CURLOPT_TIMEOUT, Configuration::getGSLSTimeout());
     curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
     curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-type: application/json', 'Content-Length: ' . strlen((string) $token)));
     curl_setopt($ch, CURLOPT_POSTFIELDS, (string) $token);
     $result = curl_exec($ch);
     if (curl_errno($ch) != CURLE_OK) {
         $ch = curl_init(Configuration::getSecondaryGSLSNode());
         if (Configuration::getCurlVerbose() >= 2) {
             curl_setopt($ch, CURLOPT_VERBOSE, 1);
         }
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($ch, CURLOPT_HTTPGET, 1);
         $result = curl_exec($ch);
         if (curl_errno($ch) != CURLE_OK) {
             throw new \Exception('Connection error: ' . curl_error($ch));
         }
     }
     $result = json_decode($result);
     curl_close($ch);
     if ($result->responseCode != 200) {
         throw new \Exception("Error: " . $result->message);
     } else {
         return $result;
     }
 }