How to get Google Adwords ad cost over time with PHP? - php

So I have successfully established an ouath connection with Google Adwords, but I fail to realize how a request should look like in order to get the ad cost over time. What parameters should I use?
I was hoping to find a more simple solution, like the api calls made to Google Analytics:
public function getAudienceMetricsAll($google_analytics_account_id)
{
$curl = new Curl("https://www.googleapis.com/analytics/v3/data/ga");
$curl->setParams([
"ids" => "ga:" . $google_analytics_account_id,
"dimensions" => "ga:date",
"metrics" => "ga:users,ga:newUsers,ga:percentNewSessions,ga:sessions,ga:bounces,ga:bounceRate,ga:avgSessionDuration,ga:goalStartsAll,ga:pageviews,ga:pageViewsPerSession",
"start-date" => "730daysAgo",
"end-date" => "today",
"max-results" => 10000
]);
$curl->setHeaders([
"Authorization" => "Bearer " . #$this->aim_account->settings["credentials"]["access_token"]
]);
$data = $curl->sendRequest("json");
return $data;
}
I mean, just the url and params I need to send in the body of the request. But I don't seem to be able to find such information.
Thank you for your time! Any help is welcomed!

Related

Why WooCommerce REST API seems not working?

I'm currently working on my admin website using WooCommerce REST API, It works very well since last week but now it shows error like
Error: Sorry, you cannot list resources. [woocommerce_rest_cannot_view]
I don't change anything in my code, The authentication credentials are all fine. Thanks for any advice.
Edited:
protected $url = 'https://mywebsite.com';
protected const CONSUMER_KEY = 'ck_b7144d17091aa01a7a096154a445180c603d****';
protected const CONSUMER_SECRET = 'cs_cdb7705d4ad5bf29aa2b6366c55ac98397e4****';
function __construct(){
$this->woocommerce = new Woo(
$this->url,
self::CONSUMER_KEY,
self::CONSUMER_SECRET,
[
'wp_api' => true,
'version' => 'wc/v2',
]
);
}
This is often as a result of an setup issue, specifically with what values you've passed to verifySsl and queryStringAuth. You didn't post your code, so I can't tell you specifically, but these two resources might help:
https://github.com/woocommerce/wc-api-node/issues/43
https://robotninja.com/blog/fix-common-woocommerce-rest-api-issues/

LARAVEL - Response from SAME url, showing different results

UPDATE 1
#lukepolo found out that the number 23842515550750742 randomly always goes to 23842515550750744 in JS. Any ideas why?
I am using Laravel for an API I am creating. I am using Forge to run the servers.
When I pull a URL for the interface in Chrome, Safari, FireFox, etc., it is returning an ID that has been increased by 2. When I pull that SAME url, with the same exact request in Postman, CRUD, Terminal via cURL, etc., it returns the CORRECT response.
Results from Postman:
accountid: 1334372826597482
campaign:"Facebook"
campaignid: 23842515550750742 <-- CORRECT
client_id: 72
id: 817
Results from Chrome:
accountid:1334372826597482
campaign:"Facebook"
campaignid:23842515550750744 <-- INCORRECT
client_id:72
id:817
I am using the Facebook Marketing API to save the information locally, so I can run massive queries in a much faster resposne than their API would give me. Here is the code for listing the campaigns with basic information:
$this->parseRequestData($request);
$campaigns = FacebookPerformanceOverview::whereIn('client_id', $this->sheet_ids)
->whereIn('accountid', $this->accountIds['facebook'])
->whereIn('campaignid', $this->campaigns)
->whereBetween('date', [$this->start_date, $this->end_date])
->groupBy('campaignid')
->get();
$response = [];
foreach ($campaigns AS $campaign) {
$data = FacebookPerformanceOverview::whereIn('client_id', $this->sheet_ids)
->whereIn('accountid', $this->accountIds['facebook'])
->where('campaignid', $campaign->campaignid)
->whereIn('adsetid', $this->adsets)
->whereBetween('date', [$this->start_date, $this->end_date]);
$response[] = array(
'company' => $campaign->company,
'campaign' => FacebookCampaignSettings::where('campaignid', $campaign->campaignid)->first(),
'impressions' => $data->sum('impressions'),
'clicks' => $data->sum('clicks'),
'cost' => $data->sum('cost'),
'reach' => $data->sum('reach'),
'frequency' => $data->avg('frequency'),
'uniqueclicks' => $data->sum('uniqueclicks'),
'actions' => $data->sum('actions'),
'offsiteconversions' => $data->sum('offsiteconversions'),
'leads' => $data->sum('actionsleadgen') + $this->calculateLeads(),
);
}
return response()->json($response, 200, [], JSON_NUMERIC_CHECK);
I am unable to give the URL, due to it containing sensitive information, but I am confused as to what is going on. I have cleared cache, different browsers, different computers, etc. Web browsers always return the ID in the network tab incorrectly. Any ideas?
Javascript not parsing large number in JSON correctly
Is the answer.

Facebook graph PHP API batch request

I am trying to get to get clicks and spend data for ad campaigns.
I am currently getting all ad campaign ID's with a curl request which returns about 260 ID's.
I want to make a batch request and get the clicks, spend, start and end dates for each ID.
I have found the PHP SDK FacebookRequest() function very confusing so have been trying to make cURL requests.
Would really appreciate some help because I am just stumped at the moment. Is it best to use to FacebookRequest() function or can I continue using the cURL requests?
Not sure if I am on the right track but essentially what I have at the moment is all the campaign ID's which I group with a method, relative_url and body and then pass to a requestHandler function. The code is as follows:
$ad_account_ids = <ad_account_id>;
$ad_campaign_ids = FbAdCampaign::all()->lists('ad_campaign_id')->toArray();
foreach ($ad_campaign_ids as $key => $value) {
$ad_campaign_ids[$key] = array(
"method" => "GET",
"relative_url" => "v2.4/act_".$ad_account_ids."/adgroups",
"body" => "campaign_id=".$value."&redownload=1&bid_type=CPC&bid_info={\"clicks\":150}&creative={\"creative_id\":\"{result=create_creative:$.id}\"}&targeting={\"countries\":[\"US\"]}&name=test1"
);
$fields[] = array(
'access_token' => $access_token,
'batch' => $ad_campaign_ids[$key]
);
// $url = 'https://graph.facebook.com/v2.4/act_'.$ad_campaign_ids.'/adcampaign_groups?access_token='.$access_token;
$url = 'https://graph.facebook.com/';
$data = RequestHandler::curlRequest($url);
Ok got it.
Had to prepend the /GET data to the end of the URL to pass to the request handler.
as follows:
$url = 'https://graph.facebook.com/v2.3/act_'.$ad_account_ids.'/adcampaign_groups?access_token='.$access_token;

LyricAPI - how to get full lyrics?

I want to use LyricAPI hosted on Wikia to get lyrics of songs. But in response I get only part of lyrics:
Await the coming storm, behold the sign in the sun
Chaos upon us spawn! The arrows of time pinpoints us all
Oh, well the maddening laughter growing louder with the memories
Atoms like incense rising, like a thousand candles al[...]
How can I get entire lyrics?
Here is my code:
$client = new SoapClient("http://lyrics.wikia.com/server.php?wsdl", array(
'username' => LYRIC_LOGIN,
'password' => LYRIC_PASS
));
$auth = array(
'username' => LYRIC_LOGIN,
'password' => LYRIC_PASS
);
$header = new SoapHeader('NAMESPACE','Auth',$auth,false);
$client->__setSoapHeaders($header);
$song = $client->getSOTD();
http://api.wikia.com/wiki/Documentation
For legal reasons, the API is not allowed to provide full-lyrics at
the moment. It currently provides a fair-use sample (to verify that
the match is correct) and a link to the page where the full lyrics can
be viewed.

Twitter stream latest tweets not working

I am trying to show the latest tweets on a webpage, but I am unsure of how to do it as I am very new to the twitter api, but here is what I have thus far.
function my_streaming_callback($data, $length, $metrics) {
echo $data;
}
require '../tmhOAuth.php';
$tmhOAuth = new tmhOAuth(array(
'consumer_key' => 'key',
'consumer_secret' => 'secret',
'user_token' => 'token',
'user_secret' => 'secret',
));
$method = 'http://stream.twitter.com/1/statuses//show/:id.json';
//not sure where I am supposed to get the :id from?
$params = array(
//not sure what to put here, I would like to display the last 5 tweets
);
$tmhOAuth->streaming_request('POST', $method, $params, 'my_streaming_callback');
$tmhOAuth->pr($tmhOAuth);
I am using this https://github.com/themattharris/tmhOAuth to authenticate, and then interface with the twitter api, but I am finding it very confusing as all of this is very new to me.
So basically I would just like to try and get the latest tweets, any help would be GREATLY appreciated, as I need to get this done ASAP, thanx in advance! :)
Stream API returns you only the tweets posted after you connected. To get previous tweets you need to use general REST API method: http://dev.twitter.com/doc/get/statuses/show/:id
$tmhOAuth->request('GET', $tmhOAuth->url('1/statuses/show/$id'));
$tmhOAuth->pr(json_decode($tmhOAuth->response['response']));
where $id is the user's id.
If you wish you can use THIS beautiful jQuery plugin. It do the some thing as you require. For more information visit http://tweet.seaofclouds.com/

Categories