I have the following code and it is not working, I just get string(0) "" This code is a copy of a DeepL curl script that creates a glossary which works perfect, I just changed the url and options in DATA. I'm also not getting any errors if I change the auth. code.
<?php
$text = 'Hi there';
$source_lang = "en";
$target_lang = "de";
$glossary_id = "";
$tag_handling = "xml";
$ignore_tags = "x";
$formality = "";
$url = ";;
$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);
?>
I'm not getting any errors.
I have the following code and it is not working, I just get string(0) "" This code is a copy of a DeepL curl script that creates a glossary which works perfect, I just changed the url and options in DATA. I'm also not getting any errors if I change the auth. code.
<?php
$text = 'Hi there';
$source_lang = "en";
$target_lang = "de";
$glossary_id = "";
$tag_handling = "xml";
$ignore_tags = "x";
$formality = "";
$url = "https://api.deepl/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);
?>
I'm not getting any errors.
Share Improve this question asked Mar 17 at 4:57 PeterPeter 919 bronze badges 15 | Show 10 more comments2 Answers
Reset to default 0Can you try below updated code,
<?php
$text = 'Hi there';
$source_lang = "en";
$target_lang = "de";
$tag_handling = "xml";
$ignore_tags = "x";
$formality = "";
$url = "https://api.deepl/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);
?>
The following works perfect and was put into production yesterday, thanks to all.
<?php
$authKey = $_POST["auth_key"];
$_text = $_POST["text"];
$target_lang = $_POST["target_lang"];
$formality = $_POST["formality"];
$glossary_id = $_POST["glossary_id"];
$source_lang = "en";
$tag_handling = "xml";
$ignore_tags = "x";
$url = "https://api.deepl/v2/translate";
$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/x-www-form-urlencoded",
);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$data = array(
'text' => $_text,
'source_lang' => $source_lang,
'target_lang' => $target_lang,
'tag_handling' => $tag_handling,
'ignore_tags' => $ignore_tags,
'formality' => $formality,
'glossary' => $glossary_id
);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
//for debug only!
//curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
//curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
$resp = curl_exec($curl);
// Check HTTP status code and display the error message
if (!curl_errno($curl)) {
switch ($http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE)) {
case 200: # OK
break;
default:
echo 'DeepL Error: Unexpected HTTP code: ', $http_code;
}
}
// Check for errors and display the error message
if($errno = curl_errno($curl)) {
$error_message = curl_strerror($errno);
echo "DeepL Error: cURL error ({$errno}):\n {$error_message}";
}
curl_close($curl);
echo $resp;
?>
curl_getinfo($ch, CURLINFO_HTTP_CODE) == 200
– shingo Commented Mar 17 at 6:11http_build_query()
to properly build$data
. – Olivier Commented Mar 17 at 9:04application/xml
? You're not sending XML. – Olivier Commented Mar 17 at 9:31