2 ways to generate facebook share button code for website in php :
Method 1:
Facebook share button code for website in php
To add a Facebook share button to your website using PHP, you can use the following code:
<?php
$url = urlencode(‘http://example.com’); // Replace with the URL you want to share
$title = urlencode(‘Page Title’); // Replace with the title you want to share
$image = urlencode(‘http://example.com/image.jpg’); // Replace with the image you want to share (optional)
$summary = urlencode(‘Page summary’); // Replace with the summary you want to share (optional)
$link = ‘https://www.facebook.com/sharer/sharer.php?u=’ . $url . ‘&title=’ . $title . ‘&picture=’ . $image . ‘&summary=’ . $summary;
echo ‘<a href=”‘ . $link . ‘” target=”_blank”>Share on Facebook</a>’;
?>
In this code, replace the URL, page title, image URL, and summary with the appropriate information for your website. The $link
variable concatenates these values into a Facebook share URL, which is then used to create a link to the share page. When a user clicks on this link, they will be taken to Facebook where they can share the page with their friends.
Method 2:
To add a Facebook share button to your website using PHP, you can use the following code:
1. First, you need to include the Facebook SDK in your code. You can download it from the Facebook developers website and include it in your PHP file:
“` include_once ‘src/Facebook/autoload.php’; “`
2. Next, you need to create a Facebook app ID and secret key. You can do this through the Facebook developers website. 3. Once you have your app ID and secret key, you can create a Facebook share button using the following code:
“` $fb = new Facebook\Facebook([
‘app_id’ => ‘your-app-id’,
‘app_secret’ => ‘your-app-secret’,
‘default_graph_version’ => ‘v2.10’, ]);
$link = ‘http://example.com/’;
// Replace this with the URL you want to share.
$params = [ ‘href’ => $link, ‘layout’ => ‘button’, ‘size’ => ‘small’, ‘mobile_iframe’ => true ];
$button = $fb->get(‘/me?fields=id,name’, $params);
echo $button->getContent(); “`
4. This will create a Facebook share button with the URL you specified. When a user clicks on the button, it will share the URL on their Facebook profile. You can customize the button with different options for layout and size.
Note: Make sure to replace “your-app-id” and “your-app-secret” with the actual values for your Facebook app.
How to share website content on Facebook in PHP?
To share website content on Facebook using PHP, you can use the Facebook Graph API. The steps involved are:
- Create a Facebook app and get an access token for it. You can follow the instructions here: https://developers.facebook.com/docs/apps/register
- Include the Facebook PHP SDK in your project. You can download it from here: https://github.com/facebook/php-graph-sdk
- Use the following code to post the content to Facebook:<?php
require_once ‘path/to/facebook-php-sdk/autoload.php’; // Replace with the path to the Facebook PHP SDKuse Facebook\Facebook;
$fb = new Facebook([
‘app_id’ => ‘YOUR_APP_ID’,
‘app_secret’ => ‘YOUR_APP_SECRET’,
‘default_graph_version’ => ‘v3.2’,
]);$linkData = [
‘link’ => ‘https://www.example.com/’,
‘message’ => ‘Check out this great content on Example.com!’,
];try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->post(‘/me/feed’, $linkData, ‘{access-token}’);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
echo ‘Graph returned an error: ‘ . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
echo ‘Facebook SDK returned an error: ‘ . $e->getMessage();
exit;
}$graphNode = $response->getGraphNode();
echo ‘Posted content with id: ‘ . $graphNode[‘id’];
?>
Replace the YOUR_APP_ID
and YOUR_APP_SECRET
placeholders with your Facebook app’s ID and secret key, respectively. Also replace the access-token
placeholder with the access token you obtained in step 1.
In the $linkData
array, replace the link
value with the URL of the content you want to share, and replace the message
value with the message you want to accompany the share.
When you run this code, it will post the content to your Facebook timeline.
How to create a share option in PHP?
To create a share option in PHP, you can use the PHP header()
function to redirect the user to the social media site with the shared content. Here’s an example code to create a share option for Facebook:
<?php
$shareUrl = urlencode(‘https://www.example.com’); // Replace with the URL you want to share
$facebookShareUrl = ‘https://www.facebook.com/sharer/sharer.php?u=’ . $shareUrl;
?>
<a href=”<?php echo $facebookShareUrl; ?>” target=”_blank”>Share on Facebook</a>
In this code, the $shareUrl
variable contains the URL of the content you want to share. The urlencode()
function is used to encode the URL for use in the Facebook share URL.
The $facebookShareUrl
variable concatenates the encoded URL into a Facebook share URL. The link to the Facebook share page is created using an anchor tag with the href
attribute set to the $facebookShareUrl
variable. The target="_blank"
attribute is used to open the share page in a new tab or window.
You can use similar code for other social media sites by replacing the share URL with the appropriate URL for that site.