通过Facebook PHP Business SDK上载了广告的视频状态

时间:2018-08-28 16:26:31

标签: php facebook facebook-graph-api facebook-php-sdk

我正在尝试通过Facebook Business SDK制作广告。一切正常,直到我尝试创建AdCreativeVideoData。代码:

 protected function createAdVideoCreative($thumbnail_url, $video_id, $name){
    $video_data = new AdCreativeVideoData();
    $video_data->setData(array(
        AdCreativeVideoDataFields::IMAGE_URL => $thumbnail_url,
        AdCreativeVideoDataFields::VIDEO_ID => $video_id,
        AdCreativeVideoDataFields::CALL_TO_ACTION => array(
            'type' => AdCreativeCallToActionTypeValues::LIKE_PAGE,
            'value' => array(
                'page' => FbAds::PAGE_ID,
            ),
        ),
    ));

    $object_story_spec = new AdCreativeObjectStorySpec();
    $object_story_spec->setData(array(
        AdCreativeObjectStorySpecFields::PAGE_ID => FbAds::PAGE_ID,
        AdCreativeObjectStorySpecFields::VIDEO_DATA => $video_data,
    ));

    $creative = new AdCreative(null, FbAds::AD_ACCOUNT_ID);

    $creative->setData(array(
        AdCreativeFields::NAME => $name,
        AdCreativeFields::OBJECT_STORY_SPEC => $object_story_spec,
    ));

    try {
        $creative->create();

        return $creative;
    } catch (Exception $e) {
        print("Create Ad Video Creative Exception: " . $e->getMessage() . " (" . $e->getCode() . ")");

        exit;
    }
}

通过以下方法将所选视频上传到Facebook时,将调用上述方法:

   protected function createAdVideo($video_path){
    $video = new Advideo(null, FbAds::AD_ACCOUNT_ID);

    $video->{AdVideoFields::SOURCE} = $video_path;

    try {
        $video->create();

        return $video->{AdVideoFields::ID};
    } catch (Exception $e) {
        print("Create Ad Video Exception: " . $e->getMessage() . " (" . $e->getCode() . ")");

        exit;
    }

}

问题是,当我尝试创建AdCreativeVideoData时,会引发以下错误:

[message] => Invalid parameter
[type] => OAuthException
[code] => 100
[error_subcode] => 1885252
[is_transient] => 
[error_user_title] => Video not ready for use in an ad
[error_user_msg] => The video is still being processed. Please wait for the video to finish processing before using it in an ad.
[fbtrace_id] => AwW0d9+Piz1

如您所见,视频尚未处理。我的问题是:如何检查视频的状态?是否可以通过ping检查状态的某个端点可用? documentation指出我可以检查状态,但是createAdVideo()方法中的AdVideo对象没有状态字段:

AdVideo fields

我在这里很茫然,所以我希望有人可以阐明这个问题。预先感谢!

2 个答案:

答案 0 :(得分:0)

AdVideo 此后没有status字段,但 Video 却有:https://developers.facebook.com/docs/graph-api/reference/video

内部是相同的id,因此您可以请求https://graph.facebook.com/v4.0/{video-id}?fields=id,status,它将返回上传的(Ad)Video 的状态。

答案 1 :(得分:0)

我认为这是因为视频根本没有上传。 而不是使用“源”,请尝试使用“ file_url”。您可能还想添加参数“标题”,这样就不会将其命名为“无标题的视频”,但这不是必需的。

并尝试像这样更聪明地使用SDK:

$myVideoUpload = (new AdAccount("act_123456676"))-
>createAdVideo(
array() //fields
array( //params
"file_url"=>"http://whatever.com",
"title"=>"my title"
)
);

如果此方法有效,它将返回id = video_id的json_encodes字符串。

如果您希望能够检索错误-如果有的话-以及所有API调用的通用方法,请确实使用图形api:

$fb = new Facebook(array(
"app_id"=>"blalaa",
"app_secret"=>"blaaaaa",
"default_graph_version"=>"v9.0"
));
$url = "/act_123456789/advideos";
$access_token = "my token";
$params = 
array("file_url"=>"https://whatever.com","title"=>"some video");
try{
 $response = $fb->post(
  $url,
  $params,
  $access_token
 )
 $response = $response->getGraphNode();
} catch(FacebookResponseException $e) {
  return "graph error:: " . $e->getMessage();
} catch(FacebookSDKException$e) {
  return "sdk error:: " . $e->getMessage();
}

如果您有一个access_token可以访问您要请求的特定边缘,那么最后一个可以应用于所有内容,因此只需更改URL和参数即可。

注意: 使用graph-api时:如果要更改或创建某些内容,请使用POST;如果只想阅读,请使用GET。