1

I'm trying to use the Google API V2 with PHP

I need to exclude some words so I'm wrapping them into <span class"notranslate">WORD</span>

The problem is that my text contains some special chars so I'm using urlencode($input)

The issue is that urlencode breaks the exclude word functionality ...

What I'm doing wrong ?

Example

$url = "https://www.googleapis.com/language/translate/v2";
$params = array(
    'key' => $helper->getConfigValue('google_api'),
    'source' => $from,
    'target' => $to, // NOTE for CHINESE zh-CN
    'q' => urlencode($input),
);
$url .= '?' . http_build_query($params);

$handle = curl_init($url);
....
WonderLand
  • 5,494
  • 7
  • 57
  • 76

1 Answers1

3

http_build_query already applies url encoding, so the urlencode($input) is redundant

<?php
$str = '<span class="notranslate">WORD</span>';

$params = [
    'single' => $str,
    'double' => urlencode($str)
];           
echo http_build_query($params);

Results in:

single=%3Cspan+class%3D%22notranslate%22%3EWORD%3C%2Fspan%3E
double=%253Cspan%2Bclass%253D%2522notranslate%2522%253EWORD%253C%252Fspan%253E

Note the double encoding on double.

Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95