PHP script for creating a Social Media Sharing System

0
553

The following PHP script defines a URL and title for the content to share, and then loops through an array of social media platforms and generates share links for each one.

The share link URLs are generated by appending the content URL and title to the platform’s URL prefix, and the script outputs HTML links for each platform.

When a user clicks one of the links, they will be taken to the corresponding social media platform’s share page with the content URL and title pre-populated.

Follow this video for complete guidance :

<?php
// Define the URL and title of the content to share
$url = 'https://reeteshghimire.com.np/';
$title = 'Lets Try This!';

// Define an array of social media platforms and their corresponding share URLs
$platforms = [
    'Facebook' => 'https://www.facebook.com/sharer/sharer.php?u=',
    'Twitter' => 'https://twitter.com/intent/tweet?url=',
    'LinkedIn' => 'https://www.linkedin.com/shareArticle?mini=true&url=',
    'Pinterest' => 'https://pinterest.com/pin/create/button/?url=',
];

// Loop through the platforms and generate share links
foreach ($platforms as $name => $urlPrefix) {
    // Generate the share link URL by appending the content URL and title to the platform's URL prefix
    $shareUrl = $urlPrefix . urlencode($url) . '&title=' . urlencode($title);
    
    // Output the share link HTML for the platform
    echo "<a href=\"$shareUrl\" target=\"_blank\">Share on $name</a><br>";
}
?>

 

 

Comments are closed.