I am trying to get all the custom audiences from my account - and I can't get more than 25 audiences, only the top 25.
I am using the 'getCustomAudiences()' function.
Any ideas?
$field = array(
CustomAudienceFields::NAME,
CustomAudienceFields::ID);
$account = new AdAccount($account_id);
$audiences = $account->getCustomAudiences($field);
foreach ($audiences as $value){
echo "ID:". $value->id. " Name:".$value->name."<BR>" ;
}
Related
I have my script to monitor some Facebook pages. Since Facebook API banned page public access permission on 4-SEP-2019. I need to parse the content by xpath method.
Each Facebook post is wrap by div[contains(#class,"userContentWrapper")]. I would like to loop posts one by one to find a desired data.
I don't known why $message = $post->findvalue('//div[#data-testid="post_message"]//p'); show all text in <p> of every posts.
use LWP::UserAgent;
$ua = new LWP::UserAgent;
$request = new HTTP::Request;
$request->url('https://www.facebook.com/pg/FIFA/posts/');
$request->method('GET');
$request->header('User-Agent' => 'Mozilla/5.0 Chrome/71.0.3578.98 Safari/537.36');
$response = $ua->request($request);
open(HTM, ">zzz.htm");
print HTM $response->content;
close(HTM);
use HTML::TreeBuilder::XPath;
$tree = HTML::TreeBuilder::XPath->new_from_content($response->content);
$posts = $tree->findnodes('//div[contains(#class,"userContentWrapper")]');
for my $post (#{$posts})
{
$id = $post->findnodes('//div[#data-testid="story-subtitle"]/#id');
$id = $id->[0]->getValue;
print "id = $id\n\n";
$object_id = $post->findnodes('//div[#data-testid="story-subtitle"]//a/#href');
$object_id = 'https://www.facebook.com' . $object_id->[0]->getValue;
print "object_id = $object_id\n\n";
$message = $post->findvalue('//div[#data-testid="post_message"]//p');
# $message = $message->[0]->getValue;
print "$message\n\n";
$ajaxify = $post->findnodes('//div[#class="mtm"]//a/#ajaxify');
$ajaxify = $ajaxify->[0]->getValue;
print "ajaxify = $ajaxify\n\n";
$ploi = $post->findnodes('//div[#class="mtm"]//a/#data-ploi');
$ploi = $ploi->[0]->getValue;
print "ploi = $ploi\n\n";
# $plsi = $post->findnodes('//div[#class="mtm"]//a/#data-plsi');
# $plsi = $plsi->[0]->getValue;
# print "plsi = $plsi\n\n";
$href = $post->findnodes('//div[#class="mtm"]//a/#href');
$href = 'https://www.facebook.com' . $href->[0]->getValue;
print "href = $href\n\n";
print "---------------------------------------------------------\n\n";
}
The post is unclear and it seems to contain multiple questions. This needs to be fixed, but in the mean time, I'll address the following:
I would like to loop posts one by one to find a desired data.
From HTML::TreeBuilder::XPath,
findnodes ($path)
Returns a list of nodes found by $path. In scalar context returns an Tree::XPathEngine::NodeSet object.
From Tree::XPathEngine::NodeSet,
get_nodelist()
Returns a list of nodes. See Tree::XPathEngine::XMLParser for the format of the nodes.
So,
my #posts = $tree->findnodes('...');
for my $post (#posts) { ... }
or
my $posts = $tree->findnodes('...');
for my $post ($posts->get_nodelist()) { ... }
Any other questions should be posted as separate Questions.
I have problems retrieving Lead Ads.
I have the Ad-ID and the Page-ID. I haven't created them, but was added as a developer.
I was trying to use the PHP SDK and this https://developers.facebook.com/docs/marketing-api/guides/lead-ads/v2.9
Nothing is working. I cannot find a nice tutorial about that.
I just want to retrieve the leading Ads!
Anyone?
Assuming you have already installed FB API SDK and configured your FB app, you can use this to get all results from all LeadAds of your $page_id
use FacebookAds\Api;
use FacebookAds\Object\Page;
use FacebookAds\Object\Ad;
$access_token = 'YOUR TOKEN';
$app_id = 'YOUR APP ID';
$app_secret = 'YOUR APP SECRET';
$page_id = 'YOUR PAGE ID';
Api::init($app_id, $app_secret, $access_token);
$ads = getAllLeadsAds($page_id);
$result = array();
foreach ($ads->data as $item) {
$leads = getLeadAdInfo($item->id);
$i = 0;
foreach ($leads->data as $value) {
$result[$i]['ad_id'] = $item->id;
$result[$i]['lead_id'] = $value->id;
$result[$i]['form'] = $value->field_data;
$i++;
}
}
print_r($result);
function getAllLeadsAds($page)
{
$page = new Page($page);
return $page->getLeadgenForms()->getResponse()->getBody();
}
function getLeadAdInfo($ad)
{
$ad = new Ad($ad);
return $ad->getLeads()->getResponse()->getBody();
}
Its possible that you not calling all ads inside adset.
You can use facebook cursor.
$cursor->fetchAfter();
use FacebookAds\Object\AdAccount;
use FacebookAds\Object\Values\ArchivableCrudObjectEffectiveStatuses;
use FacebookAds\Object\Fields\CampaignFields;
use FacebookAds\Object\AdCampaign;
use FacebookAds\Object\Fields\AdSetFields;
use FacebookAds\Object\AdSet;
use FacebookAds\Object\Fields\AdFields;
use FacebookAds\Object\Ad;
use FacebookAds\Object\Campaign;
function getadsetname($adset_id,$acc_id){
$adset = new AdSet($adset_id, $acc_id);
$adset->read(array(
AdSetFields::NAME,
));
return $adset->name; // Outputs name of adset.
}
function get_campaignname($campaign_id){
$campaign = new Campaign($campaign_id);
$campaign->read(array(
CampaignFields::NAME,
));
return $campaign->name;
}
$account = new AdAccount($account_id);
echo $adset_name = getadsetname($adset_id,$account_id);
echo $campaign_name = get_campaignname($campaign_id);
echo "<hr>";
$adcampaign = new AdAccount($campaign_id);
$adset = new AdSet($adset_id);
$ads = $adset->getAds(array(
AdFields::NAME,
AdFields::CONFIGURED_STATUS,
AdFields::EFFECTIVE_STATUS,
AdFields::CREATIVE,
));
$ads->fetchAfter();
foreach ($ads as $ad) {
$ad_id = $ad->id;
$ad_name = $ad->name;
if($ad->configured_status=="ACTIVE"){
$ad1 = new Ad($ad_id);
$leads = $ad1->getLeads();
$leads->fetchAfter();
foreach ($leads as $lead) {
$fname = $lead->field_data;
//var_dump($fname);
$data = array();
$data['lead_id'] = $lead->id;
$data['created_time'] = $lead->created_time;
$data['ad_id'] = $ad_id;
$data['ad_name'] = $ad_name;
$data['adset_id'] = $adset_id;
$data['adset_name'] = $adset_name;//$adset_name;
$data['campaign_id'] = $campaign_id;
$data['campaign_name'] = $campaign_name;
$data['form_id'] = $lead->form_id;//$lead->id;
$data['is_organic'] = $lead->is_organic;//$lead->id;
}
}
}
$ads->fetchAfter(); will get the next list of ads and
$leads->fetchAfter(); will get the next list of leads
I am working on a facebook application, and some of the data that I want are the groups of that user and the other members of that group as shown in this code:
$groups = $facebook->api('/me/groups?since=0');
mysqlc();
foreach($groups['data'] as $group){
$gid=GetSQLValueString($group['id'], "text");
$name=GetSQLValueString($group['name'], "text");
//echo $gid." -> ".$name;
$query = sprintf("SELECT * FROM `group` WHERE gid = %s", $gid);
$res = mysql_query($query) or die('Query failed: ' . mysql_error() . "<br />\n$sql");
if(mysql_num_rows($res) == 0){
$iquery = sprintf("INSERT INTO `group`(gid, name) values(%s,%s)", $gid, $name);
$ires = mysql_query($iquery) or die('Query failed: ' . mysql_error() . "huhuhu<br />\n$sql");
}
$c = explode("'", $gid);
$params = "/".$c[1]."/members";
echo "'".$params."'";
$members = $facebook->api("'".$params."'");
print_r($members);
}
what I wanted to have after this code are the members of each group that the user has, but this code is not working properly.. So my main query is what is the proper way or replacement to this line:
$members = $facebook->api("'".$params."'");
where I can use the fetched group Id. thanks a lot to those who will answer my query.
I am using AWD Facebook wordpress plugin to allow my visitors to login with their Facebook account information. When a visitor registers on my site I automatically create a new post that is titled with their username and includes their Facebook profile picture as the content. The code for that is below:
function my_create_page($user_id){
$fbuide = 0;
$the_user = get_userdata($user_id);
$new_user_name = $the_user->user_login;
$new_user_avatar = get_avatar($the_user->user_email);
global $AWD_facebook;
$fbuide = $AWD_facebook->uid;
$headers = get_headers('http://graph.facebook.com/' . $fbuide . '/picture?type=large',1);
if(isset($headers['Location'])) {
$url = $headers['Location']; // string
} else {
$url = false;
}
$my_avatar = "<img src='" . $url . "' class='avatar AWD_fbavatar' alt='" . $alt . "' height='" . $size . "' />";
$my_post = array();
$my_post['post_title'] = $new_user_name;
$my_post['post_type'] = 'post';
$my_post['post_content'] = $my_avatar;
$my_post['post_status'] = 'publish';
wp_insert_post( $my_post );
}
add_action('user_register', 'my_create_page');
What I am looking to accomplish is a bit different though. I also want to include a brief biography about the user (currently the post is simply their picture). So when a visitor logs in with AWD Facebook, their needs to be an additional field that allows the user to type in their bio. Then I would be able to grab that info from their user profile and include it in the post. Any ideas on how I can accomplish this? Is there a different way to do this?
I would recommend storing their Facebook picture as metadata and use the content area as their bio for the automatically generated post. So something like this should get you started:
$my_post = array(
'post_title'=>$new_user_name,
'post_type'=>'post',
'post_content'=>'',
'post_status'=>'publish'
);
if( $id = wp_insert_post( $my_post ) ){
update_post_meta($id, 'avatar', $url);
}
Then you can generate the loop like so:
if ( have_posts() ) : while ( have_posts() ) : the_post();
//... stuff here
$avatar = get_post_meta($post->ID, 'avatar', 'true');
the_content();
echo '<img class="avatar AWD_fbavatar" src="'.$avatar.'" alt="'.$alt.'" height="'.$size.'" />';
endwhile;endif;
I want to fetch 'birthdays' of users, and their friends on my website, from their facebook profiles (with their facebook credentials supplied).
Is their a feature in Facebook API/Connect that I can use to fetch these details from facebook as possible on Native Facebook Apps using Facebook API.
I want to store this data in my DB, and users will be asked for their facebook credentials and consent before this is done.
Read the api documentation things like this are easily done. You can do it like this:
$facebook = new Facebook( $apikey, $secret );
$uid = $facebook->require_login();
$friends = $facebook->api_client->friends_get(); // $friends is an array holding the user ids of your friends
foreach( $friends as $f ) {
$data = $facebook->api_client->fql_query( "SELECT birthday_date FROM user WHERE uid=$f" );
// $data[0] is an array with 'birthday_date' => "02/29/1904"
// see api documentation for other fields and do a print_r
}
So recently I wanted to check my friends to see if any of them had their birthday for the current day. Using FQL this is super easy and I encourage you to explore FQL more because it will yield a more efficient solution than, say, what Pierre kindly offered. Here is a small snippet of the code:
$friends = $facebook->api_client->friends_get();
$uids = "";
foreach($friends as $f) {
$uids .= "uid=$f OR ";
}
$query_uids = substr($uids,0,strlen($query_uids)-4);
date_default_timezone_set('UTC');
$current_date = date("m/d");
echo "<br />Searching for birthdays for the given month/day: $current_date<br />";
$data = $facebook->api_client->fql_query( "SELECT name, uid FROM user WHERE ( ($query_uids) AND strpos(birthday_date,'$current_date') >= 0 )" );
if(count($data) > 0) {
foreach($data as $d) {
print_r($d);
}
} else {
echo "<br />No Birthdays Today<br />";
}
require_once('facebook-platform/client/facebook.php');
$facebook = new Facebook(API_KEY, SECRET);
$facebook->require_login();
function getInfo($user_list, $fields)
{
try
{
$u = $facebook->api_client->users_getInfo($user_list, $fields);
return $u;
}
catch (FacebookRestClientException $e)
{
echo $e->getCode() . ' ' . $e->getMessage();
}
}
function getFriendsBirthdays($user_id)
{
$f = $_REQUEST['fb_sig_friends'];
$f = explode(',', $f);
$birthdays = array();
foreach($f as $friend_id)
{
$birthdays[] = getInfo($friend_id, 'birthday');
}
return $birthdays;
}
Do something like that or use the Batch API to do multiple calls at once. Check the Facebook API.
You could fetch it via the API, but Facebook's terms strictly forbid you from storing anything other than their user ID in your database - see the developer wiki for details. You will need to query the API each time.