Скрипт PHP google translate на curl
Ранее пользовался скриптом
function gtranslate($str, $lang_to) {
$lang_from = 'en';
$query_data = array(
'client' => 'x',
'q' => $str,
'sl' => $lang_from,
'tl' => $lang_to
);
$filename = 'http://translate.google.ru/translate_a/t';
$options = array(
'http' => array(
'user_agent' => 'Mozilla/5.0 (Windows NT 6.0; rv:26.0) Gecko/20100101 Firefox/26.0',
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => http_build_query($query_data)
)
);
$context = stream_context_create($options);
$response = file_get_contents($filename, false, $context);
return json_decode($response);
}
echo gtranslate('Hello World.','ru')
Все работало прекрасно, и сейчас работает но на хостинге отключили allow_url_fopen
Как переписать скрипт на Curl?
function gtranslate($str, $lang_to) {
$lang_from = 'en';
$query_data = array(
'q' => $str,
'sl' => $lang_from,
'tl' => $lang_to
);
$options = array(
'user_agent' => 'Mozilla/5.0 (Windows NT 6.0; rv:26.0) Gecko/20100101 Firefox/26.0',
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => http_build_query($query_data)
);
$url = 'https://translate.google.ru/translate_a/t';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $options);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($query_data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$response = curl_exec($ch);
curl_close($ch);
return json_decode($response);
}
Так не работает