/**
  * @param string $retailer_id
  * @param int $catalog_id
  * @param array $data
  * @param array $params
  * @return bool
  */
 public static function updateProductInCatalogByRetailerId($retailer_id, $catalog_id, array $data = array(), array $params = array())
 {
     $product = new static();
     $product->setData($data);
     $data = Api::instance()->call(self::buildCatalogUrlForRetailerId($retailer_id, $catalog_id), RequestInterface::METHOD_POST, array_merge($product->exportData(), $params))->getContent();
     return isset($data['success']) ? $data['success'] : false;
 }
Example #2
0
 public function __construct(ResponseInterface $response, AbstractObject $object_prototype, Api $api = null)
 {
     $this->response = $response;
     $this->objectPrototype = $object_prototype;
     $this->api = $api !== null ? $api : Api::instance();
     $this->appendResponse($response);
 }
 /**
  * Register the application services.
  *
  * @return void
  */
 public function register()
 {
     $this->app->bind('FacebookAdsAPI', function () {
         $config = config('facebook');
         Api::init($config['app_id'], $config['app_secret'], $config['access_token']);
         return Api::instance();
     });
 }
 public function testInit()
 {
     $api = Api::init(static::VALUE_SESSION_APP_ID, static::VALUE_SESSION_APP_SECRET, static::VALUE_SESSION_ACCESS_TOKEN);
     $this->assertTrue($api instanceof Api);
     $this->assertTrue($api === Api::instance());
     $this->assertEquals(static::VALUE_SESSION_APP_ID, $api->getSession()->getAppId());
     $this->assertEquals(static::VALUE_SESSION_APP_SECRET, $api->getSession()->getAppSecret());
     $this->assertEquals(static::VALUE_SESSION_ACCESS_TOKEN, $api->getSession()->getAccessToken());
 }
 public function testInstance()
 {
     $this->assertTrue($this->getApi() === Api::instance());
     $newApi = new Api($this->getSession());
     Api::setInstance($newApi);
     $this->assertTrue($newApi === Api::instance());
     $this->assertFalse($this->getApi() === Api::instance());
     // Restore default for future tests
     Api::setInstance($this->getApi());
 }
 /**
  * @param string $query
  * @param string $type
  * @param string $class
  * @param array $params
  * @param Api $api
  * @return Cursor
  * @throws \InvalidArgumentException
  */
 public static function search($type, $class = null, $query = null, array $params = array(), Api $api = null)
 {
     $api = $api ?: Api::instance();
     if (!$api) {
         throw new \InvalidArgumentException('An Api instance must be provided as argument or ' . 'set as instance in the \\FacebookAds\\Api');
     }
     $params = array_merge($params, array('type' => $type, 'class' => $class, 'q' => $query));
     $response = $api->call('/search', RequestInterface::METHOD_GET, $params);
     return new Cursor($response, new TargetingSearch());
 }
 /**
  * @param string $query
  * @param string $type
  * @param string $class
  * @param array $params
  * @param Api $api
  * @return Cursor
  * @throws \InvalidArgumentException
  */
 public static function search($type, $class = null, $query = null, array $params = array(), Api $api = null)
 {
     $api = $api ?: Api::instance();
     if (!$api) {
         throw new \InvalidArgumentException('An Api instance must be provided as argument or ' . 'set as instance in the \\FacebookAds\\Api');
     }
     $params = array_merge($params, array('type' => $type, 'class' => $class, 'q' => $query));
     $response = $api->call('/search', Api::HTTP_METHOD_GET, $params);
     $result = array();
     foreach ($response->getResponse()->{'data'} as $data) {
         /** @var $object AbstractObject */
         $object = new static();
         if (is_object($data)) {
             $data = get_object_vars($data);
         }
         $object->setData($data);
         $result[] = $object;
     }
     return new Cursor($result, $response);
 }
use FacebookAds\Object\AdAccount;
use FacebookAds\Logger\CurlLogger;
use FacebookAdsTest\Bootstrap;
$relative_path = getcwd();
chdir(__DIR__);
if (!ini_get('date.timezone')) {
    ini_set('date.timezone', 'UTC');
}
$loader = (require __DIR__ . '/../autoload.php');
require_once __DIR__ . '/../../test/FacebookAdsTest/Bootstrap.php';
// Setup the same env as our tests
Bootstrap::initAutoloader();
$config = Bootstrap::initIntegrationConfig();
Api::init($config->appId, $config->appSecret, $config->accessToken);
if (in_array('--dump', $_SERVER['argv'])) {
    Api::instance()->setLogger(new CurlLogger(STDERR));
}
$get_class_name = function ($object) {
    return (new ReflectionClass($object))->getShortName();
};
$delete_object = function (AbstractCrudObject $object) use($get_class_name) {
    echo sprintf(' > Deleting %s %d', $get_class_name($object), $object->{AbstractCrudObject::FIELD_ID}) . PHP_EOL;
    $object->delete();
};
$clean_edge = function ($cursor_provider) use($get_class_name, $delete_object) {
    try {
        $cursor = call_user_func($cursor_provider);
        if (!$cursor instanceof Cursor) {
            throw new ErrorException(sprintf("Provider returned instance of %s", $get_class_name($cursor)));
        }
        $cursor->setUseImplicitFetch(true);
 /**
  * @param Api|null $instance
  * @return Api
  * @throws \InvalidArgumentException
  */
 protected static function assureApi(Api $instance = null)
 {
     $instance = $instance ?: Api::instance();
     if (!$instance) {
         throw new \InvalidArgumentException('An Api instance must be provided as argument or ' . 'set as instance in the \\FacebookAds\\Api');
     }
     return $instance;
 }
 /**
  * {@inheritdoc}
  */
 public function init($accessToken)
 {
     Api::init(config('facebook-ads.app_id'), config('facebook-ads.app_secret'), $accessToken);
     return Api::instance();
 }
$access_token = null;
$app_id = null;
$app_secret = null;
// should begin with "act_" (eg: $account_id = 'act_1234567890';)
$account_id = null;
define('SDK_DIR', __DIR__ . '/..');
// Path to the SDK directory
$loader = (include SDK_DIR . '/vendor/autoload.php');
// Configurations - End
if (is_null($access_token) || is_null($app_id) || is_null($app_secret)) {
    throw new \Exception('You must set your access token, app id and app secret before executing');
}
if (is_null($account_id)) {
    throw new \Exception('You must set your account id before executing');
}
use FacebookAds\Api;
use FacebookAds\Logger\CurlLogger;
Api::init($app_id, $app_secret, $access_token);
// Create the CurlLogger
$logger = new CurlLogger();
// To write to a file pass in a file handler
// $logger = new CurlLogger(fopen('test','w'));
// If you need to escape double quotes, use the following - useful for docs
$logger->setEscapeLevels(1);
// Hide target ids and tokens
$logger->setShowSensitiveData(false);
// Attach the logger to the Api instance
Api::instance()->setLogger($logger);
use FacebookAds\Object\AdAccount;
use FacebookAds\Object\Fields\AdAccountFields;
$account = (new AdAccount($account_id))->read(array(AdAccountFields::ID, AdAccountFields::NAME, AdAccountFields::ACCOUNT_STATUS));
 /**
  * Authenticate & initiate
  *
  * @param string $appId
  * @param string $appSecret
  * @param string $token
  * @return Api|null
  */
 public function instanceWithAuth($appId, $appSecret, $token)
 {
     Api::init($appId, $appSecret, $token);
     return Api::instance();
 }
$custom_audience->read();
// _DOC close [CUSTOM_AUDIENCE_READ]
// _DOC open [CUSTOM_AUDIENCE_READ_RULE]
// _DOC vars [custom_audience_id]
// use FacebookAds\Object\CustomAudience;
// use FacebookAds\Object\Fields\CustomAudienceFields;
$custom_audience = new CustomAudience($custom_audience_id);
$custom_audience->read(array(CustomAudienceFields::NAME, CustomAudienceFields::RULE));
// _DOC close [CUSTOM_AUDIENCE_READ_RULE]
// _DOC open [CUSTOM_AUDIENCE_DELETE]
// _DOC vars [custom_audience_id]
// use FacebookAds\Object\CustomAudience;
$custom_audience = new CustomAudience($custom_audience_id);
$custom_audience->delete();
// _DOC close [CUSTOM_AUDIENCE_DELETE]
$api = Api::instance();
$response = $api->call('/me/accounts');
$data = $response->getContent();
$page_token = '';
foreach ($data['data'] as $page) {
    if ($page['id'] == $page_id) {
        $page_token = $page['access_token'];
        break;
    }
}
if ($page_token === '') {
    throw new \InvalidArgumentException('Page access token for the page id ' . $page_id . ' cannot be found.');
}
$page_session = new Session($config->appId, $config->appSecret, $page_token);
$page_api = new Api($api->getHttpClient(), $page_session);
$page_api->setLogger($api->getLogger());