Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

no data returned when calling $adCreative->read() without specifying fields #49

Closed
cornernote opened this issue Dec 3, 2014 · 9 comments

Comments

@cornernote
Copy link

I try to read an AdCreative like this:

Api::init($app_id, $app_secret, $access_token);
$adCreative = new AdCreative($ad_creative_id, $account_id);
$adCreative->read();
print_r($adCreative->getData());

But it results in:

Array
(
    [id] => THE ID
    [title] => 
    [actor_id] => 
    [actor_name] => 
    [name] => 
    [object_id] => 
    [object_story_id] => 
    [object_story_spec] => 
    [body] => 
    [image_hash] => 
    [image_file] => 
    [image_url] => 
    [image_crops] => 
    [video_id] => 
    [actor_image_hash] => 
    [link_url] => 
    [object_url] => 
    [url_tags] => 
    [preview_url] => 
    [follow_redirect] => 
    [object_store_url] => 
    [link_deep_link_url] => 
    [call_to_action_type] => 
    [object_type] => 
)

I then try using $adCreative->getFields() as an argument to $adCreative->read():

Api::init($app_id, $app_secret, $access_token);
$adCreative = new AdCreative($ad_creative_id, $account_id);
$adCreative->read($adCreative->getFields());
print_r($adCreative->getData());

Which throws an exception:

Array
        (
            [message] => (#100) Tried accessing nonexisting field (image_file) on node type (AdCreative)
            [type] => OAuthException
            [code] => 100
        )

I then remove image_file from the list of fields:

Api::init($app_id, $app_secret, $access_token);
$adCreative = new AdCreative($ad_creative_id, $account_id);
$fields = $adCreative->getFields();
foreach ($fields as $k => $v) {
    if ($v = 'image_file') {
        unset($fields[$k]);
    }
}
$adCreative->read($fields);
print_r($adCreative->getData());

Which results again in no data:

Array
(
    [id] => THE ID
    [title] => 
    [actor_id] => 
    [actor_name] => 
    [name] => 
    [object_id] => 
    [object_story_id] => 
    [object_story_spec] => 
    [body] => 
    [image_hash] => 
    [image_file] => 
    [image_url] => 
    [image_crops] => 
    [video_id] => 
    [actor_image_hash] => 
    [link_url] => 
    [object_url] => 
    [url_tags] => 
    [preview_url] => 
    [follow_redirect] => 
    [object_store_url] => 
    [link_deep_link_url] => 
    [call_to_action_type] => 
    [object_type] => 
)

I try making my own fields array, and then remove any that cause an exception, and end up with this list:

Api::init($app_id, $app_secret, $access_token);
$adCreative = new AdCreative($ad_creative_id, $account_id);
$fields = array(
    AdCreativeFields::ID,
    AdCreativeFields::TITLE,
    AdCreativeFields::ACTOR_ID,
    AdCreativeFields::ACTOR_NAME,
    AdCreativeFields::NAME,
    AdCreativeFields::OBJECT_ID,
    AdCreativeFields::OBJECT_STORY_ID,
    AdCreativeFields::OBJECT_STORY_SPEC,
    AdCreativeFields::BODY,
    AdCreativeFields::IMAGE_HASH,
    //AdCreativeFields::IMAGE_FILE,
    AdCreativeFields::IMAGE_URL,
    AdCreativeFields::IMAGE_CROPS,
    AdCreativeFields::VIDEO_ID,
    AdCreativeFields::ACTOR_IMAGE_HASH,
    AdCreativeFields::LINK_URL,
    AdCreativeFields::OBJECT_URL,
    AdCreativeFields::URL_TAGS,
    //AdCreativeFields::PREVIEW_URL,
    //AdCreativeFields::FOLLOW_REDIRECT,
    AdCreativeFields::OBJECT_STORE_URL,
    AdCreativeFields::LINK_DEEP_LINK_URL,
    AdCreativeFields::CALL_TO_ACTION_TYPE,
    AdCreativeFields::OBJECT_TYPE,
);
$adCreative->read($fields);
print_r($adCreative->getData());

Now I get what is expected (i have replaced the actual data incase of any security issue sharing it):

Array
(
    [id] => THE ID
    [title] => AD TITLE
    [actor_id] => THE ACTOR ID
    [actor_name] => 
    [name] => AD NAME
    [object_id] => 
    [object_story_id] => 
    [object_story_spec] => 
    [body] => AD BODY
    [image_hash] => IMAGE HASH
    [image_file] => 
    [image_url] => https://fbcdn-creative-a.akamaihd.net/REMOVED-PATH.png
    [image_crops] => 
    [video_id] => 
    [actor_image_hash] => 
    [link_url] => 
    [object_url] => http://www.example.com/
    [url_tags] => 
    [preview_url] => 
    [follow_redirect] => 
    [object_store_url] => 
    [link_deep_link_url] => 
    [call_to_action_type] => 
    [object_type] => DOMAIN
)

My question is, is this the expected behaviour? Or should I be able to do $adCreative->read() without specifying a field list?

@paulbain
Copy link
Contributor

paulbain commented Dec 3, 2014

This is expected behaviour. You should only request the fields from the API that you need as it decreases load and increases performance.

You also cannot read all possible fields of an object. Some fields can only be used when writing.

You can set the default fields to read on an object once which you may want to do if you always read the same fields.

Sent from my iPhone

On Dec 3, 2014, at 2:23 AM, cornernote notifications@github.com wrote:

I try to read an AdCreative like this:

Api::init($app_id, $app_secret, $access_token);
$adCreative = new AdCreative($ad_creative_id, $account_id);
$adCreative->read();
print_r($adCreative->getData());
But it results in:

Array
(
[id] => THE ID
[title] =>
[actor_id] =>
[actor_name] =>
[name] =>
[object_id] =>
[object_story_id] =>
[object_story_spec] =>
[body] =>
[image_hash] =>
[image_file] =>
[image_url] =>
[image_crops] =>
[video_id] =>
[actor_image_hash] =>
[link_url] =>
[object_url] =>
[url_tags] =>
[preview_url] =>
[follow_redirect] =>
[object_store_url] =>
[link_deep_link_url] =>
[call_to_action_type] =>
[object_type] =>
)
I then try using $adCreative->getFields() as an argument to $adCreative->read():

Api::init($app_id, $app_secret, $access_token);
$adCreative = new AdCreative($ad_creative_id, $account_id);
$adCreative->read($adCreative->getFields());
print_r($adCreative->getData());
Which throws an exception:

Array
(
[message] => (#100) Tried accessing nonexisting field (image_file) on node type (AdCreative)
[type] => OAuthException
[code] => 100
)
I then remove image_file from the list of fields:

Api::init($app_id, $app_secret, $access_token);
$adCreative = new AdCreative($ad_creative_id, $account_id);
$fields = $adCreative->getFields();
foreach ($fields as $k => $v) {
if ($v = 'image_file') {
unset($fields[$k]);
}
}
$adCreative->read($fields);
print_r($adCreative->getData());
Which results again in no data:

Array
(
[id] => THE ID
[title] =>
[actor_id] =>
[actor_name] =>
[name] =>
[object_id] =>
[object_story_id] =>
[object_story_spec] =>
[body] =>
[image_hash] =>
[image_file] =>
[image_url] =>
[image_crops] =>
[video_id] =>
[actor_image_hash] =>
[link_url] =>
[object_url] =>
[url_tags] =>
[preview_url] =>
[follow_redirect] =>
[object_store_url] =>
[link_deep_link_url] =>
[call_to_action_type] =>
[object_type] =>
)
I try making my own fields array, and then remove any that cause an exception, and end up with this list:

Api::init($app_id, $app_secret, $access_token);
$adCreative = new AdCreative($ad_creative_id, $account_id);
$fields = array(
AdCreativeFields::ID,
AdCreativeFields::TITLE,
AdCreativeFields::ACTOR_ID,
AdCreativeFields::ACTOR_NAME,
AdCreativeFields::NAME,
AdCreativeFields::OBJECT_ID,
AdCreativeFields::OBJECT_STORY_ID,
AdCreativeFields::OBJECT_STORY_SPEC,
AdCreativeFields::BODY,
AdCreativeFields::IMAGE_HASH,
//AdCreativeFields::IMAGE_FILE,
AdCreativeFields::IMAGE_URL,
AdCreativeFields::IMAGE_CROPS,
AdCreativeFields::VIDEO_ID,
AdCreativeFields::ACTOR_IMAGE_HASH,
AdCreativeFields::LINK_URL,
AdCreativeFields::OBJECT_URL,
AdCreativeFields::URL_TAGS,
//AdCreativeFields::PREVIEW_URL,
//AdCreativeFields::FOLLOW_REDIRECT,
AdCreativeFields::OBJECT_STORE_URL,
AdCreativeFields::LINK_DEEP_LINK_URL,
AdCreativeFields::CALL_TO_ACTION_TYPE,
AdCreativeFields::OBJECT_TYPE,
);
$adCreative->read($fields);
print_r($adCreative->getData());
Now I get what is expected (i have replaced the actual data incase of any security issue sharing it):

Array
(
[id] => THE ID
[title] => AD TITLE
[actor_id] => THE ACTOR ID
[actor_name] =>
[name] => AD NAME
[object_id] =>
[object_story_id] =>
[object_story_spec] =>
[body] => AD BODY
[image_hash] => IMAGE HASH
[image_file] =>
[image_url] => https://fbcdn-creative-a.akamaihd.net/REMOVED-PATH.png
[image_crops] =>
[video_id] =>
[actor_image_hash] =>
[link_url] =>
[object_url] => http://www.example.com/
[url_tags] =>
[preview_url] =>
[follow_redirect] =>
[object_store_url] =>
[link_deep_link_url] =>
[call_to_action_type] =>
[object_type] => DOMAIN
)
My question is, is this the expected behaviour? Or should I be able to do $adCreative->read() without specifying a field list?


Reply to this email directly or view it on GitHub.

@cornernote
Copy link
Author

Ok, makes sense. Thanks for clarifying.

@bnamnguyen
Copy link

hi cornernote,
where can I get $access_token to put into Api::init($app_id, $app_secret, $access_token); ?
thank you very much

@cornernote
Copy link
Author

hey @bibibaonam,

It's explained here:
https://developers.facebook.com/docs/reference/ads-api/overview#access_token

@cornernote
Copy link
Author

@paulbain
Copy link
Contributor

Please don't share access tokens publicly. That's a quick way to get your account into a lot of trouble.
You should consider changing your FB password to invalidate the token you just shared, or uninstalling the app from your FB settings.

@paulbain
Copy link
Contributor

To answer your question, prefix the account ID with act_ like in all the examples:

$account = new AdAccount('act_<YOUR ACCOUNT ID>);
$fields_you_want_to_read = array('name');
$adaccount = $account->read($fields_you_want_to_read);
echo $adaccount->name;

@bnamnguyen
Copy link

Thank you very much Paulbain.
I removed app from my account https://www.facebook.com/settings?tab=applications
Your reply worked, tks again.

@victorarthur
Copy link

Hi @paulbain
I have the same issue, I would like to clone the AdCreative (to rename the url_tags and this parameter doesn't available on the /copies node) so I have do download the object and recreate with a different url_tags parameter. In advance, I don't know what fields does the AdObject has so I can not pre-define the fields I would request.

Do you know any solution for this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants