I
I
ilya173922019-09-20 10:44:19
PHP
ilya17392, 2019-09-20 10:44:19

A json string comes in, after using the json_decode function, it outputs an array, how to select individual elements from it, for example lang and a string at the end?

THE CODE:

$data = array(
    'key' => 'trnsl.1.1.20190919T120001Z.d754b7a5da841428.846bdf32c23f6295e7a341b8b1dc6e2c0bf31c30',
    'text' => 'My name is ...',
    'lang' => 'de',
    'format' => 'plain',
    'options' => 1,
);
 
$curlObject = curl_init();
 
curl_setopt($curlObject, CURLOPT_URL, 'https://translate.yandex.net/api/v1.5/tr.json/translate');
 
curl_setopt($curlObject, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curlObject, CURLOPT_SSL_VERIFYHOST, false);
 
curl_setopt($curlObject, CURLOPT_POST, true);
curl_setopt($curlObject, CURLOPT_POSTFIELDS, http_build_query($data,'','&'));
 
curl_setopt($curlObject, CURLOPT_RETURNTRANSFER, true);
 
$responseData = curl_exec($curlObject);
 
curl_close($curlObject);
 
if ($responseData === false) {
    throw new Exception('Response false');
}
 
var_dump(json_decode($responseData, true));

For example, to display en-ru: Hello-Hi

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
BATPYIIIKOB, 2019-09-20
@BATPYIIIKOB

$data = json_decode($responseData, true);

echo $data['lang'] ;

etc....

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question