0

Hello folks I've the api link there I want to get out the result using php. I've done it in javascript. I'm not sure how to do this in php?

My api link

code:

<?php 
$from = ($_GET['f']);
$to = ($_GET['t']);
$phrese = urlencode($_GET['ph']);
$json=file_get_contents("https://glosbe.com/gapi/tm?
from=$from&dest=$to&format=json&phrase=$phrese&page=1&pretty=true");
$details=json_decode($json);
if($details->Response=='True')
?>

test  <?php echo $details->res.phrase.text;?><br>
Elumalai Kaliyaperumal
  • 1,498
  • 1
  • 12
  • 24
Zabkas
  • 67
  • 9

1 Answers1

2

You can use file_get_contents method to get json data. For file_get_contents to work you should enable allow_url_fopen. This can be used in run time as ini_set("allow_url_fopen", 1);

$json = file_get_contents('https://glosbe.com/gapi/tm?from=en&dest=fa&format=json&phrase=nice&page=1&pretty=true');
$obj = json_decode($json);
if($obj->result === 'ok') {
    foreach($obj->{'examples'} as $data) {
        echo $data->{'first'}."<br />";
    }
}

For alternative way you can use curl to get json data as

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, 'https://glosbe.com/gapi/tm?from=en&dest=fa&format=json&phrase=nice&page=1&pretty=true');
$result = curl_exec($ch);
curl_close($ch);

$obj = json_decode($result);
if($obj->result === 'ok') {
    foreach($obj->{'examples'} as $data) {
        echo $data->{'first'}."<br />";
    }
}
Elumalai Kaliyaperumal
  • 1,498
  • 1
  • 12
  • 24
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/160001/discussion-on-answer-by-elumalai-kaliyaperumal-how-to-get-result-out-of-json-us). – Andy Nov 28 '17 at 14:03