<?php
//1 acces token aanmaken
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => '
https://www.googleapis.com/oauth2/v3/token?grant_type=refresh_token&client_id=12345678910&client_secret=asecret',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'Content-length: 0'
),
));
//1 code
$response = curl_exec($curl);
$httpcode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
if ($httpcode == 200) {
$response = json_decode($response, true);
$accesToken = $response['access_token'];
$fullAccesToken = 'Authorization: Bearer ' . $accesToken;
//print_r($accesToken);
//2 budget id aanmaken
$curl2 = curl_init();
curl_setopt_array($curl2, array(
CURLOPT_URL => '
https://googleads.googleapis.com/v11/customers/123456789/campaignBudgets:mutate',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"operations": [
{
"create": {
"name": "My Campaign Budget ysdwpnzgs",
"amountMicros": 500000
}
}
]
}',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'developer-token: ',
'login-customer-id: 123456789',
$fullAccesToken
),
));
//2 code
$response2 = curl_exec($curl2);
$httpcode2 = curl_getinfo($curl2, CURLINFO_HTTP_CODE);
if ($httpcode2 == 200) {
$response2 = json_decode($response2, true);
$budgetId = $response2['results'][0]['resourceName'];
print_r($budgetId);
//3 Create campaign
$curl3 = curl_init();
curl_setopt_array($curl3, array(
CURLOPT_URL => '
https://googleads.googleapis.com/v11/customers/123456789/campaigns:mutate',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS =>'{
"operations": [
{
"create": {
"name": "An campaign testt28",
"status": "PAUSED",
"campaignBudget": "' . $budgetId . '",
"advertisingChannelType": "SEARCH",
"networkSettings": {
"targetGoogleSearch": true,
"targetSearchNetwork": true,
"targetContentNetwork": true,
"targetPartnerSearchNetwork": false
},
"target_spend": {}
}
}
]
}',
CURLOPT_HTTPHEADER => array(
'Content-Type: application/json',
'developer-token: ',
'login-customer-id: 123456789',
$fullAccesToken
),
));
//3 code
$response3 = curl_exec($curl3);
$httpcode3 = curl_getinfo($curl3, CURLINFO_HTTP_CODE);
print_r($response3);
// if ($httpcode3 == 200) {
// $response3 = json_decode($response3, true);
// $campaignId = $response3['results'][0]['resourceName'];
// print_r($campaignId);
// }
// //foutcode
// else {
// echo 'fout bij 3';
// }
}
else {
echo 'fout bij 2';
}
}
else {
echo 'fout bij 1';