Translate value before sending information - php

is there a away to translate values in php using either google api translate or any other api...
<?php
// 1.- Query to get information
// 2.- build array with that query
// Example array from query
$data = array(
'0' => array (
'name' => 'Zapatos',
'color' => 'Verde'
),
'1' => array (
'name' => 'Casa',
'color' => 'Rosa'
),
);
// Now that the array has been build, lets make a translation
// Which I have no idea how to do that but the final array should be
$final = array(
'0' => array (
'name' => 'Zapatos',
'color' => 'Verde',
'name_en' => 'Shoes',
'color_en' => 'Green'
),
'1' => array (
'name' => 'Casa',
'color' => 'Rosa',
'name_en' => 'House',
'color_en' => 'Pink'
),
);
is this process possible or am I just dreaming?
I have very little knowledge on how exactly Goolge API works since I only use the Google Translate widget and the translation is after you present the information but in this case we need to make a translation before presenting the information...

Google translate API is a paid service. You need to get a api key from google api services :
google translate API
After that, you can make a curl to google api after getting your results from query :
sample url for curl :
https://www.googleapis.com/language/translate/v2?key=INSERT-YOUR-KEY&source=en&target=de&q=Hello%20world&q=My%20name%20is%20Jeff
You will get the results as JSON object,do json_decode and add results to your array.

Related

LinkedIn API UGC Post Mention: share commentary is invalid

I'm using LinkedIn API to create posts. I also use the LinkedIn Mentions to create posts with mentions.
I can successfully create a post with mention except if I add in the message emojis or accents. Without mentions feature, I'm able to create any post with success.
Posts that work:
Hello Stackoverflow
Hello 馃槃 Stackoverflow
Hell贸贸 Stackoverflow
Hello Stackoverflow (with mention to the linkedin page)
Posts that do not work:
Hello 馃ぃ Stackoverflow (with mention to the linkedin page)
Hell贸贸 Stackoverflow (with mention to the linkedin page)
I receive the following error:
com.linkedin.content.common.ResponseException: share commentary is
invalid
I send the following data to LinkedIn:
array (
'author' => 'urn:li:organization:X',
'lifecycleState' => 'PUBLISHED',
'visibility' =>
array (
'com.linkedin.ugc.MemberNetworkVisibility' => 'PUBLIC',
),
'specificContent' =>
array (
'com.linkedin.ugc.ShareContent' =>
array (
'shareCommentary' =>
array (
'text' => 'Hell贸贸 Stackoverflow',
'attributes' =>
array (
0 =>
array (
'length' => 13,
'start' => 8,
'value' =>
array (
'com.linkedin.common.CompanyAttributedEntity' =>
array (
'company' => 'urn:li:organization:X',
),
),
),
),
),
'shareMediaCategory' => 'NONE',
),
),
)
Solved.
Three issues needed to be fixed:
Switch from strpos to mb_strpos
Switch from strlen to mb_strlen
Count emojis in the body text as length 2, instead of 1
I'm now able to send text with accents, special chars & emojis with mentions.

mongoDB, PHP update specific value not all the values

I am having a problem in updating values i get from web service ..
$collection = $modb->$table;
$collection->update(array("id" => (int)$row['id']),
array('$set' => array(
"user_id" => (int)$post_data_array['user_id'],
"story" => (int)$post_data_array['story'],
"surprize_sub1" => (int)$post_data_array['surprize_sub1'],
"surprize_sub2" => (int)$post_data_array['surprize_sub2'],
"surprize_sub3" => (int)$post_data_array['surprize_sub3'],
"exr_solve" => (int)$post_data_array['exr_solve'],
"exr_assessmnt" => (int)$post_data_array['exr_assessmnt'],
"exr_refresh" => (int)$post_data_array['exr_refresh'],
"sound_control" => (int)$post_data_array['sound_control'],
"clock_control" => (int)$post_data_array['clock_control'],
"switch_user" => (int)$post_data_array['switch_user'],
"exr_print" => (int)$post_data_array['exr_print'],
"write_on_wall" => (int)$post_data_array['write_on_wall'],
"switch_letter" => (int)$post_data_array['switch_letter'],
"view_controls" => (int)$post_data_array['view_controls'],
)));
I get these values from end users.. i want the specific field sent to be updated without loosing all the rest of data ..
in this code only sent data is set while removing the rest .. i want to change only sent ones by keeping the rest as they are, please advice
you need to use updateOne instead of update .
updateOne
Use the MongoDB\Collection::updateOne() method to update a single document matching a filter.
$collection = $modb->$table;
$collection->updateOne(array("id" => (int)$row['id']),
array('$set' => array(
// .... array elements
)));

Zend 2 Form Validator 'InArray' - Complicated array returning invalid

I am trying to implement the 'InArray' validator in Zend 2 on a form and it keeps on returning invalid. Here is the code:
The Form Element setup:
$enquiryType = new Element\Select('enquiryType');
$enquiryType->setLabel('* What is your enquiry about?')
->setLabelAttributes(array('class' => 'sq-form-question-title'))
->setAttribute('class', 'sq-form-field required')
->setValueOptions($enquiryTypes);
The Filter/Validator setup:
$enquiryType = new Input('enquiryType');
$enquiryType->getValidatorChain()
->addByName('InArray', array('haystack' => $enquiryTypes));
And here is the array that gets passed into $enquiryTypes via the module.config.php file
'enquiryCategories' => array(
'empty_option' => 'Please select an option',
array(
'label' => 'General enquiries',
'options' => array(
'skype' => 'Skype with your library feedback',
'question' => 'Ask a question',
'feedback' => 'Library feedback',
'eresource' => 'Electronic resources (e.g. e-book, e-journal, database)',
'webbridge' => 'WebBridge Problems',
'dro' => 'DRO'
)
),
array(
'label' => 'Application to review',
'options' => array(
'10400' => 'I\'ve returned the item',
'10401' => 'I didn\'t know about overdue points but now I have some',
'10402' => 'Why did I get this invoice?',
'10403' => 'The item I borrowed is lost',
'10404' => 'The item I borrowed has been damaged',
'10405' => 'I never got/had the book',
'10406' => 'Other'
)
)
),
I have tried different variations of this (using the recursive validation also) but have not been able to work this out.
One thing that I'd try, is as follows:
$enquiryType = new Input('enquiryType');
$enquiryType->getValidatorChain()
->addByName('InArray', array('haystack' => $enquiryTypes['enquiryCategories']));
The reason I say that, is that it looks like you might be creating an array inside of the array perhaps. Unless I've misunderstood the description.
If that isn't working for you then maybe you might need to explore the Explode option as pointed out in the following question
ZF2: How do I use InArray validator to validate Multiselect form element?
Good luck.
I finally got a working solution for this. I feel there should be a better solution but I could not find one.
Here is the filter I used:
$enquiryType = new Input('enquiryType');
$enquiryType->getValidatorChain()
->addByName('InArray', array('haystack' => array_keys(
$enquiryTypes[0]['options']+$enquiryTypes[1]['options']
)));
$enquiryType->getFilterChain()
->attach(new Filter\StringToLower())
->attachByName('StripTags');
Basically I had to disect the array options into a straight associative array. As the array remains static in this instance then this works well.
If the data becomes dynamic then something more would be required (loop through and find the 'option' key, add children to filter array etc...)

Create campaign with dynamic segment using MailChimp API V3.0

Using MailChimp API V3.0 to create a campaign.
I want to create a campaign that sends to users with a specific interest. It looks like this is possible in the docs, but I've tried every permutation I can think of. I can create the campaign fine as long as I leave out the segment_ops member. Does anyone have an example of PHP code that will do it?
It seems that interests are handled strangely since you don't include the interest-category when setting a users interests via the API. I'm not sure how this affects campaign creation.
I've gotten this to work, API definition can be found here https://us1.api.mailchimp.com/schema/3.0/Segments/Merge/InterestSegment.json
Interests have to be grouped under interest categories (called 'Groups' in some parts of the UI).
Here is the JSON for the segment_opts member of the recipients array:
"segment_opts": {
"match": "any",
"conditions": [{
"condition_type": "Interests",
"field": "interests-31f7aec0ec",
"op": "interestcontains",
"value": ["a9014571b8", "5e824ac953"]
}]
}
Here is the PHP array version with comments. The 'match' member refers to the the rules in the array of 'conditions'. The segment can match any, all, or none of the conditions. This example has only one condition, but others can be added as additional arrays in the 'conditions' array:
$segment_opts = array(
'match' => 'any', // or 'all' or 'none'
'conditions' => array (
array(
'condition_type' => 'Interests', // note capital I
'field' => 'interests-31f7aec0ec', // ID of interest category
// This ID is tricky: it is
// the string "interests-" +
// the ID of interest category
// that you get from MailChimp
// API (31f7aec0ec)
'op' => 'interestcontains', // or interestcontainsall, interestcontainsnone
'value' => array (
'a9014571b8', // ID of interest in that category
'5e824ac953' // ID of another interest in that category
)
)
)
);
You may also send to a Saved segment. The gotcha on this is that the segment_id has to be int. I was saving this value in a db as varchar, and it would not work unless cast to int.
(i am using use \DrewM\MailChimp\MailChimp;)
$segment_id = (int) $methodThatGetsMySegmentID;
$campaign = $MailChimp->post("campaigns", [
'type' => 'regular',
'recipients' => array(
'list_id' => 'abc123yourListID',
'segment_opts' => array(
'saved_segment_id' => $segment_id,
),
),
'settings' => array(
'subject_line' => 'A New Article was Posted',
'from_name' => 'From Name',
'reply_to' => 'info#example.com',
'title' => 'New Article Notification'
)
]);

Google Analytics SEO - PHP API

I have a PHP file which give's me google analytics data, such as pageviews, Top Pages, or Organic Data simple stuff.
Now I neet to get Stuff from the SEO Part.
For example: TOP 50 Search Keywords (with Impression and Clicks)
I can't find any help in the API how to get these values.
this is a example of my api call:
$params = array(
'dimensions' => array('date', 'pagePath', 'pageTitle'),
'metrics' => array('sessions', 'pageviews'),
'sort' => '-ga:sessions',
'filters' => null,
'startdate' => $startdate,
'enddate' => $enddate,
'startindex' => null,
'limit' => 25,
'mapping' => array('pagepath' => 'pagepath', 'pagetitle' => 'pagetitle', 'sessions' => 'visits', 'pageviews' => 'pageviews'),
);
$results = $this->service->data_ga->get($this->profile, $params['startdate'], $params['enddate'], $metrics, $optParams);
The search engine optimization data shown in Google Analytics actually comes from Google Search Console (Webmaster Tools) and is available from the Google Search Console API.
https://developers.google.com/webmaster-tools/?hl=en
You would need to update the Dimensions and Metrics of your query. The traffic dimensions and metrics should be of help.
Below is a simplified query which gets the number of impressions and clicks for various sources and keyword combinations:
$optParams = array(
'dimensions' => 'ga:source,ga:keyword',
'sort' => '-ga:impressions,ga:source',
'filters' => 'ga:medium==organic',
'max-results' => '25');
$metrics = 'ga:impressions,ga:adClicks';
$results = $this->service->data_ga->get(
'ga:XXXX',
'today',
'7daysAgo',
$metrics,
$optParams);
Your code must have some mapping that it does from the values in $param field to the actual dimensions and metric names. I would also encourage you to play around with the query explorer to get a field of what queries are possible.

Categories