Skip to content

Commit ce03e92

Browse files
committed
* Updating to use the latest version of the PHP SDK, see README.md
* Improving custom audience handling, see examples/custom_auciences.php
1 parent 2e9f939 commit ce03e92

File tree

13 files changed

+233
-34
lines changed

13 files changed

+233
-34
lines changed

DEVELOPER_GUIDE.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ To instantiate an Api object you will need a valid access token:
3838
use FacebookAds\Api;
3939
use Facebook\FacebookSession;
4040

41+
// Set the default application information to be used with this session
42+
FacebookSession::setDefaultApplication($app_id, $app_secret);
4143
$session = new FacebookSession($access_token);
4244
$api = new Api($session);
4345
```

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ To instantiate an Api object you will need a valid access token:
4040
use FacebookAds\Api;
4141
use Facebook\FacebookSession;
4242

43+
// Set the default application information to be used with this session
44+
FacebookSession::setDefaultApplication($app_id, $app_secret);
4345
$session = new FacebookSession($access_token);
4446
$api = new Api($session);
4547
```

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
],
1111
"homepage": "https://developers.facebook.com/",
1212
"require" : {
13-
"facebook/php-sdk-v4" : "4.0.7",
13+
"facebook/php-sdk-v4" : "4.0.8",
1414
"psr/log": "1.*"
1515
},
1616
"require-dev" : {

examples/adgroup_creation.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@
2424

2525
// Set your access token here:
2626
$access_token = null;
27+
$app_id = null;
28+
$app_secret = null;
2729

28-
if(is_null($access_token)) {
29-
throw new \Exception('You must set your access token before executing');
30+
if(is_null($access_token) || is_null($app_id) || is_null($app_secret)) {
31+
throw new \Exception(
32+
'You must set your access token, app id and app secret before executing'
33+
);
3034
}
3135

3236
define('SDK_DIR', __DIR__ . '/..'); // Path to the SDK directory
@@ -35,6 +39,7 @@
3539
use FacebookAds\Api;
3640
use Facebook\FacebookSession;
3741

42+
FacebookSession::setDefaultApplication($app_id, $app_secret);
3843
$session = new FacebookSession($access_token);
3944
$api = new Api($session);
4045

examples/connectionobjects.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,19 @@
3131

3232
// Set your access token here:
3333
$access_token = null;
34+
$app_id = null;
35+
$app_secret = null;
3436

35-
if(is_null($access_token)) {
36-
throw new \Exception('You must set your access token before executing');
37+
if(is_null($access_token) || is_null($app_id) || is_null($app_secret)) {
38+
throw new \Exception(
39+
'You must set your access token, app id and app secret before executing'
40+
);
3741
}
3842

3943
define('SDK_DIR', __DIR__ . '/..'); // Path to the SDK directory
4044
$loader = include SDK_DIR.'/vendor/autoload.php';
4145

46+
FacebookSession::setDefaultApplication($app_id, $app_secret);
4247
$session = new FacebookSession($access_token);
4348
$api = new Api($session);
4449

examples/custom_audiences.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
/**
3+
* Copyright 2014 Facebook, Inc.
4+
*
5+
* You are hereby granted a non-exclusive, worldwide, royalty-free license to
6+
* use, copy, modify, and distribute this software in source code or binary
7+
* form for use in connection with the web services and APIs provided by
8+
* Facebook.
9+
*
10+
* As with any software that integrates with the Facebook platform, your use
11+
* of this software is subject to the Facebook Developer Principles and
12+
* Policies [http://developers.facebook.com/policy/]. This copyright notice
13+
* shall be included in all copies or substantial portions of the software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21+
* DEALINGS IN THE SOFTWARE.
22+
*
23+
*/
24+
25+
// Set your access token here:
26+
$access_token = null;
27+
$app_id = null;
28+
$app_secret = null;
29+
30+
// Set your ad account here: act_XX
31+
$account_id = null;
32+
33+
if (is_null($access_token) || is_null($app_id) || is_null($app_secret)) {
34+
throw new \Exception(
35+
'You must set your access token, app id and app secret before executing'
36+
);
37+
}
38+
39+
if (is_null($account_id)) {
40+
throw new \Exception(
41+
'You must set your account id before executing');
42+
}
43+
44+
define('SDK_DIR', __DIR__ . '/..'); // Path to the SDK directory
45+
$loader = include SDK_DIR.'/vendor/autoload.php';
46+
47+
use FacebookAds\Api;
48+
use Facebook\FacebookSession;
49+
50+
FacebookSession::setDefaultApplication($app_id, $app_secret);
51+
$session = new FacebookSession($access_token);
52+
$api = new Api($session);
53+
54+
55+
// use the namespace for Custom Audiences and Fields
56+
use FacebookAds\Object\CustomAudience;
57+
use FacebookAds\Object\Fields\CustomAudienceFields;
58+
use FacebookAds\Object\Values\CustomAudienceTypes;
59+
60+
// Create a custom audience object, setting the parent to be the account id
61+
$audience = new CustomAudience(null, $account_id);
62+
$audience->setData(array(
63+
CustomAudienceFields::NAME => 'My Custom Audiece',
64+
CustomAudienceFields::DESCRIPTION => 'Lots of people',
65+
));
66+
// Create the audience
67+
$audience->create();
68+
echo "Audience ID: " . $audience->id."\n";
69+
70+
// Assuming you have an array of emails:
71+
$emails = array(
72+
73+
74+
75+
76+
);
77+
78+
$audience->addUsers($emails, CustomAudienceTypes::EMAIL);
79+
$audience->read(array(CustomAudienceFields::APPROXIMATE_COUNT));
80+
echo "Estimated Size:"
81+
. $audience->{CustomAudienceFields::APPROXIMATE_COUNT}."\n";

src/FacebookAds/Api.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use \Facebook\FacebookSession;
2828
use \Facebook\FacebookRequest;
2929
use \Facebook\FacebookResponse;
30+
use \Facebook\FacebookSDKException;
3031
use \Psr\Log\LoggerInterface;
3132
use \Psr\Log\NullLogger;
3233

@@ -86,6 +87,16 @@ public function __construct(
8687
if (static::instance() === null) {
8788
static::setInstance($this);
8889
}
90+
try {
91+
FacebookSession::_getTargetAppSecret();
92+
} catch(FacebookSDKException $f) {
93+
// Disable sending app secret proof and warn
94+
FacebookSession::enableAppSecretProof(false);
95+
trigger_error(
96+
'You should set a default app id and secret, see the README.md file '.
97+
'for more information.',
98+
E_USER_DEPRECATED);
99+
}
89100
}
90101

91102
/**

src/FacebookAds/HttpClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
namespace FacebookAds;
2626

2727
use Facebook\FacebookCurl;
28-
use Facebook\FacebookCurlHttpClient;
28+
use Facebook\HttpClients\FacebookCurlHttpClient;
2929

3030
class HttpClient extends FacebookCurlHttpClient {
3131

src/FacebookAds/Object/CustomAudience.php

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
use FacebookAds\Api;
2828
use FacebookAds\Object\Fields\CustomAudienceFields;
29+
use FacebookAds\Object\Values\CustomAudienceTypes;
2930

3031
class CustomAudience extends AbstractCrudObject {
3132

@@ -84,28 +85,77 @@ protected function getEndpoint() {
8485
* Add users to the AdCustomAudiences. There is no max on the total number of
8586
* users that can be added to an audience, but up to 10000 users can be added
8687
* at a given time.
88+
* Hash type should only be used on email and phone
8789
*
88-
* @param array $users Array of user info
90+
* @param array $users
91+
* @param string $type
92+
* @param string $hash_type the algorithm to use when hasing
8993
* @return boolean Returns true on success
9094
*/
91-
public function addUsers($users) {
95+
public function addUsers(array $users, $type, $hash_type = null) {
96+
$params = $this->formatParams($users, $type, $hash_type);
9297
return $this->getApi()->call(
9398
'/'.$this->assureId().'/users',
9499
Api::HTTP_METHOD_POST,
95-
array('users' => $users))->getResponse();
100+
$params)->getResponse();
96101
}
97102

98103
/**
99104
* Delete users from AdCustomAudiences
105+
* Hash type should only be used on email and phone
100106
*
101-
* @param array $users Array of user info
107+
* @param array $users
108+
* @param string $type
109+
* @param string $hash_type the algorithm to use when hasing
102110
* @return boolean Returns true on success
103111
*/
104-
public function removeUsers($users) {
112+
public function removeUsers(array $users, $type, $hash_type = null) {
113+
$params = $this->formatParams($users, $type, $hash_type);
105114
return $this->getApi()->call(
106115
'/'.$this->assureId().'/users',
107116
Api::HTTP_METHOD_DELETE,
108-
array('users' => $users))->getResponse();
117+
$params)->getResponse();
118+
}
119+
120+
/**
121+
* Remove list of users decided to opt-out from all custom audiences
122+
* Hash type should only be used on email and phone
123+
*
124+
* @param array $users
125+
* @param string $type
126+
* @param string $hash_type the algorithm to use when hasing
127+
* @return boolean Returns true on success
128+
*/
129+
public function optOutUsers(array $users, $type, $hash_type = null) {
130+
$params = $this->formatParams($users, $type, $hash_type);
131+
return $this->getApi()->call(
132+
'/'.$this->assureParentId().'/usersofanyaudience',
133+
Api::HTTP_METHOD_DELETE,
134+
$params)->getResponse();
135+
}
136+
137+
/**
138+
* Take users and format them correctly for the request
139+
*
140+
* @param array $users
141+
* @param string $type
142+
* @param string $hash_type the algorithm to use when hasing
143+
*/
144+
protected function formatParams(array $users, $type, $hash_type = null) {
145+
$request = array();
146+
if ($type == CustomAudienceTypes::EMAIL
147+
|| $type == CustomAudienceTypes::PHONE || $hash_type !== null) {
148+
$hash_type = is_null($hash_type) ?: self::HASH_TYPE_SHA256;
149+
$request['hash_type'] = $hash_type;
150+
}
151+
152+
foreach ($users as $u) {
153+
if (!is_null($hash_type)) {
154+
$u = hash(self::HASH_TYPE_SHA256, $u);
155+
}
156+
$request['users'][] = array($type => $u);
157+
}
158+
return $request;
109159
}
110160

111161
/**
@@ -134,16 +184,4 @@ public function removeSharedAccounts($act_ids) {
134184
array('adaccounts' => $act_ids))->getResponse();
135185
}
136186

137-
/**
138-
* Remove list of users decided to opt-out from all custom audiences
139-
*
140-
* @param array $users Array of user info
141-
* @return boolean Returns true on success
142-
*/
143-
public function optOutUsers($users) {
144-
return $this->getApi()->call(
145-
'/'.$this->assureParentId().'/usersofanyaudience',
146-
Api::HTTP_METHOD_DELETE,
147-
array('users' => $users))->getResponse();
148-
}
149187
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* Copyright 2014 Facebook, Inc.
4+
*
5+
* You are hereby granted a non-exclusive, worldwide, royalty-free license to
6+
* use, copy, modify, and distribute this software in source code or binary
7+
* form for use in connection with the web services and APIs provided by
8+
* Facebook.
9+
*
10+
* As with any software that integrates with the Facebook platform, your use
11+
* of this software is subject to the Facebook Developer Principles and
12+
* Policies [http://developers.facebook.com/policy/]. This copyright notice
13+
* shall be included in all copies or substantial portions of the software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18+
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21+
* DEALINGS IN THE SOFTWARE.
22+
*
23+
*/
24+
25+
namespace FacebookAds\Object\Values;
26+
27+
abstract class CustomAudienceTypes {
28+
29+
/**
30+
* @var string
31+
*/
32+
const ID = 'id';
33+
34+
/**
35+
* @var string
36+
*/
37+
const EMAIL = 'email_hash';
38+
39+
/**
40+
* @var string
41+
*/
42+
const PHONE = 'phone_hash';
43+
44+
/**
45+
* @var string
46+
*/
47+
const CUSTOM_AUDIENCE_THIRD_PARTY_ID = 'custom_audience_third_party_id';
48+
49+
/**
50+
* @var string
51+
*/
52+
const MOBILE_ADVERTISER_ID = 'mobile_advertiser_id';
53+
54+
}

0 commit comments

Comments
 (0)