I have some PHP code that returns an object. This is from the Facebook PHP Business SDK.
I then run a function to beautify the output of print_r
so we can read it properly.
$account = new AdAccount('act_307978786963089');
$params = array(
'date_preset' => "this_month",
);
$insights = $account->getInsights(array("account_name"), $params);
function my_print_r($thing,$description=false){
echo '<pre style="background:#fff; padding:10px; color:#111; font-family:monospace; font-size:12px; border:1px solid #555">';
if($description) echo '<strong>'.$description.'</strong><br><br>';
print_r($thing);
echo '</pre>';
}
my_print_r($insights);
It returns this:
FacebookAds\Cursor Object
(
[response:protected] => FacebookAds\Http\Response Object
(
[request:protected] => FacebookAds\Http\Request Object
(
[client:protected] => FacebookAds\Http\Client Object
(
[requestPrototype:protected] => FacebookAds\Http\Request Object
(
[client:protected] => FacebookAds\Http\Client Object
*RECURSION*
[headers:protected] =>
[method:protected] => GET
[protocol:protected] => https://
[domain:protected] =>
[path:protected] =>
[graphVersion:protected] =>
[queryParams:protected] =>
[bodyParams:protected] =>
[fileParams:protected] =>
)
[responsePrototype:protected] => FacebookAds\Http\Response Object
(
[request:protected] =>
[statusCode:protected] =>
[headers:protected] =>
[body:protected] =>
[content:protected] =>
)
[defaultRequestHeaders:protected] => FacebookAds\Http\Headers Object
(
[storage:ArrayObject:private] => Array
(
[User-Agent] => fbbizsdk-php-v8.0
[Accept-Encoding] => *
)
)
[adapter:protected] => FacebookAds\Http\Adapter\CurlAdapter Object
(
[curl:protected] => FacebookAds\Http\Adapter\Curl\Curl55 Object
(
[handle:protected] => Resource id #58
)
[opts:protected] => ArrayObject Object
(
[storage:ArrayObject:private] => Array
(
[78] => 10
[13] => 60
[19913] => 1
[42] => 1
[10065] => /home/jack/public_html/FBTests/_/composerNew/vendor/facebook/php-business-sdk/src/FacebookAds/Http/../../../fb_ca_chain_bundle.crt
)
)
[client:protected] => FacebookAds\Http\Client Object
*RECURSION*
)
[caBundlePath:protected] => /home/jack/public_html/FBTests/_/composerNew/vendor/facebook/php-business-sdk/src/FacebookAds/Http/../../../fb_ca_chain_bundle.crt
[defaultGraphBaseDomain:protected] => facebook.com
)
[headers:protected] => FacebookAds\Http\Headers Object
(
[storage:ArrayObject:private] => Array
(
[User-Agent] => fbbizsdk-php-v8.0
[Accept-Encoding] => *
)
)
[method:protected] => GET
[protocol:protected] => https://
[domain:protected] => graph.facebook.com
[path:protected] => /act_307978786963089/insights
[graphVersion:protected] => 8.0
[queryParams:protected] => FacebookAds\Http\Parameters Object
(
[storage:ArrayObject:private] => Array
(
[date_preset] => this_month
[fields] => account_name
[access_token] => EAAKMdiRxiAYBAFgMI98KJpipHpoEd7ZBCXNPCd0ZCiOXf5ckFzawGpvnVfa6Ohnh10y1Hm2ZBoJIlH6w6CJF8knAtqz2OnOMZCmv7RA5W8zNKx6ZCZBJSRZCZANnVNi2nfYCfnIyZAfTkiRyxNjuZBwd8qzi6gZBBtQFcwAlYVR3CoTqqvrxzAYgiZCn
[appsecret_proof] => a13c6f9132c2179e5edae14d3d5dda81d98b8a4ae1f43635ffab1ae311f403a7
)
)
[bodyParams:protected] =>
[fileParams:protected] =>
)
[statusCode:protected] => 200
[headers:protected] => FacebookAds\Http\Headers Object
(
[storage:ArrayObject:private] => Array
(
[http_code] => HTTP/2 200
[etag] => "cc70d7a64f1f2d8f3aed3c50a00b79776a7064ff"
[x-fb-insights-stability-throttle] => {"throttled":false,"backend_qps":{"actual_score":0,"limit":300000},"complexity_score":{"actual_score":0,"limit":16000}}
[expires] => Sat, 01 Jan 2000 00:00:00 GMT
[content-type] => application/json; charset=UTF-8
[facebook-api-version] => v8.0
[x-fb-request-id] => AP6FXiyYQutj6UcUxnlXX_B
[strict-transport-security] => max-age=15552000; preload
[x-fb-trace-id] => H0H221WEtTK
[x-fb-rev] => 1002954105
[cache-control] => private, no-cache, no-store, must-revalidate
[vary] => Accept-Encoding
[pragma] => no-cache
[x-fb-ads-insights-throttle] => {"app_id_util_pct": 0.01,"acc_id_util_pct": 0.67}
[x-business-use-case-usage] => {"307978786963089":[{"type":"ads_insights","call_count":1,"total_cputime":2,"total_time":2,"estimated_time_to_regain_access":0}]}
[access-control-allow-origin] => *
[x-fb-debug] => UHmcev1Xu9DiiAjBAf0De1mafdI7NvWGL0/oEF+TyMI9bZCiZCvUk8mvsWislgWQ2DJTMcpynO548nA7264s8A==
[content-length] => 166
[date] => Wed, 11 Nov 2020 04:42:40 GMT
[alt-svc] => h3-29=":443"; ma=3600,h3-27=":443"; ma=3600
)
)
[body:protected] => {"data":[{"account_name":"B1 - A3 - Butt Seat Cushion","date_start":"2020-11-01","date_stop":"2020-11-11"}],"paging":{"cursors":{"before":"MAZDZD","after":"MAZDZD"}}}
[content:protected] => Array
(
[data] => Array
(
[0] => Array
(
[account_name] => B1 - A3 - Butt Seat Cushion
[date_start] => 2020-11-01
[date_stop] => 2020-11-11
)
)
[paging] => Array
(
[cursors] => Array
(
[before] => MAZDZD
[after] => MAZDZD
)
)
)
)
[api:protected] => FacebookAds\Api Object
(
[session:FacebookAds\Api:private] => FacebookAds\Session Object
(
[appId:protected] => 717388998871046
[appSecret:protected] => 0d3c4be5aebb5861f3a10d6c12b4f487
[accessToken:protected] => EAAKMdiRxiAYBAFgMI98KJpipHpoEd7ZBCXNPCd0ZCiOXf5ckFzawGpvnVfa6Ohnh10y1Hm2ZBoJIlH6w6CJF8knAtqz2OnOMZCmv7RA5W8zNKx6ZCZBJSRZCZANnVNi2nfYCfnIyZAfTkiRyxNjuZBwd8qzi6gZBBtQFcwAlYVR3CoTqqvrxzAYgiZCn
[appSecretProof:protected] => a13c6f9132c2179e5edae14d3d5dda81d98b8a4ae1f43635ffab1ae311f403a7
)
[logger:protected] => FacebookAds\Logger\NullLogger Object
(
)
[httpClient:protected] => FacebookAds\Http\Client Object
(
[requestPrototype:protected] => FacebookAds\Http\Request Object
(
[client:protected] => FacebookAds\Http\Client Object
*RECURSION*
[headers:protected] =>
[method:protected] => GET
[protocol:protected] => https://
[domain:protected] =>
[path:protected] =>
[graphVersion:protected] =>
[queryParams:protected] =>
[bodyParams:protected] =>
[fileParams:protected] =>
)
[responsePrototype:protected] => FacebookAds\Http\Response Object
(
[request:protected] =>
[statusCode:protected] =>
[headers:protected] =>
[body:protected] =>
[content:protected] =>
)
[defaultRequestHeaders:protected] => FacebookAds\Http\Headers Object
(
[storage:ArrayObject:private] => Array
(
[User-Agent] => fbbizsdk-php-v8.0
[Accept-Encoding] => *
)
)
[adapter:protected] => FacebookAds\Http\Adapter\CurlAdapter Object
(
[curl:protected] => FacebookAds\Http\Adapter\Curl\Curl55 Object
(
[handle:protected] => Resource id #58
)
[opts:protected] => ArrayObject Object
(
[storage:ArrayObject:private] => Array
(
[78] => 10
[13] => 60
[19913] => 1
[42] => 1
[10065] => /home/jack/public_html/FBTests/_/composerNew/vendor/facebook/php-business-sdk/src/FacebookAds/Http/../../../fb_ca_chain_bundle.crt
)
)
[client:protected] => FacebookAds\Http\Client Object
*RECURSION*
)
[caBundlePath:protected] => /home/jack/public_html/FBTests/_/composerNew/vendor/facebook/php-business-sdk/src/FacebookAds/Http/../../../fb_ca_chain_bundle.crt
[defaultGraphBaseDomain:protected] => facebook.com
)
[defaultGraphVersion:protected] => 8.0
)
[objects:protected] => Array
(
[0] => FacebookAds\Object\AdsInsights Object
(
[data:protected] => Array
(
[account_currency] =>
[account_id] =>
[account_name] => B1 - A3 - Butt Seat Cushion
[action_values] =>
[actions] =>
[ad_bid_type] =>
[ad_bid_value] =>
[ad_click_actions] =>
[ad_delivery] =>
[ad_id] =>
[ad_impression_actions] =>
[ad_name] =>
[adset_bid_type] =>
[adset_bid_value] =>
[adset_budget_type] =>
[adset_budget_value] =>
[adset_delivery] =>
[adset_end] =>
[adset_id] =>
[adset_name] =>
[adset_start] =>
[age_targeting] =>
[auction_bid] =>
[auction_competitiveness] =>
[auction_max_competitor_bid] =>
[buying_type] =>
[campaign_id] =>
[campaign_name] =>
[canvas_avg_view_percent] =>
[canvas_avg_view_time] =>
[catalog_segment_actions] =>
[catalog_segment_value] =>
[catalog_segment_value_mobile_purchase_roas] =>
[catalog_segment_value_omni_purchase_roas] =>
[catalog_segment_value_website_purchase_roas] =>
[clicks] =>
[conversion_rate_ranking] =>
[conversion_values] =>
[conversions] =>
[converted_product_quantity] =>
[converted_product_value] =>
[cost_per_15_sec_video_view] =>
[cost_per_2_sec_continuous_video_view] =>
[cost_per_action_type] =>
[cost_per_ad_click] =>
[cost_per_conversion] =>
[cost_per_dda_countby_convs] =>
[cost_per_estimated_ad_recallers] =>
[cost_per_inline_link_click] =>
[cost_per_inline_post_engagement] =>
[cost_per_one_thousand_ad_impression] =>
[cost_per_outbound_click] =>
[cost_per_store_visit_action] =>
[cost_per_thruplay] =>
[cost_per_unique_action_type] =>
[cost_per_unique_click] =>
[cost_per_unique_conversion] =>
[cost_per_unique_inline_link_click] =>
[cost_per_unique_outbound_click] =>
[cpc] =>
[cpm] =>
[cpp] =>
[created_time] =>
[ctr] =>
[date_start] => 2020-11-01
[date_stop] => 2020-11-11
[dda_countby_convs] =>
[engagement_rate_ranking] =>
[estimated_ad_recall_rate] =>
[estimated_ad_recall_rate_lower_bound] =>
[estimated_ad_recall_rate_upper_bound] =>
[estimated_ad_recallers] =>
[estimated_ad_recallers_lower_bound] =>
[estimated_ad_recallers_upper_bound] =>
[frequency] =>
[full_view_impressions] =>
[full_view_reach] =>
[gender_targeting] =>
[impressions] =>
[inline_link_click_ctr] =>
[inline_link_clicks] =>
[inline_post_engagement] =>
[instant_experience_clicks_to_open] =>
[instant_experience_clicks_to_start] =>
[instant_experience_outbound_clicks] =>
[interactive_component_tap] =>
[labels] =>
[location] =>
[mobile_app_purchase_roas] =>
[objective] =>
[outbound_clicks] =>
[outbound_clicks_ctr] =>
[place_page_name] =>
[purchase_roas] =>
[qualifying_question_qualify_answer_rate] =>
[quality_ranking] =>
[quality_score_ectr] =>
[quality_score_ecvr] =>
[quality_score_organic] =>
[reach] =>
[social_spend] =>
[spend] =>
[store_visit_actions] =>
[unique_actions] =>
[unique_clicks] =>
[unique_conversions] =>
[unique_ctr] =>
[unique_inline_link_click_ctr] =>
[unique_inline_link_clicks] =>
[unique_link_clicks_ctr] =>
[unique_outbound_clicks] =>
[unique_outbound_clicks_ctr] =>
[unique_video_continuous_2_sec_watched_actions] =>
[unique_video_view_15_sec] =>
[updated_time] =>
[video_15_sec_watched_actions] =>
[video_30_sec_watched_actions] =>
[video_avg_time_watched_actions] =>
[video_continuous_2_sec_watched_actions] =>
[video_p100_watched_actions] =>
[video_p25_watched_actions] =>
[video_p50_watched_actions] =>
[video_p75_watched_actions] =>
[video_p95_watched_actions] =>
[video_play_actions] =>
[video_play_curve_actions] =>
[video_play_retention_0_to_15s_actions] =>
[video_play_retention_20_to_60s_actions] =>
[video_play_retention_graph_actions] =>
[video_thruplay_watched_actions] =>
[video_time_watched_actions] =>
[website_ctr] =>
[website_purchase_roas] =>
[wish_bid] =>
)
I'm fairly decent at PHP, however, I can't figure out how to get data out of this object.
Such as the "account_name", which I can see in this part:
[body:protected] => {"data":[{"account_name":"B1 - A3 - Butt Seat Cushion","date_start":"2020-11-01","date_stop":"2020-11-11"}],"paging":{"cursors":{"before":"MAZDZD","after":"MAZDZD"}}}
Could someone show me all the different ways I can extract data from this object?
My Answer:
'last_7d',_ 'time_increment' => 1,_ 'time_range' => array('since' => '2021-01-01', 'until' => '2021-01-07'),_);_$insights = $account->getInsights(array(), $params);_print_r_beautiful($insights);_This code will return an object containing insights data for the specified Ad Account. To access the data within the object, you can use standard object notation in PHP. For example, if you want to access the 'date_start' field of the first item in the insights data, you can do so like this:_$dateStart = $insights->getData()[0]->date_start;_This will retrieve the value of the 'date_start' field from the first item in the insights data. You can access other fields in a similar manner by replacing 'date_start' with the desired field name.
Rate this post
3 of 5 based on 2200 votesComments