2014-09-01 59 views
1
<?php 
    $title=urlencode('Nature'); 
    $url=urlencode('http://trainees.ocs.org/training/hariharan/01-09-2014/facebook.php'); 
    $image=urlencode('http://trainees.ocs.org/training/hariharan/01-09-2014/images/img1.jpg'); 
?> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Sharing Images on Facebook</title> 
<link href="css/facebook.css" rel="stylesheet" type="text/css" /> 
</head> 

<body> 
<div class="all"> 
    <div class="top"> 
    <div class="img"><img src="images/img1.jpg" height="220" width="550" /></div> 
    <div class="share"><a onClick="window.open('http://www.facebook.com/sharer.php?s=100&amp;p[title]=<?php echo $title;?>&amp;p[url]=<?php echo $url; ?>&amp;&amp;p[images][0]=<?php echo $image;?>','sharer','toolbar=0,status=0,width=550,height=220');" href="javascript: void(0)">Share</a></div> 
    <p>&nbsp;</p> 
    </div> 
</div> 
</body> 
</html> 

上面的代碼是通過PHP共享Facebook上的圖像。但該圖片無法顯示在我的帳戶中。如何使用PHP在Facebook上分享圖像?...請幫助我的朋友。在facebook上分享通過php的圖像

+0

股份對話框不會採取任何額外的參數之外的URL分享更多的 - 所有其他信息(標題,描述,圖片)將被從該URL下的文件中包含的Open Graph meta標籤服用。 – CBroe 2014-09-01 11:12:02

回答

1

以下是您需要使用PHP共享Facebook鏈接的代碼。只需稍作更改,您就可以使用此代碼發佈消息(無鏈接),或者上傳Facebook專輯中的照片。

<?php 
// require Facebook PHP SDK 
// see: https://developers.facebook.com/docs/php/gettingstarted/ 
require_once("/YOUR_PATH_TO/facebook_php_sdk/facebook.php"); 

// initialize Facebook class using your own Facebook App credentials 
// see: https://developers.facebook.com/docs/php/gettingstarted/#install 
$config = array(); 
$config['appId'] = 'YOUR_APP_ID'; 
$config['secret'] = 'YOUR_APP_SECRET'; 
$config['fileUpload'] = false; // optional 

$fb = new Facebook($config); 

// define your POST parameters (replace with your own values) 
$params = array(
    "access_token" => "YOUR_ACCESS_TOKEN", // see: https://developers.facebook.com/docs/facebook-login/access-tokens/ 
    "message" => "Here is a blog post about auto posting on Facebook using PHP #php #facebook", 
    "link" => "http://www.pontikis.net/blog/auto_post_on_facebook_with_php", 
    "picture" => "http://i.imgur.com/lHkOsiH.png", 
    "name" => "How to Auto Post on Facebook with PHP", 
    "caption" => "www.pontikis.net", 
    "description" => "Automatically post on Facebook with PHP using Facebook PHP SDK. How to create a Facebook app. Obtain and extend Facebook access tokens. Cron automation." 
); 

// post to Facebook 
// see: https://developers.facebook.com/docs/reference/php/facebook-api/ 
try { 
    $ret = $fb->api('/YOUR_FACEBOOK_ID/feed', 'POST', $params); 
    echo 'Successfully posted to Facebook'; 
} catch(Exception $e) { 
    echo $e->getMessage(); 
} 
?> 
2

您需要先初始化像

<script src="http://connect.facebook.net/en_US/all.js"></script> 
<script> 
    window.fbAsyncInit = function() { 
     FB.init({ 
      appId  : your app Id 
      channelUrl : your site url //opional 
      status  : true, // check login status (we don't make use of this) 
      cookie  : true, // enable cookies to allow the server to access the session 
      xfbml  : true // parse XFBML 
     }); 
    }; 

    // Load the SDK Asynchronously 
    (function(d){ 
     var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0]; 
     if (d.getElementById(id)) {return;} 
     js = d.createElement('script'); js.id = id; js.async = true; 
     js.src = "//connect.facebook.net/en_US/all.js"; 
     ref.parentNode.insertBefore(js, ref);   
    }(document)); 
</script> 

然後在你的頁面寫在你記錄就緒狀態Facebook的腳本

<script type="text/javascript"> 
     $(document).ready(function(){ 
      $('#share_button').click(function(e){ 
       e.preventDefault(); 
       FB.ui(
       { 
        method: 'feed', 
        name: 'Your message', 
        link: 'your site url', 
        picture: '<?php echo $baseurl; ?>/your/image/url', 
        caption: 'Image caption', 
        description: '', 
        message: 'This is the information that you want to show people.' 
       }); 
      }); 
     }); 
    </script> 

這是分享按鈕

<div class="share"> 
    <a id="share_button" href=""><img src="images/fb_like.png" alt="" /></a> 
</div> 

這適用於我希望能幫助你。

+0

什麼是應用程序ID先生?... – 2014-09-01 09:54:12

+0

上述代碼無法正常工作先生... – 2014-09-01 10:13:05

+0

您需要在您的Facebook開發人員網站中創建您的應用程序ID,您可以按照此鏈接https://developers.facebook.com/docs/unity/getting-started/canvas或http://www.hyperarts.com/blog/how-to-create-facebook-application-to-get-an-app-id-for-your-website/ – herr 2014-09-01 10:47:37

相關問題