我有以下代码,它不起作用,我只会得到字符串(0)” 该代码是一个Deepl Curl脚本的副本,它创建了一个完美的词汇表,我只是更改了数据和选项。 如果更改auth,我也不会遇到任何错误。代码
<?php
$text = 'Hi there';
$source_lang = "en";
$target_lang = "de";
$glossary_id = "";
$tag_handling = "xml";
$ignore_tags = "x";
$formality = "";
$url = "https://api.deepl.com/v2/translate";
$authKey = "hidden";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"Authorization: DeepL-Auth-Key .$authKey",
"Content-Type: application/json",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$data = <<<DATA
text=$text&source_lang=$source_lang&target_lang=$target_lang&tag_handling=$tag_handling&ignore_tags=$ignore_tags&formality=$formality&glossary=$glossary_id
DATA;
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
// Check for errors and display the error message
if($errno = curl_errno($curl)) {
$error_message = curl_strerror($errno);
echo "cURL error ({$errno}):\n {$error_message}";
}
curl_close($curl);
var_dump($resp);
?>
我没有遇到任何错误。
您可以尝试以下更新的代码,
<?php
$text = 'Hi there';
$source_lang = "en";
$target_lang = "de";
$tag_handling = "xml";
$ignore_tags = "x";
$formality = "";
$url = "https://api.deepl.com/v2/translate";
$authKey = "hidden";
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$headers = array(
"Authorization: DeepL-Auth-Key $authKey",
"Content-Type: application/json",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$data = json_encode([
'text' => [$text],
'source_lang' => $source_lang,
'target_lang' => $target_lang,
'tag_handling' => $tag_handling,
'ignore_tags' => $ignore_tags,
'formality' => $formality
]);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
//for debug only!
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
// Check for errors and display the error message
if ($errno = curl_errno($curl)) {
$error_message = curl_strerror($errno);
echo "cURL error ({$errno}):\n {$error_message}";
} else {
echo "Response:\n";
print_r(json_decode($resp, true));
}
curl_close($curl);
?>