Facebook Ads API - 如何在PHP中为自定义受众api调用添加多个规则

时间:2015-03-23 17:43:39

标签: json facebook facebook-graph-api facebook-ads-api

我正在尝试向api调用添加2个规则,如下所示:

$parameters     =       "name=TEST-Registered-CA&";
$parameters     .=      "subtype=WEBSITE&";
$parameters     .=      "retention_days=180&";
$parameters     .=      'rule={"and":[{"url":{"i_contains":"thankyou.asp"}},{"url":{"i_contains":"HotelVenueState=CA"}}]}';

https://graph.facebook.com/v2.2/act_12345/customaudiences?access_token=123456&$parameters;
  

当我尝试在Facebook Graph Explorer上运行时,它会出现以下错误   “(#2654)规则的JSON无效:规则的提供字符串无效JSON”Rerence url(enter link description here

任何人都可以告诉我我对json做错了什么吗?

2 个答案:

答案 0 :(得分:0)

您应该确保始终对查询参数进行正确的URL编码。通过连接字符串手动构建URL通常会导致无法正确解析的URL。

以下是如何构建上述网址:

<?php

$parameters = array(
  'access_token' => '123456',
  'name' => 'TEST-Registered-CA',
  'subtype' => 'WEBSITE',
  'retention_days' => '180',
  'rule' => '{"and":[{"url":{"i_contains":"thankyou.asp"}},{"url":{"i_contains":"HotelVenueState=CA"}}]}'
);

$url = 'https://graph.facebook.com/v2.2/act_12345/customaudiences?'.http_build_query($parameters, null, '&');

答案 1 :(得分:0)

在facebook v2.3的最新更新中,他们为这些调用添加了一个参数(method = post),当我添加它时,它对我有效。所以代码应该是这样的

$parameters     =       "method=post&";
$parameters     =       "name=TEST-Registered-CA&";
$parameters     .=      "subtype=WEBSITE&";
$parameters     .=      "retention_days=180&";
$parameters     .=      'rule={"and":[{"url":{"i_contains":"thankyou.asp"}},{"url":{"i_contains":"HotelVenueState=CA"}}]}';

https://graph.facebook.com/v2.2/act_12345/customaudiences?access_token=123456&$parameters;