2013-01-13 6 views
0

Facebook API-PHP를 통해 Facebook 메시지 목록을 원합니다. PHP로 코드를 작성했습니다. ı가 코드를 작성했습니다.php 페이징을 사용하는 Facebook API 목록 수신함

 // Facebook App Connection 
    $facebook = new Facebook(array(
      'appId' => 'xxxxxxxxxxxx', 
      'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxx', 
     )); 
    $user = $facebook->getUser(); 
    if($user) { 
     try 
     { 
      $params = array(
        'fields' => 'inbox', 
       ); 
     $user_profile = $facebook->api('/me',$params); 
     } 
     catch (FacebookApiException $e) 
     { 
     error_log($e); 
     $user = null; 
     } 
    } 


    print_r($user_profile); 
    /* Output: 
Array 
(
    [id] => xxxxxxxxxx 
    [inbox] => Array 
     (
      [data] => Array 
       (
        [0] => Array 
         (
          [id] => xxxxxxxxxxxxxx 
          [to] => Array 
           (
            [data] => Array 
             (
              [0] => Array 
               (
                [name] => John Doe 
                [id] => xxxxxxxxxx 
               ) 

              [1] => Array 
               (
                [name] => Jane Doe 
                [id] => xxxxxxxxxx 
               ) 

             ) 

           ) 

          [updated_time] => 2013-01-13T21:33:34+0000 
          [unread] => 2 
          [unseen] => 1 
          [comments] => Array 
           (
            [data] => Array 
             (
              [0] => Array 
               (
                [id] => xxxxxxxxxxxxxx_xxxx 
                [from] => Array 
                 (
                  [name] => Jane Doe 
                  [id] => xxxxxxxxxx 
                 ) 

                [message] => Hi! 
                [created_time] => 2013-01-13T19:15:49+0000 
               ) 

              [1] => Array 
               (
                [id] => xxxxxxxxxxxxxx_xxxx 
                [from] => Array 
                 (
                  [name] => John Doe 
                  [id] => xxxxxxxxxx 
                 ) 

                [message] => Hi,How are you? 
                [created_time] => 2013-01-13T19:15:54+0000 
               ) 

              [2] => Array 
               (
                [id] => xxxxxxxxxxxxxx_xxxx 
                [from] => Array 
                 (
                  [name] => Jane Doe 
                  [id] => xxxxxxxxxx 
                 ) 

                [message] => Fine but have a problem facebook api 
                [created_time] => 2013-01-13T19:16:03+0000 
               ) 

              [3] => Array 
               (
                [id] => xxxxxxxxxxxxxx_xxxx 
                [from] => Array 
                 (
                  [name] => John Doe 
                  [id] => xxxxxxxxxx 
                 ) 

                [message] => what problem? 
                [created_time] => 2013-01-13T19:16:25+0000 
               ) 

              [4] => Array 
               (
                [id] => xxxxxxxxxxxxxx_xxxx 
                [from] => Array 
                 (
                  [name] => Jane Doe 
                  [id] => xxxxxxxxxx 
                 ) 

                [message] => not list all messages in inbox 
                [created_time] => 2013-01-13T21:04:37+0000 
               ) 

              [5] => Array 
               (
                [id] => xxxxxxxxxxxxxx_xxxx 
                [from] => Array 
                 (
                  [name] => John Doe 
                  [id] => xxxxxxxxxx 
                 ) 

                [message] => I can't help 
                [created_time] => 2013-01-13T21:04:40+0000 
               ) 
.bla 
.bla 
.bla 
.bla 
.bla 
.bla 
              [24] => Array 
               (
                [id] => xxxxxxxxxxxxxx_xxxx 
                [from] => Array 
                 (
                  [name] => John Doe 
                  [id] => xxxxxxxxxx 
                 ) 

                [message] => Okey,bye 
                [created_time] => 2013-01-13T21:33:34+0000 
               ) 

             ) 

            [paging] => Array 
             (
              [previous] => https://graph.facebook.com/377285305670598/comments?limit=25&since=1358112814&__paging_token=377285305670598_2394&__previous=1 
              [next] => https://graph.facebook.com/377285305670598/comments?limit=25&until=1358104549&__paging_token=377285305670598_2370 
             ) 

           ) 

         ) 



    */ 

출력하고 싶습니다.
Jane Doe : 안녕하세요.
John Doe : 안녕하세요?
제인 Doe : 좋아하지만 문제가 있습니다. facebook api
John Doe : 무슨 문제입니까?
신원 미상 :

안녕, 좋아요 나는 코드를 작성; 나는
신원 미상 도움이되지 수 :받은 편지함
신원 미상의 모든 메시지를 나열하지

내 결과

 for($i=0; $i <= 5; $i++){ 
     foreach($data as $a): 
     foreach($a[$i] as $id => $to): 
     foreach($to as $data): 
     foreach($data as $key => $value): 
      echo $value["name"]."=>".$value["message"]; 
     endforeach; 
      endforeach; 
     endforeach; 
    endforeach; 
    endforeach; 
    echo "<br/>"; 
;

Jane Doe =>
John Doe =>
=> 안녕하세요!
=> 안녕하세요, 안녕하세요?
=> 문제가 없지만 페이스 북의 API가
=> 무슨 문제입니까?
=>받은 편지함
에서 모든 메시지를 나열하지 => 나는 = 즐
이 ... 어쩌구
이 ... 어쩌구
=> 나는
시간을 도울 수
을 ... 도울 수 > h
h => h

모든 메시지와 보낸 사람 이름을 표시하고 싶습니다. 나는 노력했다.
1- 다음 URL 얻기 및 file_get_content ($ url); - 작동 안함.
2 제한 및 오프셋 부착 - 작동하지 않음
코드를 작성하는 방법?

답변

0

나는 이것이 당신이 원하는 것이라고 생각합니다.

foreach($user_profile as $main){ 
foreach($main as $a){ 
foreach($a as $b){ 
foreach($b as $c){ 
foreach($c as $d){ 
foreach($d as $e){ 
if(strlen($e["name"]) > 1){ 
echo "<b>".$e["name"].":</b>"; 
} 
} 
echo $d["message"]."</br>\n"; 
} 
} 
} 
} 
} 
0

이것은 도움이 될 것입니다.

<h1>Inbox Start</h1> 

    <?php 

    $inbox = $user_profile["inbox"]["data"]; 

    foreach ($inbox as $data){ ?> 



     <h3>To: </h3> 

     <?php 
     foreach ($data["to"]["data"] as $to){ 
     ?> 
      <a href="http://www.facebook.com/<?php echo $to["id"]?>" target="_blank"><?php echo $to["name"]?></a> &nbsp; 
     <?php 
     } 
     ?> 
     <h3>Last Reply: </h3> 
     <?php echo date("d M, Y H:i:s",strtotime($data["updated_time"]))?> 

     <h3>Message Read: </h3> 
     <?php if($data["unread"]==1){ echo "Unread";}else { echo "Read";}?> 


     <h3>Message Seen: </h3> 
     <?php if($data["unseen"]==1){ echo "Unseen";}else { echo "Seen";}?> 

     <h3>Comment: </h3> 
     <?php 
     $comments = $data["comments"]["data"]; 
     foreach ($comments as $comment){ 
     ?> 

      <div> 
       <div style="float:left; width:50px;"> 
        <img src="https://graph.facebook.com/<?php echo $comment['from']['id']?>/picture?width=50&height=50" /> 
       </div> 
       <div style="float:left; margin-left:10px;"> 
        <strong style="color: #AF2D2D;"> <?php echo $comment['from']['name']?></strong> <br /> 
        <p style="color: gray !important;"><?php echo date("d M, Y H:i:s",strtotime($comment["created_time"]));?></p> 
       </div> 

       <div style="clear:both; margin-bottom:10px;"></div> 
       <div style="width:500px; color: #AF2D2D;"> 
        <?php echo $comment['message'] ?> 

       </div>  

      </div> 



     <?php 
     } 
     ?> 

     <hr><bR> 

<?php 
    } 
    ?>