How to share title and image to twitter using PHP website

6.8k Views Asked by At

I am sharing data to twitter.com and facebook.com. Sharing to facebook means automatically sending image, title, and domain it is working fine.

I want to implement in twitter, but it is not taking title and image.

while implementing twitter it is taking only the URL path,ig click the Share on Twitter

  <!-- Load Facebook SDK for JavaScript -->
  <div id="fb-root"></div>
<script>(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = 'https://connect.facebook.net/en_GB/sdk.js#xfbml=1&version=v2.12';
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>

  <!-- Your share button code -->
 <div class="fb-share-button" data-href="http://g2evolution.in/spreadit_API/event-details.php?id=MQ==&postedCustonerId=MQ==&eventType=UHVibGlj" data-layout="button_count" data-size="small" data-mobile-iframe="true"><a target="_blank" href="http://g2evolution.in/spreadit_API/event-details.php?id=MQ==&postedCustonerId=MQ==&eventType=UHVibGlj" class="fb-xfbml-parse-ignore">Share on Facebook</a></div> <br><br>

<a href="https://twitter.com/intent/tweet?url=http://g2evolution.in/spreadit_API/event-details.php?id=MQ==&postedCustonerId=MQ==&eventType=UHVibGlj">Share on Twitter</a>

1

There are 1 best solutions below

2
On

In regards to the share link, you can use the code below:

<?php

    $title = 'Title here';
    $short_url = 'http://shorturl.co';
    $url = 'http://fullurl.com';

    $twitter_params = 
    '?text=' . urlencode($title) . '+-' .
    '&amp;url=' . urlencode($short_url) . 
    '&amp;counturl=' . urlencode($url) .
    '';


    $link = "http://twitter.com/share" . $twitter_params . "";
?>

<a href="<?php echo $link; ?>">Share on Twitter</a>

<?php 
// rest of your code
?>

For sharing the image as facebook, you might want to look a previous answer.