Problems with adding keywords with AdWords API - php

I'm trying to add some keywords in AdWords using PHP language and AdWords API. When I add keywords in english it works fine, but in russian it shows me next error message:
[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 You can see my code:
$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);
}
Tell me please how can I solve this problem?
Thanks!

It looks like the API only accepts UTF-8 strings. So you will need to convert that russian text to be a UTF-8 encoded string.
mb_detect_encoding can help you figure out what the existing encoding is and iconv will allow you to convert it to UTF-8.
Try this:
$word = "Текст";
$word = iconv(mb_detect_encoding($word, mb_detect_order(), true), "UTF-8", $word);

Related

Read a variable from a JSON file

Good evening,
I am retrieving a JSON file and trying to read a variable at the very end of the file.
$eedomus_api_user = 'xyz';
$eedomus_api_secret = 'zzz';
$periph_id = 'yyy';
$url = "http://19X.XXX.0.XX/api/get?";
$url .= "api_user=$eedomus_api_user";
$url .= "&api_secret=$eedomus_api_secret";
$url .= "&action=periph.value";
$url .= "&periph_id=$periph_id";
$result = file_get_contents($url);
$phpObj = json_decode($result, true);
The file I'm getting is the following :
{ "success": 1, "body":{"last_value": 100} }
I'm interested in getting the '100' corresponding to the last_value field.
Using the function strpos, I'm intending to look for the position of the first '}' and retrieve the characters until the previous space. Is there an easier way to achieve this ?
Thanks for your help.

COM object method for PHP

I am in process of developing the COM object that will encrypt the passed string. It is to be called from PHP.
I am not getting how can i return the value from COM to PHP. I tried following code perhaps it is not working.
STDMETHODIMP CATLObject::EncryptURL(VARIANT* sURLString, VARIANT* sEncryptedValue)
{
URLEncryption oURLEncryption;
char sRequestString[MAX_NAME] = "abcbbefekjdss dsf dsk fkjds fk sd";
char sEncrytedRequestStrin
g[MAX_NAME] = "";
char sDecrytedRequestString[MAX_NAME] = "";
oURLEncryption.EncryptQuery(sRequestString, sEncrytedRequestString);
KeyValue oKeyValue1;
KeyValue oParameterKeyValue;
oKeyValue1.ParseKeyValueString(sEncrytedRequestString, '&', true);
string sParameter = oKeyValue1.GetValue("sp");
string sCheckSum = oKeyValue1.GetValue("chk");
oURLEncryption.DecryptQuery(sParameter, sCheckSum, (void *)&oParameterKeyValue);
string sCidAudio = oParameterKeyValue.GetValue("cid_audio");
string sEUid = oParameterKeyValue.GetValue("euid");
printf("sCidAudio = %s\n", sCidAudio.c_str());
printf("sEUid = %s\n\n", sEUid.c_str());
// Create an instance of the MEMORYSTATUSEX structure
MEMORYSTATUSEX memstatex;
// Specify the length of the structure
memstatex.dwLength = sizeof(memstatex);
// Call the GlobalMemoryStatusEx function and pass to it
// a reference to our MEMORYSTATUSEX instance
::GlobalMemoryStatusEx(&memstatex);
// Set the ulVal (unsigned long value) of the VARIANT parameter
// passed by reference to the function with the dwMemoryLoad
// value of the MEMORYSTATUEX instance which specifies the
// approximate percentage of the physical memory currently
// in use.
sURLString->ulVal = memstatex.dwMemoryLoad;
sEncryptedValue->bstrVal = L"Output from DLL!!!";
return S_OK;
}
From PHP I am trying following way to get return value
$testConnection = new COM("URLEncryption.ATLObject");
$sURLString = new VARIANT(0, VT_UI4);
$sEncryptedValue = new VARIANT(0, VT_UI4);
$testConnection->EncryptURL($sURLString,$sEncryptedValue);
Looks to me to be a silly typo:
new COM("URLEncryption.ATLObject")
compared to
STDMETHODIMP CATLObject::EncryptURL
^^^
I'd say new COM should be changed to:
$testConnection = new COM("URLEncryption.CATLObject");

Preg_Match For YouTube Views

I'm new to php and i'm facing this problem. Need some help in matching the youtube views preg_match.
The below codes posted are the codes i made for the preg match but these are not working. Can anyone help me out.
if($row['type']=="Views"){
$data = $row['data'];
$type = "YouTube Views";
//INSTAGRAM
$file = #file_get_contents($data);
preg_match('/\"watch-view-count":(.*?)\,/',$file,$mfc);
$ccnt = $mfc[1];
//INSTAGRAM
}
if($row['type']=="Views"){
$data = $row['data'];
$type = "YouTube Views";
//Views
$file = #file_get_contents($data) or die("YouTube Offline ? :/");
preg_match('/\"watch-view-count":(.*?)\,/',$file,$mfc);
$ccnt = $mfc[1];
$data = http://www.youtube.com/watch?v=BPmhFrwDJTo
I need the preg_match to get the current count of views in the youtube video. I'm really sorry for the bad grammar.
Don't use regex for parsing HTML. Use a DOM Parser instead. It'll be more efficient.
In this case, you can simple use the Youtube API:
<?php
$video_ID = 'BPmhFrwDJTo';
$JSON = file_get_contents("https://gdata.youtube.com/feeds/api/videos/{$video_ID}?v=2&alt=json");
$JSON_Data = json_decode($JSON);
$views = $JSON_Data->{'entry'}->{'yt$statistics'}->{'viewCount'};
echo $views.' views';
?>
Output:
7 views

PHP json_encode() UTF-8 special characters fail

I am using html_entity_decode($string) to decode html special characters such as &auml=ä. I am then using json_encode() to create a json string that I use for an Android application. My problem is that i get an output of \u00e4 instead of 'ä'. I know that json_encode() only works with UTF-8 encoded strings, but when I run mb_detect_encding($myString) on my values it returns "UTF-8". It does't help to run ut8_encode() on the values. Here is my code:
$newsList = array();
while($row = $news->fetch_object()){
$tmpNews = new News();
$tmpNews->imgId = $row->image_id;
$tmpNews->author = html_entity_decode($row->author);
$tmpNews->subject = $row->subject;
$tmpNews->msg = $row->msg;
$tmpNews->newsmsg = $row->newsmsg;
$tmpNews->date = $row->wdate;
array_push($newsList, $tmpNews);
$tmpNews = null;
}
$json = array();
foreach($newsList as $news){
array_push($json, $news->getJson());
}
var_dump($json);
echo json_encode($json);
When i do the var_dump($json) my special characters shows as normal.

Random results in custom encryption / decryption function

Im trying to encrypt data through a custom function i created... (based on base64)
The code works but to a degree... it gives random results (sometimes work and sometimes doesn't).
<?php
set_time_limit(0);
class encryptor{
function encrypt($data){
$all = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!?#,.&*()$; ";
$chars = str_split($all, 1); // Split $all to array of single characters
$text_chars = str_split($data, 1); // Split $data to array of single characters
// Create array of unique results based on the characters from $all
foreach($chars as $char){
$array[$char] = md5(uniqid(rand(), true));
}
// Replace the input text with the results from $array
foreach($text_chars as $text_char){
$data = str_replace($text_char,$array[$text_char], $data);
}
// Encode and compress $array as $solution
$solution = gzcompress(base64_encode(json_encode($array)),9);
// Return the encoded solution + Breaker + encoded and compressed input text
return $solution."BREAKHERE".gzcompress(base64_encode($data),9);
}
function decrypt($data){
// Break the encrypted code to two parts
$exploded = explode('BREAKHERE', $data);
// Decoding the contents
$contents = base64_decode(gzuncompress($exploded[1]));
// Decoding solution ($array)
$solves = json_decode(base64_decode(gzuncompress($exploded[0])),true);
$fliped = array_flip($solves);
// Replace the encrypted data with the solution
foreach($solves as $solve){
$contents = str_replace($solve,$fliped[$solve], $contents);
}
return($contents); // Return decoded data
}
}
$text = "11 1";
$enc = new encryptor();
$encrypted = $enc->encrypt($text);
$decrypted = $enc->decrypt($encrypted);
echo $decrypted;
?>
Since its just for fun, this change appears to make it work:
// Replace the input text with the results from $array
$encrypted = '';
foreach($text_chars as $text_char){
//$data = str_replace($text_char,$array[$text_char], $data);
$encrypted .= $array[$text_char];
}
Running str_replace in the loop results in substitutions of data that have been previously substituted.
Aside from that, to make transport of the encrypted data easier, I'd change:
return $solution."BREAKHERE".gzcompress(base64_encode($data),9);
to:
return base64_encode($solution."BREAKHERE".gzcompress($data,9);
and then make the appropriate changes in the decryptor. The compress data can have null characters and non-printable characters so if you base64 encode the entire result you can pass it around more easily.

Categories