我正在尝试使用PHP语言和AdWords API在AdWords中添加一些关键字。当我用英语添加关键字时,它工作正常,但在俄语中它向我显示下一条错误消息:
[SoapFault]
SOAP-ERROR: Encoding: string 'xd2...' is not a valid utf-8 string (0)
/var/www/html/www3.repka.com.ua/sources/repka/work/adwords/AdWordsApi/source/src/Google/Api/Ads/Common/Lib/AdsSoapClient.php:232
#0: SoapClient->__soapCall(string, array, NULL, array, array)
/var/www/html/www3.repka.com.ua/sources/repka/work/adwords/AdWordsApi/source/src/Google/Api/Ads/Common/Lib/AdsSoapClient.php:232
#1: AdsSoapClient->__soapCall(string, array)
/var/www/html/www3.repka.com.ua/sources/repka/work/adwords/AdWordsApi/source/src/Google/Api/Ads/AdWords/v201409/AdGroupCriterionService.php:9936
#2: AdGroupCriterionService->mutate(array)
/var/www/html/www3.repka.com.ua/sources/repka/work/adwords/index.php:213
波纹管你可以看到我的代码:
$adGroupId = 1648319****;
$adGroupId = (float)$adGroupId;
$user = new AdWordsUser();
$user->LogAll();
$word = "Текст"; // error, but with $word = "Keyword" it works fine
echo $word;
echo urlencode($word);
$word = urlencode($word);
$adGroupCriterionService = $user->GetService('AdGroupCriterionService', 'v201409');
// Create keyword criterion.
$keyword = new Keyword();
$keyword->text = $word;
$keyword->matchType = 'BROAD';
// Create biddable ad group criterion.
$adGroupCriterion = new BiddableAdGroupCriterion();
$adGroupCriterion->adGroupId = $adGroupId;
$adGroupCriterion->criterion = $keyword;
// Set additional settings (optional).
$adGroupCriterion->userStatus = 'PAUSED';
$adGroupCriterion->destinationUrl = 'https://repka.ua/noutbuki/asus-s301lp-s301lp-c1010h-92202/';
$adGroupCriteria[] = $adGroupCriterion;
//var_dump($adGroupCriteria);
// Create operation.
$operation = new AdGroupCriterionOperation();
$operation->operand = $adGroupCriterion;
$operation->operator = 'ADD';
$operations[] = $operation;
$result = $adGroupCriterionService->mutate($operations);
// Display results.
foreach ($result->value as $adGroupCriterion) {
printf("Keyword with text '%s', match type '%s', and ID '%s' was added.n",
$adGroupCriterion->criterion->text,
$adGroupCriterion->criterion->matchType,
$adGroupCriterion->criterion->id);
}
请告诉我如何解决这个问题?谢谢!
看起来 API 只接受 UTF-8 字符串。因此,您需要将该俄语文本转换为 UTF-8 编码字符串。
mb_detect_encoding可以帮助您弄清楚现有的编码是什么,iconv 将允许您将其转换为 UTF-8。
试试这个:
$word = "Текст";
$word = iconv(mb_detect_encoding($word, mb_detect_order(), true), "UTF-8", $word);