Facebook即时文章:使用图谱API创建文章

时间:2016-10-19 16:47:42

标签: php facebook facebook-graph-api facebook-instant-articles

我一直在开发一个WordPress插件,它使用Facebook的API创建即时文章。 (我从这里使用SDK版本5.3.1:https://github.com/facebook/php-graph-sdk/tree/5.4)它需要包含许多自定义字段,因此新插件而不是使用现有插件。我使用Vagrant / VirtualBox linux服务器在我的本地机器上开发了它,从那里我能够成功验证并创建一个Instant Article,然后它出现在Facebook的发布工具中的列表中>即时文章>发展文章。

我将相同的代码部署到实时Web服务器以继续测试它。身份验证仍然成功但当我尝试从该服务器发布文章时,Instant Article永远不会创建,也不会出现在Facebook的列表中。

令人费解的是,两次尝试的反应几乎相同。一个区别是 accessToken 值,我将其标记为{access_token}。回复之间的其他差异我标记为{different}

任何人都可以帮助我理解为什么一个成功而另一个失败而没有PHP或API响应中的任何明显错误?

使用本地计算机的响应(成功):

object(Facebook\FacebookResponse)#144 (6) {
  ["httpStatusCode":protected]=>
  int(200)
  ["headers":protected]=>
  array(13) {
    ["Access-Control-Allow-Origin"]=>
    string(1) "*"
    ["Pragma"]=>
    string(8) "no-cache"
    ["Cache-Control"]=>
    string(44) "private, no-cache, no-store, must-revalidate"
    ["facebook-api-version"]=>
    string(4) "v2.7"
    ["Expires"]=>
    string(29) "Sat, 01 Jan 2000 00:00:00 GMT"
    ["Content-Type"]=>
    string(31) "application/json; charset=UTF-8"
    ["x-fb-trace-id"]=>
    string(11) "{different}"
    ["x-fb-rev"]=>
    string(7) "{different}"
    ["Vary"]=>
    string(15) "Accept-Encoding"
    ["X-FB-Debug"]=>
    string(88) "{different}"
    ["Date"]=>
    string(29) "Wed, 19 Oct 2016 16:24:51 GMT"
    ["Connection"]=>
    string(10) "keep-alive"
    ["Content-Length"]=>
    string(2) "25"
  }
  ["body":protected]=>
  string(25) "{"id":"{different}"}"
  ["decodedBody":protected]=>
  array(1) {
    ["id"]=>
    string(16) "{different}"
  }
  ["request":protected]=>
  object(Facebook\FacebookRequest)#145 (9) {
    ["app":protected]=>
    object(Facebook\FacebookApp)#183 (2) {
      ["id":protected]=>
      string(16) "{app_id}"
      ["secret":protected]=>
      string(32) "{app_secret}"
    }
    ["accessToken":protected]=>
    string(168) "{access_token}"
    ["method":protected]=>
    string(4) "POST"
    ["endpoint":protected]=>
    string(29) "/{some_endpoint_id}/instant_articles"
    ["headers":protected]=>
    array(1) {
      ["Content-Type"]=>
      string(33) "application/x-www-form-urlencoded"
    }
    ["params":protected]=>
    array(3) {
      ["development_mode"]=>
      string(1) "1"
      ["published"]=>
      bool(false)
      ["html_source"]=>
      string(1600) "<html> ... </html>"
    }
    ["files":protected]=>
    array(0) {
    }
    ["eTag":protected]=>
    NULL
    ["graphVersion":protected]=>
    string(4) "v2.7"
  }
  ["thrownException":protected]=>
  NULL
}

使用Live Server的响应(失败):

object(Facebook\FacebookResponse)#107 (6) {
  ["httpStatusCode":protected]=>
  int(200)
  ["headers":protected]=>
  array(13) {
    ["Access-Control-Allow-Origin"]=>
    string(1) "*"
    ["Pragma"]=>
    string(8) "no-cache"
    ["Cache-Control"]=>
    string(44) "private, no-cache, no-store, must-revalidate"
    ["facebook-api-version"]=>
    string(4) "v2.7"
    ["Expires"]=>
    string(29) "Sat, 01 Jan 2000 00:00:00 GMT"
    ["Content-Type"]=>
    string(31) "application/json; charset=UTF-8"
    ["x-fb-trace-id"]=>
    string(11) "{different}"
    ["x-fb-rev"]=>
    string(7) "{different}"
    ["Vary"]=>
    string(15) "Accept-Encoding"
    ["X-FB-Debug"]=>
    string(88) "{different}"
    ["Date"]=>
    string(29) "Wed, 19 Oct 2016 16:02:08 GMT"
    ["Connection"]=>
    string(10) "keep-alive"
    ["Content-Length"]=>
    string(2) "24"
  }
  ["body":protected]=>
  string(24) "{"id":"{different}"}"
  ["decodedBody":protected]=>
  array(1) {
    ["id"]=>
    string(15) "{different}"
  }
  ["request":protected]=>
  object(Facebook\FacebookRequest)#106 (9) {
    ["app":protected]=>
    object(Facebook\FacebookApp)#126 (2) {
      ["id":protected]=>
      string(16) "{app_id}"
      ["secret":protected]=>
      string(32) "{app_secret}"
    }
    ["accessToken":protected]=>
    string(164) "{access_token}"
    ["method":protected]=>
    string(4) "POST"
    ["endpoint":protected]=>
    string(29) "/{some_endpoint_id}/instant_articles"
    ["headers":protected]=>
    array(1) {
      ["Content-Type"]=>
      string(33) "application/x-www-form-urlencoded"
    }
    ["params":protected]=>
    array(3) {
      ["development_mode"]=>
      string(1) "1"
      ["published"]=>
      bool(false)
      ["html_source"]=>
      string(1639) "<html> ... </html>"
    }
    ["files":protected]=>
    array(0) {
    }
    ["eTag":protected]=>
    NULL
    ["graphVersion":protected]=>
    string(4) "v2.7"
  }
  ["thrownException":protected]=>
  NULL
}

1 个答案:

答案 0 :(得分:1)

感谢CBroe在评论中提出的建议,我能够发现问题的原因。在向get the article import status发出请求后,我在响应中发现了此错误消息:

["body":protected]=>
  string(439) "{"errors":[{"level":"ERROR","message":"Unclaimed URL: The URL http:\/\/www.example.com\/my-article-url\/ has not been claimed for Instant Articles. Please check to make sure you have a URL registered for your page. For more information refer to URLs in the Publishing Articles section of the Instant Articles documentation."}],"status":"FAILED","id":"{some_id}"}"

然后我意识到,在我构建Instant Article标记的函数中,我在标记上做了str_replace,这样我的本地测试域名就会被正确的&#34;声明&#34替换;域名。由于我们已将代码部署到新域,因此未进行此替换,并且Facebook看到了错误的&#34;无人认领的&#34;标记中的域。

通过将新域名添加到要替换的名称数组中,我能够使用&#34;声明&#34;将标记发送到Instant Articles。域。我的文章成功创作了!

非常感谢CBroe,我正在失去理智。