How can PHP be used to retrieve the direct URL of an image from a Facebook link?

To retrieve the direct URL of an image from a Facebook link using PHP, you can use the Facebook Graph API to fetch the image data and extract the direct URL from the response. You will need to make a request to the API endpoint for the Facebook link and parse the JSON response to get the image URL.

<?php
$fbLink = 'https://www.facebook.com/examplepage/posts/123456789'; // Replace with the Facebook link
$accessToken = 'YOUR_ACCESS_TOKEN'; // Replace with your Facebook Graph API access token

$response = file_get_contents("https://graph.facebook.com/v13.0/?id=$fbLink&fields=og_object{images}&access_token=$accessToken");
$data = json_decode($response, true);

if(isset($data['og_object']['images'][0]['source'])){
    $imageURL = $data['og_object']['images'][0]['source'];
    echo $imageURL;
} else {
    echo 'Image URL not found';
}
?>