Skip to content

Commit

Permalink
Fix curl request
Browse files Browse the repository at this point in the history
  • Loading branch information
davmixcool committed Mar 7, 2022
1 parent ac3474d commit 4658ea9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ $logsnag = new LogSnag('7f568d735724351757637b1dbf108e5');
### Publish Event

```php
$logsnag->publish({
$logsnag->publish([
'project' => "my-saas",
'channel' => "waitlist",
'event' => "User Joined",
'icon' => "🎉",
'notify' => true
});
]);
```


Expand Down
5 changes: 1 addition & 4 deletions src/LogSnag.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,15 @@ class LogSnag
{
private $api_token = '';
private $request_handler;
private $format;

/**
* LogSnag constructor.
* @param $api_token
*/
public function __construct($api_token='')
{
// Set keys and format passed to class
$this->api_token = $api_token;
$this->format = $format;
// Throw an error if the keys are not both passed
// Throw an error if the token is not passed passed
try {
if (empty($this->api_token)) {
throw new Exception("Your logsnag api token is not set!");
Expand Down
18 changes: 8 additions & 10 deletions src/Request/LogSnagCurlRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,30 +48,28 @@ public function execute($command, array $fields = [])
$fields['version'] = 1;
$fields['key'] = $this->api_token;

// Throw an error if the format is not equal to json or xml
if ($fields['format'] != 'json' && $fields['format'] != 'xml') {
return ['error' => 'Invalid response format passed. Please use "json" as a format value'];
}

// Generate query string from fields
$post_fields = http_build_query($fields, '', '&');

// Generate the HMAC hash from the query string and api_token
$hmac = hash_hmac('sha512', $post_fields, $this->api_token);
$headers = [
"Content-Type: application/json",
"Authorization: Bearer ".$this->api_token
];

// Check the cURL handle has not already been initiated
if ($this->curl_handle === null) {

// Initiate the cURL handle and set initial options
$this->curl_handle = curl_init($api_url);
$this->curl_handle = curl_init();
curl_setopt($this->curl_handle, CURLOPT_URL,$api_url);
curl_setopt($this->curl_handle, CURLOPT_FAILONERROR, TRUE);
curl_setopt($this->curl_handle, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($this->curl_handle, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($this->curl_handle, CURLOPT_POST, TRUE);
}

// Set HMAC header for cURL
curl_setopt($this->curl_handle, CURLOPT_HTTPHEADER, array('HMAC:' . $hmac));
// Set header for cURL
curl_setopt($this->curl_handle, CURLOPT_HTTPHEADER, $headers);

// Set HTTP POST fields for cURL
curl_setopt($this->curl_handle, CURLOPT_POSTFIELDS, $post_fields);
Expand Down
9 changes: 8 additions & 1 deletion src/Validator/LogSnagValidator.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php
namespace Davmixcool\Validator;

use \Exception;
/**
* Class LogSnagValidator
*
Expand Down Expand Up @@ -40,7 +42,7 @@ class LogSnagValidator
'type' => 'string'
],
'notify' => [
'type' => 'string'
'type' => 'boolean'
]
]
]
Expand Down Expand Up @@ -248,6 +250,11 @@ private function validateFieldType($field_key, $field_value, $expected_type, $fi
$actual_type = gettype($field_value);
}
break;
case 'boolean':
if (filter_var($field_value, FILTER_VALIDATE_BOOLEAN) === FALSE) {
$actual_type = gettype($field_value);
}
break;
case 'array':
if (!is_array($field_value)) {
$actual_type = gettype($field_value);
Expand Down

0 comments on commit 4658ea9

Please sign in to comment.