Re: Generating Ads with PHP

218 views
Skip to first unread message

Jose Alcérreca (AdSense API Team)

unread,
Apr 12, 2013, 5:00:03 AM4/12/13
to adsen...@googlegroups.com
Hi Sean


Just list them to get your ad client id. Look for "AFC" as it's most likely the product code you will be using.

Cheers!
Jose

---
Jose Alcérreca
Developer Relations

Google UK Limited
Registered Office: Belgrave House, 76 Buckingham Palace Road, London SW1W 9TQ
Registered in England Number: 3977902


Google Inc.| Developer Relations | AdSense API Team | developers.google.com/adsense



On Friday, 12 April 2013 02:15:40 UTC+1, Sean wrote:
I'm not sure if I'm missing something with this, but I'm having problems generating ads with php and the v4.1 AdSense host.

I have all the access and refresh tokens up and running correctly, got the link set up between the user and all that set up. Now I'm trying to create ads for my test user.

You need to create ads first with the insert statement first, then get them with the generateAdCode function correct?

I copied this out of the example PHP code, doing a print_r it all seems to look correct.

    $adUnit = new Google_AdUnit();
    $adUnit->setName('Test 1');

    $contentAdsSettings = new Google_AdUnitContentAdsSettings();
    $backupOption = new Google_AdUnitContentAdsSettingsBackupOption();
    $backupOption->setType('COLOR');
    $backupOption->setColor('ffffff');
    $contentAdsSettings->setBackupOption($backupOption);
    $contentAdsSettings->setSize('SIZE_600_160'); //width then height
    $contentAdsSettings->setType('TEXT');
    $adUnit->setContentAdsSettings($contentAdsSettings);

    $customStyle = new Google_AdStyle();
    $colors = new Google_AdStyleColors();
    $colors->setBackground('ffffff');
    $colors->setBorder('000000');
    $colors->setText('000000');
    $colors->setTitle('000000');
    $colors->setUrl('0000ff');
    $customStyle->setColors($colors);
    $customStyle->setCorners('SQUARE');
    $font = new Google_AdStyleFont();
    $font->setFamily('ACCOUNT_DEFAULT_FAMILY');
    $font->setSize('ACCOUNT_DEFAULT_SIZE');
    $customStyle->setFont($font);
    $adUnit->setCustomStyle($customStyle);



This is the insert statement I'm running:  ($pubID is the users pubID, $clientID is the app client ID for the project) (I think that is correct right?)

$result = $adsensehostService->accounts_adunits->insert($pubID, $clientID, $adUnit);

This is the error I'm getting

Fatal error: Uncaught exception 'Google_ServiceException' with message 'Error calling POST https://www.googleapis.com/adsensehost/v4.1/accounts/pub-25716040XXXXXXXXX/adclients/74197XXXXXXXXXX.apps.googleusercontent.com/adunits?key=AIzaSyDCXXXXXXXXXvkxQBU1JYplaoNwc5-E: (404) Ad client not found.'

I've been trying to figure out what the Ad client not found means, or if I'm even on the right track for making ads. 

Any help would be appreciated.

Sean





Jose Alcérreca (AdSense API Team)

unread,
Apr 15, 2013, 1:06:13 PM4/15/13
to adsen...@googlegroups.com
Hi there,

The ad client id would be ca-host-pub-15831278XXXXXXXX.

Cheers!

---
Jose Alcérreca
Developer Relations

Google UK Limited
Registered Office: Belgrave House, 76 Buckingham Palace Road, London SW1W 9TQ
Registered in England Number: 3977902


Google Inc.| Developer Relations | AdSense API Team | developers.google.com/adsense


On Friday, 12 April 2013 19:04:00 UTC+1, Sean wrote:
Do you need to get the list of available product types from AdClient first to build ads?

Like this part of the returned array from the get function
Array ( [kind] => adsensehost#adClient [id] => ca-host-pub-15831278XXXXXXXX [productCode] => AFC_HOST [supportsReporting] => 1 )

for the insert statement for the adUnit function.  accountID is the pubID of the end user correct? So what value should be for adClientID?   I think that is where I'm stuck.

Sean

Jose Alcérreca (AdSense API Team)

unread,
Apr 16, 2013, 4:39:31 AM4/16/13
to adsen...@googlegroups.com
Hi Sean,

You're using the Client ID from the APIs console, not the "ca-pub-XXXXXXXX" ad client ID.

/adsensehost/v4.1/accounts/pub-25716040XXXXXXXXX/adclients/74197XXXXXXXXXX.apps.googleusercontent.com/adunits

Cheers,
Jose

---
Jose Alcérreca
Developer Relations

Google UK Limited
Registered Office: Belgrave House, 76 Buckingham Palace Road, London SW1W 9TQ
Registered in England Number: 3977902


Google Inc.| Developer Relations | AdSense API Team | developers.google.com/adsense


On Tuesday, 16 April 2013 01:50:04 UTC+1, Sean wrote:
When using the the sample php code, and plug in all the numbers for both IDs I get the following error about adclient not found. Which makes no sense as to why it is doing that.


Fatal error: Uncaught exception 'Google_ServiceException' with message 'Error calling POST https://www.googleapis.com/adsensehost/v4.1/accounts/pub-25716040XXXXXXXXX/adclients/74197XXXXXXXXXX.apps.googleusercontent.com/adunits?key=AIzaSyDCXXXXXXXXXvkxQBU1JYplaoNwc5-E: (404) Ad client not found.'

I've been trying to use the try it console from here : https://developers.google.com/adsense/host/v4.1/accounts/adunits/insert

When i fill in the accountID and adClientID and leave the adUnit section blank, I can get the same AdClient not found error as above.  And when I fill in the AdCode section to see what response I get, I get this

503 Service Unavailable
- Show headers -
{
"error": {
"errors": [
{
"domain": "global",
"reason": "backendError",
"message": "Backend Error"
}
],
"code": 503,
"message": "Backend Error"
}
}


I'm wondering if either I'm still missing something, or because I can't get it to work right that there is either a bug in the php sample code that isn't getting the ad unit info through the API correctly, or if something else isn't working quite right.

Sean

Jose Alcérreca (AdSense API Team)

unread,
Apr 17, 2013, 7:07:25 AM4/17/13
to adsen...@googlegroups.com
Hi Sean,

The problem in that case is that you're not inserting a complete ad unit. Please check the samples to see how to define it correctly. Your first code in PHP looks good.

Cheers,
Jose

---
Jose Alcérreca
Developer Relations

Google UK Limited
Registered Office: Belgrave House, 76 Buckingham Palace Road, London SW1W 9TQ
Registered in England Number: 3977902


Google Inc.| Developer Relations | AdSense API Team | developers.google.com/adsense


On Tuesday, 16 April 2013 23:13:33 UTC+1, Sean wrote:
I've been using this page to make sure I'm using everything correctly to make the adUnit: https://developers.google.com/adsense/host/v4.1/accounts/adunits/insert

accountID: pub-25716040XXXXXXXX
adClientID: 74197XXXXXXXX.apps.googleusercontent.com

I filled out the fancy html5 box for defining an adUnit to come up with this

{
"kind": "adsensehost#adUnit",
"name": "qq",
"contentAdsSettings": {
"size": "SIZE_160_600",
"type": "TEXT"
},
"code": "hjktrdres421rew",
"id": "jhftderszzresw5r56"
}

And it still comes back with this.

503 Service Unavailable

{
"error": {
"errors": [
{
"domain": "global",
"reason": "backendError",
"message": "Backend Error"
}
],
"code": 503,
"message": "Backend Error"
}
}


I think I'm using all the proper everything to make the adUnits right? Then once I create them, I can run the getAdCode function to get the actual Javascript for them correct?

Sean

Jose Alcérreca (AdSense API Team)

unread,
Apr 17, 2013, 1:00:42 PM4/17/13
to adsen...@googlegroups.com
Hi,
 
Do an adclient.list to the publisher and get the ad client id from there. You're using a wrong ad client id, the host's. You should get the publisher's ad client id.

Cheers,
Jose

---
Jose Alcérreca
Developer Relations

Google UK Limited
Registered Office: Belgrave House, 76 Buckingham Palace Road, London SW1W 9TQ
Registered in England Number: 3977902


Google Inc.| Developer Relations | AdSense API Team | developers.google.com/adsense


On Wednesday, 17 April 2013 17:45:08 UTC+1, Sean wrote:
I'm still having problems getting this to work though using the sample PHP code. I'm plugging in all the numbers and I can't seem to make it work. Is there anything I'm missing here? I still don't know why it is giving me the (404) Ad client not found when I try and insert the ad.


I set up the ad unit as so.


$adUnit = new Google_AdUnit();
    $adUnit->setName('Test 1');

    $contentAdsSettings = new Google_AdUnitContentAdsSettings();
    $backupOption = new Google_AdUnitContentAdsSettingsBackupOption();
    $backupOption->setType('COLOR');
    $backupOption->setColor('ffffff');
    $contentAdsSettings->setBackupOption($backupOption);
    $contentAdsSettings->setSize('SIZE_160_600'); //width then height //ledarboard

    $contentAdsSettings->setType('TEXT');
    $adUnit->setContentAdsSettings($contentAdsSettings);

    $customStyle = new Google_AdStyle();
    $colors = new Google_AdStyleColors();
    $colors->setBackground('ffffff');
    $colors->setBorder('000000');
    $colors->setText('000000');
    $colors->setTitle('000000');
    $colors->setUrl('0000ff');
    $customStyle->setColors($colors);
    $customStyle->setCorners('SQUARE');
    $font = new Google_AdStyleFont(); //added Google_ to tihs to make it work

    $font->setFamily('ACCOUNT_DEFAULT_FAMILY');
    $font->setSize('ACCOUNT_DEFAULT_SIZE');
    $customStyle->setFont($font);
    $adUnit->setCustomStyle($customStyle);

//print the ad unit to make sure it's all good.
print_r($adUnit);
echo "<BR><BR>";

print out the vars I'm using to make sure it's right

    echo "clientid: " . $clientID . "<BR>";
    echo "pubID: " . $pubID . "<BR>";
    echo "thisAdClientID: " . $thisAdClientID . "<BR>";

//now do the insert
    $result = $adsensehostService->accounts_adunits->insert($pubID, $clientID, $adUnit); // $clientID $thisAdClientID $clientID $pubID
 
      echo "<BR><BR>r: " . $result . "<BR>xx<BR>";




Google_AdUnit Object ( [code] => [__contentAdsSettingsType:protected] => Google_AdUnitContentAdsSettings [__contentAdsSettingsDataType:protected] => [contentAdsSettings] => Google_AdUnitContentAdsSettings Object ( [__backupOptionType:protected] => Google_AdUnitContentAdsSettingsBackupOption [__backupOptionDataType:protected] => [backupOption] => Google_AdUnitContentAdsSettingsBackupOption Object ( [color] => ffffff [type] => COLOR [url] => ) [size] => SIZE_160_600 [type] => TEXT ) [__customStyleType:protected] => Google_AdStyle [__customStyleDataType:protected] => [customStyle] => Google_AdStyle Object ( [__colorsType:protected] => Google_AdStyleColors [__colorsDataType:protected] => [colors] => Google_AdStyleColors Object ( [background] => ffffff [border] => 000000 [text] => 000000 [title] => 000000 [url] => 0000ff ) [corners] => SQUARE [__fontType:protected] => Google_AdStyleFont [__fontDataType:protected] => [font] => Google_AdStyleFont Object ( [family] => ACCOUNT_DEFAULT_FAMILY [size] => ACCOUNT_DEFAULT_SIZE ) [kind] => ) [id] => [kind] => [__mobileContentAdsSettingsType:protected] => Google_AdUnitMobileContentAdsSettings [__mobileContentAdsSettingsDataType:protected] => [mobileContentAdsSettings] => [name] => Test 1 [status] => )

clientid: 7419718XXXXX.apps.googleusercontent.com
pubID: pub-25716040709XXXXXX
thisAdClientID: ca-host-pub-158312780XXXXXX

Fatal error: Uncaught exception 'Google_ServiceException' with message 'Error calling POST https://www.googleapis.com/adsensehost/v4.1/accounts/pub-2571604070xxxxx/adclients/741971xxxxxxx.apps.googleusercontent.com/adunits?key=AIzaSyDC9eQ8QIO_cTLJvkxQXXXXXXXXwc5-E: (404) Ad client not found.' in D:\Inetpub\wwwroot\site\_googleapi\src\io\Google_REST.php:66 Stack trace: #0 D:\Inetpub\wwwroot\site\_googleapi\src\io\Google_REST.php(36): Google_REST::decodeHttpResponse(Object(Google_HttpRequest)) #1 D:\Inetpub\wwwroot\site\_googleapi\src\service\Google_ServiceResource.php(186): Google_REST::execute(Object(Google_HttpRequest)) #2 D:\Inetpub\wwwroot\site\_googleapi\src\contrib\Google_AdsensehostService.php(198): Google_ServiceResource->__call('insert', Array) #3 D:\Inetpub\wwwroot\site\_googleapi\api\build-ads-single.php(153): Google_AccountsAdunitsServiceResource->insert('pub-257160xxxxx...', '7419718xxxxxx.ap...', Object(Google_AdUnit)) #4 {main} thrown in D:\Inetpub\wwwroot\site\_googleapi\src\io\Google_REST.php on line 66


Sean

Jose Alcérreca (AdSense API Team)

unread,
Apr 18, 2013, 5:03:31 AM4/18/13
to adsen...@googlegroups.com
Hi,

Check the samples to make sure you're using the correct set of parameters:

https://code.google.com/p/google-api-php-client/source/browse/trunk/examples/adsensehost/examples/AddAdUnitToPublisher.php

Cheers,
Jose
---
Jose Alcérreca
Developer Relations

Google UK Limited
Registered Office: Belgrave House, 76 Buckingham Palace Road, London SW1W 9TQ
Registered in England Number: 3977902


Google Inc.| Developer Relations | AdSense API Team | developers.google.com/adsense

On Wednesday, 17 April 2013 19:23:59 UTC+1, Sean wrote:
From this page: https://developers.google.com/adsense/host/v4.1/accounts/adunits/insert

Using accountID = pub-257160407xxxxxx and adClientID = ca-pub-257160407xxxxxx (is that the two IDs I'm supposed to be using?)

Using this AdUnit

{
"code": "asfd asdf asf dsadf ",
"contentAdsSettings": {
"backupOption": {
"color": "GREEN",
"type": "COLOR"
},
"size": "SIZE_160_600",
"type": "TEXT"
},
"customStyle": {
"colors": {
"background": "BLUE",
"border": "GREEN",
"text": "YELLOW",
"title": "PURPLE",
"url": "BLACK"
},
"corners": "SQUARE",
"font": {
"family": "TIMES",
"size": "MEDIUM"
},
"kind": "adsensehost#adStyle"
},
"id": "as fdasdf sa d",
"kind": "adsensehost#adUnit",
"name": "adfasfa ",
"status": "ACTIVE",
"mobileContentAdsSettings": {
"markupLanguage": "PHP",
"scriptingLanguage": "PHP",
"size": "SIZE_160_600",
"type": "TEXT"
}

}


I get a backendError message.

Jose Alcérreca (AdSense API Team)

unread,
Apr 18, 2013, 9:13:32 AM4/18/13
to adsen...@googlegroups.com
Hi Sean,

You were using an incorrect ad client id when you last showed PHP code. Have you tried again with the correct ID?


---
Jose Alcérreca
Developer Relations

Google UK Limited
Registered Office: Belgrave House, 76 Buckingham Palace Road, London SW1W 9TQ
Registered in England Number: 3977902


Google Inc.| Developer Relations | AdSense API Team | developers.google.com/adsense


On Thursday, 18 April 2013 14:06:18 UTC+1, Sean wrote:
That is where I pulled my above code from and I haven't been able to get it to work. Which is why I was asking you about it.

Which is also why I've been trying to use the Try it now tester form since that doesn't involve coding. But I can't get that to work either.

What am I doing wrong, and how can I get this working?

Sean

Sean

unread,
Apr 18, 2013, 1:49:37 PM4/18/13
to adsen...@googlegroups.com
This one worked just now for some reason.

insert('pub-25716040709XXXXXX', 'ca-pub-25716040709XXXXXX', $adUnit);  where both of those numbers are the same.  accountID being the publisher accountID, and then adClientID, is the AFC id that you get when you list that users adClients.

Are there fields that if you submit them the adUnit insert that it breaks the code? Like status and other fields that are available to submit values though via the try it now API explorer?

Sean



On Thursday, April 18, 2013 8:25:25 AM UTC-6, Sean wrote:
Yes, I have changed those vars.  I've been trying to say that and get that accross.

Using the above PHP in the first post, for this line of it

insert($pubID, $clientID, $adUnit);

I've been using these values

insert('pub-25716040709XXXXXX', '7419718XXXXX.apps.googleusercontent.com', $adUnit);

That didn't work.

Neither has this.  Which I'm not sure if this is right either.

insert('pub-25716040709XXXXXX', 'ca-pub-25716040709XXXXXX', $adUnit);

Sean

Jose Alcérreca (AdSense API Team)

unread,
Apr 22, 2013, 12:57:49 PM4/22/13
to adsen...@googlegroups.com
Hi Sean,

That is actually the correct combination. They are similar but not the same.

There are fields that, when submitted, can break the request, yes. You have to submit a specific set of parameters.

Cheers,
Jose

---
Jose Alcérreca
Developer Relations

Google UK Limited
Registered Office: Belgrave House, 76 Buckingham Palace Road, London SW1W 9TQ
Registered in England Number: 3977902


Google Inc.| Developer Relations | AdSense API Team | developers.google.com/adsense


Reply all
Reply to author
Forward
0 new messages