2012-02-21 21 views

回答

0

您可以使用下面的代碼發佈到Facebook塗鴉牆,只需調用函數與正確的參數

試試這個,

<?php 


    function doWallPost($postName='',$postMessage='',$postLink='',$postCaption='',$postDescription='') 
    { 
    $FB_APP_ID='xxxxxxxxxxxxxxxxxxxxxxxx'; 
    $FB_APP_SECRET='xxxxxxxxxxxxxxxxxxxxxxxxxxx'; 

    $APP_RETURN_URL=((substr($_SERVER['SERVER_PROTOCOL'],0,4)=="HTTP")?"http://":"https://").$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME']; 

    $code = $_REQUEST["code"]; 

    if(empty($code)) 
    { 
     $dialog_url = "http://www.facebook.com/dialog/oauth?client_id=".$FB_APP_ID."&redirect_uri=".$APP_RETURN_URL."&scope=publish_stream";     
     header("Location:$dialog_url"); 
    } 

    $token_url = "https://graph.facebook.com/oauth/access_token?client_id=".$FB_APP_ID."&redirect_uri=".urlencode($APP_RETURN_URL)."&client_secret=".$FB_APP_SECRET."&code=".$code; 
    $access_token = file_get_contents($token_url); 

    $param1=explode("&",$access_token); 
    $param2=explode("=",$param1[0]); 
    $FB_ACCESS_TOKEN=$param2[1]; 


    $url = "https://graph.facebook.com/me/feed"; 
    $attachment = array( 'access_token' => $FB_ACCESS_TOKEN,       
        'name'   => $postName, 
        'link'   => $postLink, 
        'description' => $postDescription, 
        'message'  => $postMessage, 
        'caption'  => $postCaption, 
       ); 

    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); 
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT,2); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,true); 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment); 
    $result=curl_exec($ch); 
    header('Content-type:text/html'); 
    curl_close($ch); 

    return $result 
    } 







    ?> 
+1

日Thnx這個代碼,但我想在JavaScript中不要在PHP – 2012-02-21 05:29:46

+2

@RanaMuhammadUsman爲什麼不能你做一個Ajax調用 – 2012-02-21 05:33:36

+1

我ahve使用FB API使用JavaScript,每一件事情是確定接受此關聯 的https: //s-static.ak.fbcdn.net/connect/xd_proxy.php?version=3#cb=f2c3dbe8c4&origin=http://localhost:8080/f1c4ddee7&relation=opener&transport=postmessage&frame=f26fe99c8&result=%7B%22post_id%22%3a %22100001210701227_250276391723830%22%7D – 2012-02-21 05:39:00

2

下面這個簡單的JavaScript示例演示如何使用在JavaScript SDK中使用Feed對話框中的FB.ui方法

FB.init({appId:「YOUR_APP_ID」,status:true,cookie:true});

function postToFeed() { 

    // calling the API ... 
    var obj = { 
    method: 'feed', 
    name: 'First Application' 
    }; 

    function callback(response) { 
     alert("Post ID: " + response['post_id']); 
    } 

    FB.ui(obj, callback); 
    } 
+2

我使用相同的方法看代碼,我的問題是其他請閱讀 – 2012-02-21 07:35:54

+2

@RanaMuhammadUsman這個問題我試過了你已經發布的代碼,它工作正常 – sadik 2012-02-21 07:49:26

+2

@RanaMuhammadUsman我的事情你的params origin = locahost)去錯誤的檢查你的代碼。 – sadik 2012-02-21 07:50:30