Skip to content

Commit

Permalink
feat: Add verify option
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoxuan6 committed Apr 2, 2024
1 parent 091c9a7 commit 9bf5c48
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
vendor
.idea
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ try {
}
```

## without verify

```shell
\Justmd5\DeeplX\DeepLTranslator::withoutVerifying()->en2zh('hello');
```


## Contributing
Expand Down
30 changes: 26 additions & 4 deletions src/DeepLTranslator.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,27 @@ class DeepLTranslator
*/
protected $timeout;

/**
* @var bool
*/
protected static $verify = true;

public function __construct(int $timeout = 5)
{

$this->timeout = $timeout;
}

/**
* @return DeepLTranslator
*/
public static function withoutVerifying(): DeepLTranslator
{
self::$verify = false;

return new self;
}

/**
* @return array|string
*
Expand Down Expand Up @@ -101,7 +116,7 @@ public function translate(string $query, string $to, string $from = 'auto'): Dee
}

/**
* @param int $type 0:format 1:json string 2:array
* @param int $type 0:format 1:json string 2:array
* @return array|string
*/
public function result(int $type = self::TYPE_FORMAT)
Expand Down Expand Up @@ -181,16 +196,23 @@ private static function getTimeStamp(string $translateText): int
*/
private function postData(string $url, string $data)
{
$curl = curl_init($url);
curl_setopt_array($curl, [
$opt = [
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $data,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
],
CURLOPT_CONNECTTIMEOUT => $this->timeout,
CURLOPT_RETURNTRANSFER => true,
]);
];

if (self::$verify == false) {
$opt[CURLOPT_SSL_VERIFYPEER] = false;
$opt[CURLOPT_SSL_VERIFYHOST] = false;
}

$curl = curl_init($url);
curl_setopt_array($curl, $opt);
$response = curl_exec($curl);
curl_close($curl);

Expand Down

0 comments on commit 9bf5c48

Please sign in to comment.