使用AdWords API添加关键字时出现问题

时间:2014-12-12 15:47:56

标签: php error-handling google-api google-adwords

我正在尝试使用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

Bellow你可以看到我的代码:

$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);
}

请告诉我,我该如何解决这个问题? 谢谢!

1 个答案:

答案 0 :(得分:1)

看起来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);