Get xml data and insert into database - mysql

How can i insert XML data into my table?
Here is my XML data:
SimpleXMLElement Object
(
[songitem] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[song] => Elona
[album] => AL-F-001
[path] => link1
[calen] => 278
[dl] => d1
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[song] => Evis
[album] => AL-F-002
[path] => link2
[calen] => 279
[dl] => d2
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[song] => Kozeta
[album] => AL-F-003
[path] => link3
[calen] => 280
[dl] => d3
)
)
)
)
want to get song, album,path etc.. and insert into table.

Related

CakePHP 3 - Model - Accessing data from third layer of array

CakePHP 3
My Model
Users has Many Badges
Titles has Many Badges
Badges belongs to Titles
Badges belongs to Users
In my Users View, I want to access the value of Titles but I don't know how.
**Users Controller**
public function view($id = null)
{
$this->viewBuilder()->layout('admin');
TableRegistry::get('Users');
$user = $this->Users->get($id, [
'contain' => ['Badges', 'Projects', 'Reports', 'Rewards']
]);
$this->set(compact('user'));
$this->set('_serialize', ['user']);
}
UsersTable.php
$this->hasMany('Badges', [
'foreignKey' => 'user_id'
]);
TitleTable.php
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Titles', [
'foreignKey' => 'title_id',
'joinType' => 'INNER'
]);
BadgesTable.php
$this->hasMany('Badges', [
'foreignKey' => 'title_id'
]);
App\Model\Entity\User Object
(
[id] => 1
[firstname] => Jl
[lastname] => C
[email] => m
[username] => admin
[password] =>
[profile_pic] => /img/noimage.jpg
[report_count] => 0
[role] => admin
[reputation] => 999999
[account_status] => Active
[confirmation] => 1
[site_name] => C
[site_email] => mi
[created] => Cake\I18n\FrozenTime Object
(
[time] => 2017-02-27T13:46:51+08:00
[timezone] => Asia/Manila
[fixedNowTime] =>
)
[modified] => Cake\I18n\FrozenTime Object
(
[time] => 2017-03-01T10:37:50+08:00
[timezone] => Asia/Manila
[fixedNowTime] =>
)
[badges] => Array
(
[0] => App\Model\Entity\Badge Object
(
[id] => 1
[user_id] => 1
[title_id] => 11
[created] => Cake\I18n\FrozenTime Object
(
[time] => 2017-02-28T15:16:08+08:00
[timezone] => Asia/Manila
[fixedNowTime] =>
)
[modified] => Cake\I18n\FrozenTime Object
(
[time] => 2017-02-28T15:16:08+08:00
[timezone] => Asia/Manila
[fixedNowTime] =>
)
[[new]] =>
[[accessible]] => Array
(
[*] => 1
)
[[dirty]] => Array
(
)
[[original]] => Array
(
)
[[virtual]] => Array
(
)
[[errors]] => Array
(
)
[[invalid]] => Array
(
)
[[repository]] => Badges
)
)
[[new]] =>
[[accessible]] => Array
(
[*] => 1
)
[[repository]] => Users
)
What I want is to get the data from title table (i.e name)
[badges] => Array
(
[0] => App\Model\Entity\Badge Object
(
[id] => 1
[user_id] => 1
[title_id] => 11
[created] => Cake\I18n\FrozenTime Object
In your contain, you can use 'Model.Model' to get further related data like so :
$user = $this->Users->get($id, [
'contain' => ['Badges.Titles', 'Projects', 'Reports', 'Rewards']
]);

How will i print the result in view in CodeIgniter?

I am trying to get a result in view returned as array in controller.This is a result of an api call using the json.
I need to display the results in the view. There is design integrated in view to display it.
How could i do it in CodeIgniter?
Function call:
$data['nearme_values'] = $this->swsdk->nearMe($lat, $lon, $radius, $page, $size);
This is the var_dumb result of the function call.
stdClass Object
( [content] => Array
( [0] => stdClass Object
( [id] => 1 [name] => Make America Great
[screenShotUrl] => http://52.77.190.221/SW/speaker_wire_images/streamSShot/stream-1479113014667.jpg
[type] => PUBLIC [status] => RECORDED [userId] => 1 [userHandle] => abhilekh
[channelId] => 1 [channelName] => Sports [likes] => 0 [views] => 0
[liveViewers] => 0
)
[1] => stdClass Object
( [id] => 2 [name] => Make America Great
[screenShotUrl] => http://52.77.190.221/SW/speaker_wire_images/streamSShot/stream-1479110563909.jpg
[type] => PUBLIC [status] => RECORDED [userId] => 1 [userHandle] => abhilekh
[channelId] => 2 [channelName] => Politics [likes] => 0 [views] => 0
[liveViewers] => 0
)
[2] => stdClass Object
( [id] => 3 [name] => Make America Great
[screenShotUrl] => http://52.77.190.221/SW/speaker_wire_images/streamSShot/stream-1479109601008.jpg
[type] => PUBLIC [status] => RECORDED [userId] => 2 [userHandle] => sudhanshu
[channelId] => 3 [channelName] => Fun [likes] => 0 [views] => 0
[liveViewers] => 0
)
[3] => stdClass Object
( [id] => 4 [name] => Make America Great
[screenShotUrl] => http://52.77.190.221/SW/speaker_wire_images/profiles/pic-130.jpg
[type] => PUBLIC [status] => RECORDED [userId] => 2 [userHandle] => sudhanshu
[channelId] => 4 [channelName] => Art [likes] => 0 [views] => 0
[liveViewers] => 0
)
) [last] => 1 [totalPages] => 1 [totalElements] => 4
[numberOfElements] => 4 [sort] => [first] => 1
[size] => 10 [number] => 0
)
I am new to the CodeIgniter. Please support with sample code.
if you are gonna call as API, no need to put in view.
Just echo like this in controller's method.
$nearme_values = $this->swsdk->nearMe($lat,$lon,$radius,$page,$size);
echo json_encode($nearme_values);
Displaying in View.
// in Controller.
$data['nearme_values'] = $this->swsdk->nearMe($lat, $lon, $radius, $page, $size);
$this->load->view('ur_view_file', $data);
// in view
foreach ($nearme_values->content as $nearme) {
echo $nearme->id .' / '. $nearme->screenShotUrl .' / '. $nearme->type .'<br>';
}

Facebook AdSet API "Your Budget is Too Low" error

Problem
I made a request to Facebook's Ad Set API attempting to create an ad set with the following JSON blob, and the error I received was "Your Budget Is Too Low. The minimum budget for this ad set is $32.04."
The ad set is set to use auto_bid and a valid lifetime_target. The ad set runs for 31 days, and with a $1 minimum daily budget, setting $64 for lifetime budget should've been sufficient.
Attempted Solutions
I tried to set a higher lifetime budget: from 64 to 1000 to 5000. 1000 did not work in this case, but 5000 did.
JSON Blob Used
Array
(
[account_id] =>
[adset_schedule] =>
[bid_amount] =>
[billing_event] => IMPRESSIONS
[budget_remaining] =>
[campaign_id] => 6054825216096
[created_time] =>
[creative_sequence] =>
[daily_budget] =>
[end_time] => 2016-11-08T07:59:59+0000
[id] =>
[is_autobid] => 1
[lifetime_budget] => 64
[lifetime_imps] =>
[name] => Some-Test-Campaign
[optimization_goal] => LINK_CLICKS
[pacing_type] =>
[recommendations] =>
[rf_prediction_id] =>
[start_time] => 2016-10-07T07:00:00+0000
[updated_time] =>
[targeting] => FacebookAds\Object\TargetingSpecs Object
(
[data:protected] => Array
(
[genders] =>
[age_min] =>
[age_max] =>
[geo_locations] => Array
(
[zips] => Array
(
[0] => Array
(
[key] => US:98004
)
[1] => Array
(
[key] => US:98005
)
[2] => Array
(
[key] => US:98006
)
[3] => Array
(
[key] => US:98007
)
[4] => Array
(
[key] => US:98008
)
[5] => Array
(
[key] => US:98009
)
[6] => Array
(
[key] => US:98011
)
[7] => Array
(
[key] => US:98014
)
[8] => Array
(
[key] => US:98015
)
[9] => Array
(
[key] => US:98019
)
[10] => Array
(
[key] => US:98021
)
)
)
[geo_markets] =>
[excluded_geo_locations] => Array
(
[countries] => Array
(
)
)
[exclusions] =>
[user_adclusters] =>
[interests] =>
[user_os] =>
[user_device] =>
[wireless_carrier] =>
[page_types] => Array
(
[0] => desktopfeed
[1] => mobilefeed
[2] => mobileexternal
[3] => rightcolumn
)
[connections] =>
[excluded_connections] =>
[family_statuses] =>
[friends_of_connections] =>
[flexible_spec] => Array
(
[0] => Array
(
[interests] => Array
(
[0] => Array
(
[id] => 6003277229371
)
)
[behaviors] => Array
(
[0] => Array
(
[id] => 6017447625983
)
)
)
)
[behaviors] =>
[relationship_statuses] =>
[interested_in] =>
[life_events] =>
[location_types] =>
[politics] =>
[markets] =>
[industries] =>
[income] =>
[net_worth] =>
[home_type] =>
[home_ownership] =>
[home_value] =>
[ethnic_affinity] =>
[generation] =>
[household_composition] =>
[moms] =>
[office_type] =>
[education_schools] =>
[education_statuses] =>
[college_years] =>
[education_majors] =>
[work_employers] =>
[work_positions] =>
[locales] =>
[zips] =>
[custom_audiences] =>
[custom_locations] =>
[excluded_custom_audiences] =>
[dynamic_audience_ids] =>
[product_audience_specs] =>
[excluded_product_audience_specs] =>
)
)
[promoted_object] =>
[adlabels] =>
[product_ad_behavior] =>
[execution_options] =>
[configured_status] =>
[effective_status] =>
)
I shortened the targeting for simplicity sake, but there's over 100 postal codes being targetd.
The solution here is that lifetime_budget is denoted in cents, so $64 should be 6400.
You may check if the budget you set is not less of required.
Perform this check at the endpoint:
https://graph.facebook.com/v2.12/act_{ads_account_id}/minimum_budgets
With headers:
Authorization: Bearer {your token with 'ads_management' rights to this account}
Returns:
{
"data": [
{
"currency": "AED",
"min_daily_budget_imp": 300,
"min_daily_budget_video_views": 300,
"min_daily_budget_high_freq": 750,
"min_daily_budget_low_freq": 6000
},
{
"currency": "ARS",
"min_daily_budget_imp": 400,
"min_daily_budget_video_views": 400,
"min_daily_budget_high_freq": 1000,
"min_daily_budget_low_freq": 8000
}
]
}
PS. I see, that my response is 2 years late, but someone may find it useful.

How to read youtube data api v3 response in php

I have following code:
$yt_profiles = $youtube->channels->listChannels('brandingSettings', array(
'mine' => 'true',
));
That returns the following output:
Google_Service_YouTube_ChannelListResponse Object
(
[collection_key:protected] => items
[etag] => "WFPuK6TsnblcGPcnMex79s42ynQ/sTnuE1bHO-tokx_mFFDt1ybN90g"
[eventId] =>
[itemsType:protected] => Google_Service_YouTube_Channel
[itemsDataType:protected] => array
[kind] => youtube#channelListResponse
[nextPageToken] =>
[pageInfoType:protected] => Google_Service_YouTube_PageInfo
[pageInfoDataType:protected] =>
[prevPageToken] =>
[tokenPaginationType:protected] => Google_Service_YouTube_TokenPagination
[tokenPaginationDataType:protected] =>
[visitorId] =>
[modelData:protected] => Array
(
[pageInfo] => Array
(
[totalResults] => 1
[resultsPerPage] => 1
)
[items] => Array
(
[0] => Array
(
[kind] => youtube#channel
[etag] => "WFPuK6TsnblcGPcnMex79s42ynQ/ecOcHFmWyWQ7ToCD7-B1L36b4L4"
[id] => UCQO6uXy5maTpYvSa_yM--Bw
[brandingSettings] => Array
(
[channel] => Array
(
[title] => Vasim Padhiyar
[showRelatedChannels] => 1
[featuredChannelsTitle] => Featured Channels
[featuredChannelsUrls] => Array
(
[0] => UCw-TnDmYDQyjnZ5qpVWUsSA
)
[profileColor] => #000000
)
[image] => Array
(
[bannerImageUrl] => http://s.ytimg.com/yts/img/channels/c4/default_banner-vfl7DRgTn.png
)
[hints] => Array
(
[0] => Array
(
[property] => channel.featured_tab.template.string
[value] => Everything
)
[1] => Array
(
[property] => channel.banner.image_height.int
[value] => 0
)
[2] => Array
(
[property] => channel.modules.show_comments.bool
[value] => True
)
)
)
)
)
)
[processed:protected] => Array
(
)
)
I want to loop through modelData:protected variable to get the list of channels and its items data. Its json object so $yt_profiles->modelData:protected not working while accessing. Please help me to solve this issue.
Thanks in advance
You can reach it as in an array:
print_r ($yt_profiles['modelData']);
Your request:
$yt_profiles = $youtube->channels->listChannels('brandingSettings', array(
'mine' => 'true',
));
To acces to values Channel title for example:
$titleChannel = $yt_profiles["items"][0]["brandingSettings"]["channel"]["title"];
To acces to values bannerImageUrl for example:
$banner = $yt_profiles["items"][0]["brandingSettings"]["image"]["bannerImageUrl"];
Does this help you?
$yt_profiles = $youtube->channels->listChannels('id, brandingSettings', array('mine' => 'true',));
foreach ($yt_profiles['modelData']['items'] as $searchResult) {
switch ($searchResult['kind']) {
case 'youtube#channel':
$channels[]= array('id'=> $searchResult['id'],
'brandingSettings'=> $searchResult['brandingSettings']);
break;
}
}

How to order sql results by counting a field in CakePHP 2.0

When I perform the following:
$popular_posts = $this->Blog->find('all', array('limit' => 5));
I get the following:
Array
(
[0] => Array
(
[Blog] => Array
(
[id] => 4fcfb37d-3eb0-4ec2-a744-175c987a2b72
[title] => This is a post example2
[short_description] => You've stumbled across our blog! Welcome! Here
[created] => 2012-06-06 21:46:05
[modified] => 2012-06-07 16:01:24
)
[Reply] => Array
(
[0] => Array
(
[id] => 4fcfb305-0c58-421b-9149-175c987a2b72
)
[1] => Array
(
[id] => 4fd0ae9e-dca0-4afe-862c-1258987a2b72
)
)
),
[1] ...
[2] ...
)
How can I order the results by the number of Reply ??? (desc)?
Try this
$data = $this->Blog->find('all',array('group' =>array('Reply.id'),
'order' => array('COUNT(Reply.id) DESC'),
'limit'=> 5));