Skip to content

Commit

Permalink
Merge pull request #1 from xiaoxuan6/master
Browse files Browse the repository at this point in the history
feat: Add verify option
  • Loading branch information
justmd5 authored Apr 2, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents 091c9a7 + 9bf5c48 commit 109ebb4
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
@@ -64,6 +64,11 @@ try {
}
```

## without verify

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


## Contributing
30 changes: 26 additions & 4 deletions src/DeepLTranslator.php
Original file line number Diff line number Diff line change
@@ -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
*
@@ -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)
@@ -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);

0 comments on commit 109ebb4

Please sign in to comment.