0

I am trying to send data via curl by POST.

Here's what I have done

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,"https://lc-api.sdl.com/translate");
    $headers = array();
    $headers[] = 'Content-type:application/json';
    $headers[] = 'Authorization:LC apiKey=QidCWRGQ%2BHsdzGE6rrBgiw%3D%3D';

    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS,"text=Bonjour&from=fra&to=eng");  //Post Fields
    $server_output = curl_exec ($ch);

   curl_close ($ch);

   print  $server_output ;    

I get an error indicating

Error parsing json at column:6 line:1 offset:-1

Here is the working Curl request

  curl -X POST -H "Content-type: application/json" -H "Authorization: LC apiKey=QidCWRGQ%2BHsdzGE6rrBgiw%3D%3D" -d '{"text":"Bonjour", "from" : "fra", "to" : "eng"}' https://lc-api.sdl.com/translate

2 Answers 2

2

Try as below :

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://lc-api.sdl.com/translate");
$headers = array();
$headers[] = 'Content-type:application/json';
$headers[] = 'Authorization:LC apiKey=QidCWRGQ%2BHsdzGE6rrBgiw%3D%3D';

$data = array("text" => "Bonjour", "from" => "fra", "to" => "eng");
$data_string = json_encode($data);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);  //Post Fields
$server_output = curl_exec ($ch);

curl_close ($ch);

print  $server_output ;
?>

Post fields in json_encode format.

Sign up to request clarification or add additional context in comments.

4 Comments

I am getting the same error even after I changed it
I got {"partialTranslation":false,"from":"fra","to":"eng","wordCount":1,"charCount":7,"translation":"Hello"} result. Is this result as per your requirement? Clear your browser cache and then check.
Yeah ankit this is the desired result
I run above code and it worked on my localhost. Are you still getting that error?
0

add:

 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

full code:

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"https://lc-api.sdl.com/translate");
$headers = array();
$headers[] = 'Content-type:application/json';
$headers[] = 'Authorization:LC apiKey=QidCWRGQ%2BHsdzGE6rrBgiw%3D%3D';

$data = array("text" => "Bonjour", "from" => "fra", "to" => "eng");
$data_string = json_encode($data);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_POSTFIELDS,$data_string);  //Post Fields
$server_output = curl_exec ($ch);

curl_close ($ch);

var_dump($server_output );
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.