Example #1
0
 /**
  * UTF8 Encode data
  *
  * @param $mixed
  * @return array|string
  */
 public static function utf8ize($mixed)
 {
     if (is_array($mixed)) {
         foreach ($mixed as $key => $value) {
             $mixed[$key] = SafeJson::utf8ize($value);
         }
     } else {
         if (is_string($mixed)) {
             return utf8_encode($mixed);
         }
     }
     return $mixed;
 }
Example #2
0
 /**
  * Query Elastic Search
  *
  * @param $string
  * @param string $index
  * @param array $search_fields
  * @param $get_fields
  * @return mixed
  * @throws SearchAPIException
  */
 public static function query($string, $index, $search_fields, $get_fields)
 {
     if (empty($index) || empty($search_fields)) {
         throw new SearchAPIException("Missing required fields");
     }
     $params = ['query' => ['simple_query_string' => ['query' => $string, 'analyzer' => 'snowball', 'fields' => $search_fields, 'default_operator' => 'and']], 'fields' => $get_fields];
     $post_fields = SafeJson::encode($params);
     $ch = curl_init("http://" . self::$host . "/{$index}/_search");
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
     curl_setopt($ch, CURLOPT_TIMEOUT, 120);
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     curl_setopt($ch, CURLOPT_POST, true);
     curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
     $header_params = ['Content-type: application/json'];
     curl_setopt($ch, CURLOPT_HTTPHEADER, $header_params);
     $result = curl_exec($ch);
     curl_close($ch);
     return json_decode($result, true);
 }
Example #3
0
 /**
  * Post data to Net Suite API
  *
  * @param $data
  * @return array|bool|float|int|string
  * @throws NetSuiteException
  */
 public function post($data)
 {
     $this->result = [];
     $this->error_code = null;
     $this->error_message = null;
     $request = $this->client->post(null, [], SafeJson::encode($data), ['timeout' => floatval($this->timeout)]);
     try {
         $response = $request->send();
         return $this->success($response->json());
     } catch (Exception $e) {
         if (strpos($e->getMessage(), '[curl] 28') !== false) {
             throw new NetSuiteException($this->timeout_error);
         } else {
             throw new NetSuiteException($this->bad_res);
         }
     }
 }
Example #4
0
 /**
  * Send multiple events to Event bus
  *
  * Will submit all events in .2 seconds
  *
  * @param $event
  * @param $key
  * @param $object_name
  * @param $data
  * @return bool|\Guzzle\Service\Resource\Model
  */
 public function sendMultiple($event, $key, $object_name, $data)
 {
     if ($this->client == null) {
         return false;
     }
     $records = [];
     foreach ($data as $record) {
         $event_data = ['id' => $this->id, 'event' => $event, 'reference_id' => strtolower($object_name) . '_' . $record[$object_name][$key], 'payload' => $record];
         $records[] = ['Data' => SafeJson::encode($event_data), 'PartitionKey' => $record[$object_name][$key]];
     }
     $time_start = microtime(true);
     $result = $this->client->putRecords(['Records' => $records, 'StreamName' => $this->streamName]);
     $time_end = microtime(true);
     $this->time_taken = number_format($time_end - $time_start, 2);
     return $result;
 }
Example #5
0
 /**
  * Marshal data for json
  *
  * @param $data
  * @return array
  */
 public function marshallData($data)
 {
     $marshaler = new Marshaler();
     $data = $this->array_remove_empty($data);
     $json = $marshaler->marshalJson(SafeJson::encode($data));
     return $json;
 }
 /**
  * Call Snap Fulfil API
  *
  * @param string $action
  * @param array $parameters
  * @param bool $put
  * @param int $instance
  * @param bool $log
  * @param bool $delete
  * @param bool $gui (specifies if a gui is requesting it
  * @return array|stdClass
  * @throws SnapFulfilAPIError
  */
 public static function call($action, $parameters = null, $put = FALSE, $instance = 1, $log = FALSE, $delete = FALSE, $gui = FALSE)
 {
     if (defined('DISABLE_SNAP') && DISABLE_SNAP === true) {
         return new stdClass();
     }
     $config = SnapConfig::config($instance);
     $ch = curl_init($config['url'] . $action);
     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
     curl_setopt($ch, CURLOPT_TIMEOUT, 120);
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
     curl_setopt($ch, CURLOPT_USERPWD, $config['username'] . ":" . $config['password']);
     $header_params = array('Content-type: application/json');
     $post_fields = SafeJson::encode($parameters);
     if (!empty($parameters)) {
         curl_setopt($ch, CURLOPT_POST, true);
         curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
     }
     if ($put) {
         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
         if ($parameters === NULL) {
             $header_params[] = 'Content-length: 0';
         }
     }
     if ($delete) {
         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "DELETE");
         if ($parameters === NULL) {
             $header_params[] = 'Content-length: 0';
         }
     }
     curl_setopt($ch, CURLOPT_HTTPHEADER, $header_params);
     $result = curl_exec($ch);
     $header = curl_getinfo($ch);
     curl_close($ch);
     // temp code to track api calls to snap
     if ($log) {
         //$trace     = debug_backtrace();
         $trace = debug_backtrace();
         $api_audit = ClassRegistry::init('ApiAudit');
         $api_data = ['reference_name' => "Snap API", 'reference_id' => 0, 'source' => "", 'destination' => $action, 'sent' => json_encode(['parameters' => $parameters, 'trace' => $trace]), 'received' => $header['http_code'] . " - " . $result, 'notes' => 'Snap API Call'];
         $api_audit->clear();
         $api_audit->create($api_data);
         $api_audit->save();
     }
     switch ($header['http_code']) {
         case 200:
             //returned data
             return json_decode($result);
             break;
         case 201:
             //entity created
             return json_decode($result);
             break;
         case 204:
             //No Content
             return FALSE;
             break;
         case 304:
             //              error_log("Already exists");
             //if being sent to the GUI display the duplicate error message
             if ($result == '' && $delete == FALSE && $gui == TRUE) {
                 throw new SnapFulfilAPIError("shipment already exists in Snap.", $header['http_code']);
             } elseif ($result == '' && $delete == FALSE) {
                 //don't error out when being processed by the SQS snap send
                 return TRUE;
             } else {
                 //catch all other situations
                 throw new SnapFulfilAPIError("Snap send error.", $header['http_code']);
             }
             break;
         case 400:
             //invalid entity//
             // not in Snap
             if (strpos($result, 'SKUId":"SKU"') !== FALSE) {
                 throw new SnapFulfilAPIError("One of the submitted SKUs is not in the Snap, " . "the shipment didn't send correctly, contact Support to resolve.", $header['http_code']);
             } else {
                 throw new SnapFulfilAPIError("Invalid entity.", $header['http_code']);
             }
             break;
         case 401:
             //not authorized
             //              error_log("Not authorized");
             return FALSE;
             break;
         case 404:
             //entity not found
             //              $error = !empty($result) ? $result : "No message returned";
             //              error_log("Not found error: " . $error);
             throw new SnapFulfilAPIError("Entity not found", $header['http_code']);
             break;
         case 409:
             //Conflict
             //              $error = !empty($result) ? $result : "No message returned";
             //              error_log("Conflict: " . $error);
             return FALSE;
             break;
         case 500:
         case 503:
             sleep(5);
             break;
         default:
             //              error_log(curl_error($ch));
             throw new InternalErrorException();
             break;
     }
     return false;
 }
Example #7
0
 /**
  * Send JSON data
  *
  * @link http://docs.aws.amazon.com/aws-sdk-php/v2/api/class-Aws.Sqs.SqsClient.html#_sendMessage
  * @param $data
  * @param null $delaySeconds
  * @param null $priority
  * @return bool|\Guzzle\Service\Resource\Model
  */
 public function sendJSON($data, $delaySeconds = null, $priority = null)
 {
     if (empty($this->url)) {
         return false;
     }
     $sendData = ['QueueUrl' => $this->url, 'MessageBody' => SafeJson::encode($data)];
     if ($delaySeconds != null) {
         $sendData['DelaySeconds'] = $delaySeconds;
     }
     if ($priority != null) {
         $sendData['MessageAttributes'] = ["Priority" => ['DataType' => 'Number', 'StringValue' => $priority]];
     }
     return $this->client->sendMessage($sendData);
 }