I am trying to get the FB ads through the Graph API using the code below,
$fb = new \Facebook\Facebook([
'app_id' => 'app_id',
'app_secret' => 'app_secret',
'default_graph_version' => 'v19.0',
//'default_access_token' => '{access-token}', // optional
]);
// Use one of the helper classes to get a Facebook\Authentication\AccessToken instance.
// $helper = $fb->getRedirectLoginHelper();
// $helper = $fb->getJavaScriptHelper();
// $helper = $fb->getCanvasHelper();
// $helper = $fb->getPageTabHelper();
$params = array(
"access_token" => "accesstokenhere",
"ad_reached_countries" => "US",
"search_terms" => "bags"
);
try {
// Get the \Facebook\GraphNodes\GraphUser object for the current user.
// If you provided a 'default_access_token', the '{access-token}' is optional.
$response = $fb->get('/ads_archive',
'accesstokenhere',
array (
'ad_reached_countries' => 'US',
'search_terms' => 'bags',
),
);
} catch(\Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(\Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$me = $response->getGraphUser();
echo 'Logged in as ' . $me->getName();
dd($me);
but it gave me
The parameter ad_reached_countries is required
I tried concatenating the $params
variable at the end of the GET call but still it gives the same error message.
When I also placed ad_reached_countries
after the ads_archive
, it gave me Invalid parameter
error. How would I properly place the ad_reached_countries
and/or other paramters when searching for ads.
If passing the parameters as an array doesn't create the actually needed request URL, then specifying the full URL including the parameters in the first get
method argument, should work.
And since you are not requesting a user object any more, the $me = $response->getGraphUser();
needs to be replaced with something more appropriate as well. Using $graphEdge = $response->getGraphEdge();
, you should afterwards be able to loop over the result, and access the individual GraphNodes.