Posts tagged with php

I have been playing with this now for a week and can't seems to find a solution. Whatsapp Business API is set up and working 100% in Live. I have verified my Webhook but are not able to see any data coming into the webhook file since the verification. Many variations of code have been tried to get data but still I don't see any data on the file, even when testing from the Quickstart > Configuration page.

The code I am currently running is from an answer provided by @Tarik at the following post: (for facebook itself but coding is the same) Facebook Messenger API - Can't verify webhook URL (PHP)

My webhook.php file is:

$message = date('Y-m-d H:i:s'); foreach ($_REQUEST as $key => $value){     $message .= "$key => $value (".$_SERVER['REQUEST_METHOD'].")\n"; } $input = file_get_contents("php://input"); $array = print_r(json_decode($input, true), true); file_put_contents('log.txt', $message.$array."\nREQUEST_METHOD: $_SERVER[REQUEST_METHOD]\n----- Request Date: ".date("d.m.Y H:i:s")." IP: $_SERVER[REMOTE_ADDR] -----\n\n", FILE_APPEND); if ( isset($_REQUEST['hub_challenge']) ){     echo $_REQUEST['hub_challenge']; } 

When sending a test from the Webhook Configuration page, the log.txt file only returns this:

2024-08-08 13:17:38 REQUEST_METHOD: GET ----- Request Date: 08.08.2024 13:17:38 IP: 173.252.107.116 ----- 

I am a bit stuck here and also lost on ideas. Can anyone advise me how to actually read the data I should receive on the webhook? I was considering playing with the verification token to see if Meta require it for some sort of asynchronous communication but that don't seem to be the case. Any assistance will be appreciated.

I'm encountering an issue with the Facebook Graph API when filtering leads based on the time_created field using UNIX timestamps. It seems that the API's time filtering is not precise down to the second.

For example, if I filter leads created at 06-07-2024 11:58:00 AM (actually in UNIX timestamp) and a lead has a time_created value of 06-07-2024 11:58:12 AM, the lead is still included in the response, resulting in duplicates in my data.

Has anyone else experienced this issue or found a way to ensure the filtering is precise to the second?

My Code:

$currentDateTime = Carbon::now(); //At Very Start of Code. $lastestDate = Carbon::parse(LeadData::latest()->first()->last_processed_time)->getTimestamp(); . . . . $newUrl2 = "https://graph.facebook.com/v20.0/{$adId}/leads"; $paramsNew2 = [     //'since' => $lastestDate,     'filtering' => json_encode([         [             'field' => 'time_created',             'operator' => 'GREATER_THAN', //_OR_EQUAL             'value' => $latestDate, // 1720247400, getting it from DB as last_processed_time.         ]     ]),     'fields' => "id,form_id,platform,partner_name,field_data,ad_id,adset_id,campaign_id,ad_name,adset_name,campaign_name,created_time,post",     'access_token' => $accessToken, ]; $response2 = Http::get($newUrl2, $paramsNew2); $data3 = $response2->json(); $leadDatas = $data3['data'] ?? null; foreach ($leadDatas as $leadData)  {     try      {         LeadData::create([                 'account_id' => $adAccountId ?? null,                 'account_name' => null, ///$accountName,                 'campaign_id' => $leadData['campaign_id'],                 'campaign_name' => $leadData['campaign_name'],                 'adset_id' => $leadData['adset_id'],                 'adset_name' => $leadData['adset_name'],                 'ad_id' => $leadData['ad_id'],                 'ad_name' => $leadData['ad_name'],                 'ad_status' => $adStatus,                 'lead_id' => $leadData['id'],                 'lead_created_time' => $leadData['created_time'],                 'platform' => $leadData['platform'],                 'form_id' => $leadData['form_id'],                 'field_data' => json_encode($leadData['field_data'], JSON_UNESCAPED_UNICODE),                 'last_processed_time' => $currentDateTime,             ]);     } catch (\Exception $e) {         echo "Data entry in database failed: " . $e->getMessage() ."\n";         Log::error($e->getMessage());     } } 

Then I'm saving the leads in the DB.

I'm trying to get all ads from a Facebook ad account that have metrics with 'spend' set to 0 and 'reach' set to 1 using GRAPH API SDK v.19. However, if 'reach' does not exist, this filter skips ads that have 'spend' at 0. On the other hand, if in my filter I put 'reach' at 0 and 'spend' at 0, this brings me all the ads of the ad account (+5000) even if they do not have metrics on the specified date. How can I fix this problem and get only the ads that have metrics with 'spend' greater than or equal to 0 excluding those without.

I tried using Facebook's Graph Explorer to get all the ads in an ad account that have metrics. I used the 'filtering' field in my query to filter the results based on these criteria of spend greater than or equal to 0 and reach greater than or equal to 1. However, if 'reach' does not exist, this filter ignores ads that have 'spend' at 0. On the other hand, if in my filter I put 'reach' at 0 and 'spend' at 0, this brings me all the ads in the ad account even if they do not have metrics on the specified date. I was hoping to get only the ads that have metrics.

I share graph explorer URL: act_384943998894065/ads?fields=id,name, effective_status,created_time,insights.time_range({'since':'2024-06-19','until':'2024-06-19'}){ad_id,reach,spend,objective,actions,cost_per_action_type}&filtering=[{"field":"spend","operator":"GREATER_THAN_OR_EQUAL","value":0},{"field":"reach","operator":"GREATER_THAN_OR_EQUAL","value":0}]&time_range={'since':'2024-06-19','until':'2024-06-19'}

attached image of graph explorer results:

{       "id": "120208793679360133",       "name": "CE1705202404",       "effective_status": "ACTIVE",       "created_time": "2024-05-17T14:59:20-0500"     },     {       "id": "120208458805660133",       "name": "CE202404301910",       "effective_status": "ACTIVE",       "created_time": "2024-05-09T12:23:30-0500",       "insights": {         "data": [           {             "ad_id": "120208458805660133",             "spend": "0",             "objective": "OUTCOME_ENGAGEMENT",             "actions": [               {                 "action_type": "onsite_conversion.messaging_welcome_message_view",                 "value": "1"               }             ],             "cost_per_action_type": [               {                 "action_type": "onsite_conversion.messaging_welcome_message_view",                 "value": "0"               }             ],             "date_start": "2024-06-19",             "date_stop": "2024-06-19"           }         ],         "paging": {           "cursors": {             "before": "MAZDZD",             "after": "MAZDZD"           }         }       }     }, 

I attach my PHP code:

public function getAdsByAccountId($date){         try {             $date_range = "{'since':'$date','until':'$date'}";             $time_range = "&time_range=$date_range";             $fields = [                 "id", "campaign_id", "adset_id", "adset{promoted_object}", "creative{id,image_url,video_id,object_story_spec}", "name", "created_time", "status", "effective_status", "configured_status",                 "campaign{id,account_id, name, objective, effective_status, status, configured_status, created_time}",                 "insights.time_range($date_range){ad_id,reach,spend,objective,actions,cost_per_action_type}",             ];             $string_fields = $this->buildFields($fields);             $filtering = "&filtering=[{'field':'spend','operator':'GREATER_THAN_OR_EQUAL','value':0},{'field':'reach','operator':'GREATER_THAN_OR_EQUAL','value':1}]";             $limit = "&limit=200";             $url = "/" . $this->BUSINESS_ACCOUNT_ID . "/ads" . $string_fields . $filtering . $time_range . $limit;             $response = $this->FB_SDK->get($url, $this->ACCESS_TOKEN);             $response = json_decode($response->getBody());             $ads = $response->data;             $data = $this->getAdsFromNextPages($response, $ads);             $ads = $data->ads;             $response = $data->response;             return (object) [                 "ads" => $ads,                 "response" => $response,             ];         } catch (\Facebook\Exceptions\FacebookResponseException $e) {             Log::error('FacebookAdsService::getAdsByAccountId Graph returned an error: ' . $e->getMessage());             throw $e;         } catch (\Facebook\Exceptions\FacebookSDKException $e) {             Log::error('FacebookAdsService::getAdsByAccountId Facebook SDK returned an error: ' . $e->getMessage());             throw $e;         }     } 

I have and Facebook developer account created around 4 years before. The problem is when i try to fetch page videos, its not returning any result.

Note: i am using laravel-socialite package and the app is not verified yet.

When tried to debug i found out that the auth toke does not contain "pages_read_user_content" this permission.

On my laravel code i have added this permission but still when i am doing authentication its not showing this permission.

public function login(){     return Socialite::driver('facebook')         ->stateless()         ->setScopes(['pages_read_engagement', 'pages_show_list', 'pages_read_user_content'])         ->usingGraphVersion('v20.0')         ->redirect(); } 

Approach i tried :

To test this i created a new facebook developers account and did all the process and boom it worked. I am attaching the screenshot of permission detail with new developer account.

So the issue is its not working with my old account. Can anyone suggest how to fix this.

Thanks.