How can I call transltr api from my localhost using php? I have zero knowledge in calling api and what to type in the php file. Thank you in advance !
php cUrl library must be loaded and enabled. You can check this using phpinfo file.
To use API, you can try code below.
<?php
$params = array('text'=>'something', 'from'=>'en', 'to'=>'de');
$data_string = json_encode($params);
$ch = curl_init('http://www.transltr.org/api/translate');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
if(!$result){
die('Error: "' . curl_error($curl) . '" - Code: ' . curl_errno($curl));
}
echo $result;
?>
Hope this helps
You can use curl_exec function from CURL extension. In the most simple form it looks like:
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://www.example.com/");
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
$result = curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
Now $result
holds what you need.