2013-06-08 31 views
5

tôi muốn ứng dụng của tôi đăng trên tường của người dùng thay mặt (ví dụ: quảng cáo). Tôi nghĩ về việc tạo ra một kịch bản php, mà tôi sẽ thực hiện với một công việc cron trên máy chủ của tôi mỗi tuần. Tôi có tài khoản người dùng trong cơ sở dữ liệu của mình. Bây giờ tôi muốn tập lệnh nhận một userid và sau đó đăng lên tường của người dùng. (Tất nhiên nếu người dùng vẫn cài đặt ứng dụng và cấp quyền cho phép xuất bản)Tự động đăng trên tường của người dùng (ứng dụng facebook)

có thể tạo tập lệnh kích hoạt tính năng này không?
$ post = $ facebook-> api ("/ $ user1/feed", "POST", $ params); hoặc
$ post = $ facebook-> api ("/ $ user2/feed", "POST", $ params); v.v ...?

Thank cho tư vấn

<?php 

require 'src/facebook.php'; 
$app_id = 'yourappid'; 
$app_secret = 'yourappsecret'; 
$app_namespace = 'appname'; 
$app_url = 'https://apps.facebook.com/' . $app_namespace . '/'; 
$scope = 'email,publish_actions'; 


// Init the Facebook SDK 
$facebook = new Facebook(array(
'appId' => $app_id, 
'secret' => $app_secret, 
)); 



// Get the current user 
$user = $facebook->getUser(); 

// If the user has not installed the app, redirect them to the Auth Dialog 
if (!$user) { 
    $loginUrl = $facebook->getLoginUrl(array(
    'scope' => $scope, 
    'redirect_uri' => $app_url, 
)); 

    print('<script> top.location.href=\'' . $loginUrl . '\'</script>'); 

    } 


    else { 

     try { 
     $params = array(
      'message'  => "your message", 
      'name'   => "hello world", 
      'description' => "hello world", 
      'link'   => "hello world", 
      'picture'  => "hello world", 
     ); 

     $post = $facebook->api("/$user/feed","POST",$params); 

     echo ""; 

    } 
     catch (FacebookApiException $e) { 
     $result = $e->getResult(); 
    } 

    } 

?> 
+0

Và vấn đề của bạn với mã ở trên là gì ngoài việc bạn yêu cầu cộng đồng lập trình cho bạn? –

+0

có thể tạo tập lệnh kích hoạt tính năng này không? $ post = $ facebook-> api ("/ $ user1/feed", "POST", $ params); hoặc $ post = $ facebook-> api ("/ $ user2/feed", "POST", $ params); v.v ...? –

Trả lời

4

Để giải quyết vấn đề của bạn, tôi sẽ chia sẻ mã của tôi với bạn. điều này làm việc cho tôi.

Bạn chỉ cần viết một số vòng lặp trong khi chọn tất cả id người dùng từ cơ sở dữ liệu của mình.

<?php 

    //// publish as status 

    //// publish post to users 


     require_once("facebook.php"); 


     $app_id = "xxxxxxxxxxx"; 
     $app_secret = "xxxxxxxxxxxxxx"; 
     $my_url = "http://t.xxxxx.net/facebook/publish.php/"; // refer number 

    /// 
     $config = array(); 
     $config['appId'] = 'xxxxxxxx'; 
     $config['secret'] = 'xxxxxxxxxxxx'; 
     $config['fileUpload'] = false; // optional 

     $facebook = new Facebook($config); 
     /// 

    //// 

     function getAccessToken() { 
     if ($this->accessToken !== null) { 
      return $this->accessToken; 
     } 

     $this->setAccessToken($this->getApplicationAccessToken()); 
     $user_access_token = $this->getUserAccessToken(); 
     if ($user_access_token) { 
      $this->setAccessToken($user_access_token); 
     } 

     return $this->accessToken; 
     } 

     function getApplicationAccessToken() { 
     return $this->appId.'|'.$this->appSecret; 
    } 


    /////////////////////////////// Update status Function 
    //xxxxxxxx 
    $session = $facebook->getUser(); 



    // New SDK 
    $facebook->api (array(
     'method' => 'users.setStatus', 
    'status' => 'Hi 
this new status by my app 

    ', 
     'uid' => '21511',/// user_id 
     'session'=>$session, 
    )); 


    ?> 
+1

Cảm ơn bạn rất nhiều :) Nhưng sau một chút đọc ở đây trên stackoverflow, tôi thấy bài đăng này: http://stackoverflow.com/a/15948611/2466312 Vì vậy, tôi nghĩ rằng giải pháp (tốt) của bạn sẽ không hoạt động nữa:/?? –

+1

Điều này vẫn hoạt động, cảm ơn –

Các vấn đề liên quan