Answer the question
In order to leave comments, you need to log in
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));
Answer the question
In order to leave comments, you need to log in
$data = json_decode($responseData, true);
echo $data['lang'] ;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question