0

I've tried to add a thumbnail to the facebook app link, but can't even find documentation about it. Is it possible? The current code (PHP/Laravel) gives me a working link, which looks like this: https: // fb.me/1234567890. It writes the app name as well when posted on Facebook, but with no image/thumbnail. I've tried putting an "image" or "thumbnail" parameter in http_build_query, but with no luck.

    $url = "https://graph.facebook.com/v2.6/app/app_link_hosts";
$ch = curl_init($url);

# create form post data
$metadata = "?item=" . $request->itemid;

$deepLinkURL = "APP://" . $metadata;

//echo $deepLinkURL;
$androidArray = json_encode(array(array("url"          => $deepLinkURL,
                                    "package" => "com.app.package",
                                    "app_name"     => "APPNAME")
                              )
                       ); 

$iosArray = json_encode(array(array("url"          => $deepLinkURL,
                                    "app_store_id" => 45345345,
                                    "app_name"     => "APPNAME")
                              )
                       );

$webFallbackArray = json_encode(array("should_fallback" => false));


$formQuery = http_build_query(array("access_token" => "1234567890|XXXXXXXXXXXXXXXX",
                                    "name"         => "APPNAME",
                                    "android"      => $androidArray,
                                    "ios"          => $iosArray,
                                    "thumbnail"        => "http://i.imgur.com/upnywSR.jpg",
                                    "web"          => $webFallbackArray)
                              );

  $path = base_path() . "/vendor/phpunit/phpunit/build/ca.pem"; 

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_CAINFO, $path);

# options
curl_setopt($ch, CURLOPT_POST, true); //1
curl_setopt($ch, CURLOPT_POSTFIELDS, $formQuery);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

# get response

$resultStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$jsonResponse = json_decode(curl_exec($ch), true);
curl_close($ch);



# decode response from facebook

$appLinkId = "";



# get appLinkId
foreach ($jsonResponse as $key => $val) {

    # get status
    if($key == "id") {
        $appLinkId = $val;
    }
}

# if response is good, need to request canonical URL from appLinkId
$errorMessage = "";
$canonicalUrl = "";

if(!empty($appLinkId)) {



    # create another instance of cURL to get the appLink object from facebook using the ID generated by the previous post request
    $getAppLinkUrl = "https://graph.facebook.com/" . $appLinkId;
    $ch2 = curl_init($getAppLinkUrl);

    # cURL options
    $queryString = http_build_query(array("access_token" => "206722406330430|XRV38UNZsFfRNNF1EkfikzDWkpk",
                                          "fields"       => "canonical_url",
                                          "pretty"       => true)
                                    );
/////////////////////
$path = base_path() . "/vendor/phpunit/phpunit/build/ca.pem"; 

curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch2, CURLOPT_CAINFO, $path);
/////////////////


    curl_setopt($ch2, CURLOPT_URL, $getAppLinkUrl . "?" . $queryString);
    curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);

    # get response
  //  $urlResponseJson = curl_exec($ch2);
      $urlJsonResponse = json_decode(curl_exec($ch2), true);
    curl_close($ch2);

    # decode response from facebook


    # parse response to get canonical URL
    foreach ($urlJsonResponse as $key => $val) {
        # get canonical URL
        if($key == "canonical_url") {
            $canonicalUrl = $val;
        }
    }

    # check for result
    if(empty($canonicalUrl)) {
        $errorMessage = "Unable to retreive URL.";
    }

} else {
    $errorMessage = "Unable to publish appLink.";
}

# encode response back to your app
if(empty($errorMessage)) {
    $response = json_encode(array("result"        => "success",
                                  "canonical_url" => $canonicalUrl));
} else {
    $response = json_encode(array("result" => "failed",
                                  "errorMessage" => $errorMessage));
}
return $response;
Rad
  • 830
  • 1
  • 12
  • 24

1 Answers1

0

I've tried to add a thumbnail to the facebook app link, but can't even find documentation about it. Is it possible?

No.

As https://developers.facebook.com/docs/applinks/hosting-api says,

If your application doesn't have a website for content you want to share to Facebook, you don't have public web URLs which you can annotate to support App Links. For these types of apps, Facebook provides an App Links Hosting API that will host App Links for you.

So if you have public web URLs that you want to share, then you should rather annotate those with the meta tags for App Links – then it will take the thumbnail you specified for those URLs via og:image.

If that is not an option, then you could still try and specify a thumbnail when you share the canonical URL of the App Link object, f.e. via the Feed dialog.

CBroe
  • 91,630
  • 14
  • 92
  • 150