@vadikjust

Yandex translate api post запрос ничего не получает?

Почему ничего не получает запрос? Вроде бы все верно указанно. Когда перехоу по ссылке, все работает, но при запросе все пусто.
function url_get_contents ($Url) {
    if (!function_exists('curl_init')){ 
        die('CURL is not installed!');
    }
    $ch = curl_init();
	curl_setopt($curl, CURLOPT_FAILONERROR, 1);
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // allow redirects
    curl_setopt($curl, CURLOPT_TIMEOUT, 20); // times out after 4s
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // return into a variable
    curl_setopt($ch, CURLOPT_URL, $Url);
	curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 GTB6");
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}
$params = array( 'key' => 'ключ', 'text' => 'Test Message', 'lang' => 'en-ru',); 
$query = http_build_query($params); 
$response = url_get_contents('https://translate.yandex.net/api/v1.5/tr.json/translate?'.$query); 
echo $response;
  • Вопрос задан
  • 540 просмотров
Решения вопроса 1
LightAir
@LightAir
LA
Смотрите внимательней ваш код. Инициализируется curl в переменную $ch, а опции ставятся для несуществующего $curl.

function url_get_contents ($Url) {
    if (!function_exists('curl_init')){
        die('CURL is not installed!');
    }
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_FAILONERROR, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); // allow redirects
    curl_setopt($ch, CURLOPT_TIMEOUT, 20); // times out after 4s
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return into a variable
    curl_setopt($ch, CURLOPT_URL, $Url);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5 GTB6");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

$params = array( 'key' => 'ключ', 'text' => 'Test Message', 'lang' => 'en-ru',);
$query = http_build_query($params);
$response = url_get_contents('https://translate.yandex.net/api/v1.5/tr.json/translate?'.$query);
echo $response;
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы