2016-12-30 126 views
0

我需要打印在PHP/HTML表格JSON響應,我被困了4天..打印JSON響應

我有JSON響應如下,它顯示的鏈接http://localhost:8080/fb/?code=AQCtq-HglnDn5OZGCZAgnqiDHgL6fRHIC0054JJs6HgMmqw8h_qNDSqToe4ZxAM6kkGLDIzUaoruR0rfSlSenwRnWNv6Q6TH0Gbf6jbQ73m1uVEYnt_jrPiyawtKvj6bASITtMFC41Lly5SO25NOBRXP00RWN3uQoB3SxOe3ok3eypFZ26IRW1amNhDj3QdknvjpqsrpyhEUwK3NnHkRZmQqCKdvXywcZ-ENRxYAwN1rg4Gt0-fsdpnnKaQNx63MkXm6qEiSyIMPnZVHE7QPAJzIgdmUtK-aJD5lePpoIVJKT9P-Yy0R3ZIB2rHR5OTorx1WUOs5gHI7ANDEQnJN4mB4&state=32c7de5b745a4def2865de6684770d2d#=

array(3) { 
    ["data"]=> 
    array(25) { 
    [0]=> 
    array(2) { 
     ["id"]=> 
     string(15) "961716293930505" 
     ["name"]=> 
     string(10) "Nena Gomez" 
    } 
    [1]=> 
    array(2) { 
     ["id"]=> 
     string(15) "738589686317947" 
     ["name"]=> 
     string(13) "Betuxx Chacon" 
    } 
    [2]=> 
    array(2) { 
     ["id"]=> 
     string(17) "10154358290553845" 
     ["name"]=> 
     string(24) "Gabriel Heredia Briseño" 
    } 
    [3]=> 
    array(2) { 
     ["id"]=> 
     string(16) "1568587126490267" 
     ["name"]=> 
     string(18) "Armando Von Thesse" 
    } 
    [4]=> 
    array(2) { 
     ["id"]=> 
     string(17) "10211420006316586" 
     ["name"]=> 
     string(11) "Any Obregon" 
    } 
    [5]=> 
    array(2) { 
     ["id"]=> 
     string(16) "1464100830268739" 
     ["name"]=> 
     string(35) "AarOnn LeOnne Raamiireez Raamiireez" 
    } 
    [6]=> 
    array(2) { 
     ["id"]=> 
     string(16) "1210996895614354" 
     ["name"]=> 
     string(23) "María José Hernández" 
    } 
    [7]=> 
    array(2) { 
     ["id"]=> 
     string(17) "10205997740821868" 
     ["name"]=> 
     string(14) "Mario Martinez" 
    } 
    [8]=> 
    array(2) { 
     ["id"]=> 
     string(16) "1898457230384035" 
     ["name"]=> 
     string(23) "Veronica Ramirez Cuevas" 
    } 
    [9]=> 
    array(2) { 
     ["id"]=> 
     string(16) "1333307456741256" 
     ["name"]=> 
     string(17) "Nicacio González" 
    } 
    [10]=> 
    array(2) { 
     ["id"]=> 
     string(17) "10210081324343375" 
     ["name"]=> 
     string(20) "Ariela Ampai Herrera" 
    } 
    [11]=> 
    array(2) { 
     ["id"]=> 
     string(16) "1383385655027101" 
     ["name"]=> 
     string(10) "Leyda Luna" 
    } 
    [12]=> 
    array(2) { 
     ["id"]=> 
     string(17) "10154085294360936" 
     ["name"]=> 
     string(17) "Ivan Azael Rivera" 
    } 
    [13]=> 
    array(2) { 
     ["id"]=> 
     string(16) "1839478686312278" 
     ["name"]=> 
     string(16) "Isamar Rodriguez" 
    } 
    [14]=> 
    array(2) { 
     ["id"]=> 
     string(15) "954280078035568" 
     ["name"]=> 
     string(32) "Seylin Mariana Brenes Peñaranda" 
    } 
    [15]=> 
    array(2) { 
     ["id"]=> 
     string(16) "1220976504656199" 
     ["name"]=> 
     string(17) "Yamili Ch Smendez" 
    } 
    [16]=> 
    array(2) { 
     ["id"]=> 
     string(16) "1231937083552234" 
     ["name"]=> 
     string(13) "Kennya Huerta" 
    } 
    [17]=> 
    array(2) { 
     ["id"]=> 
     string(16) "1055827734543856" 
     ["name"]=> 
     string(22) "Lizarazu Mario Esteban" 
    } 
    [18]=> 
    array(2) { 
     ["id"]=> 
     string(15) "692883830880037" 
     ["name"]=> 
     string(14) "Luian Suyos CA" 
    } 
    [19]=> 
    array(2) { 
     ["id"]=> 
     string(16) "1223005904451888" 
     ["name"]=> 
     string(12) "Rojas Andres" 
    } 
    [20]=> 
    array(2) { 
     ["id"]=> 
     string(16) "1330174903721200" 
     ["name"]=> 
     string(13) "Andrey Muñoz" 
    } 
    [21]=> 
    array(2) { 
     ["id"]=> 
     string(17) "10210287624562868" 
     ["name"]=> 
     string(27) "Carlos Manuel Veloz Sanchez" 
    } 
    [22]=> 
    array(2) { 
     ["id"]=> 
     string(16) "1454728524567426" 
     ["name"]=> 
     string(14) "Brandon Marín" 
    } 
    [23]=> 
    array(2) { 
     ["id"]=> 
     string(16) "1809382102646429" 
     ["name"]=> 
     string(27) "Dulce Maria Garcia Alvarado" 
    } 
    [24]=> 
    array(2) { 
     ["id"]=> 
     string(15) "244610952637604" 
     ["name"]=> 
     string(12) "Nafancy Soto" 
    } 
    } 
    ["paging"]=> 
    array(2) { 
    ["cursors"]=> 
    array(2) { 
     ["before"]=> 
     string(20) "OTYxNzE2MjkzOTMwNTA1" 
     ["after"]=> 
     string(20) "MjQ0NjEwOTUyNjM3NjA0" 
    } 
    ["next"]=> 
    string(296) "https://graph.facebook.com/v2.8/778837615590033/likes?access_token=EAAW6655F9B8BADxO2m3VFKuNs7mEifqLGhDWEBxUtvL9jF8x8ZATLWUl0ZBR6jephQjNpWuuZCbgosbZCZAaNJLxeJ9seTxBQPneYGWMvf2I89ZAt2jOdV3rJu7iemXniCG6LiWDdUyjBl43ocEaRXPTN5qxzlPgcZD&pretty=1&summary=total_count&limit=25&after=MjQ0NjEwOTUyNjM3NjA0" 
    } 
    ["summary"]=> 
    array(1) { 
    ["total_count"]=> 
    int(28215) 
    } 
} 

likes count 28215 

和我的代碼是

<?php 
session_start(); 
require_once __DIR__ .'/Facebook/autoload.php'; 

$fb = new \Facebook\Facebook([ 
'app_id' => '1612896019018783', 
'app_secret' =>'483a369576ee496cce5780b0d5e23202' , 
'default_graph_version' => 'v2.8', 
]); 

$permissions = ['user_photos']; 
$helper = $fb->getRedirectLoginHelper(); 
$accessToken=$helper->getAccessToken(); 

if(isset($accessToken)){ 

    $url = "https://graph.facebook.com/v2.8/778837615590033/likes?&summary=total_count&access_token={$accessToken}"; 
    //$url = "https://graph.facebook.com/v2.8/1205547979521647?fields=posts{likes,comments}&access_token={$accessToken}"; 
    //$url = "https://graph.facebook.com/v2.8/1205547979521647?me?fields=id,name&access_token={$accessToken}"; 
    $header=array("Content-type: application/json"); 

    $ch=curl_init(); 
    curl_setopt($ch,CURLOPT_HTTPHEADER,$headers); 
    curl_setopt($ch,CURLOPT_URL,$url); 
    curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1); 
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false); 
    curl_setopt($ch,CURLOPT_COOKIEJAR,'cookie.txt'); 
    curl_setopt($ch,CURLOPT_COOKIEFILE,'cookie.txt'); 
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
    curl_setopt($ch,CURLOPT_USERAGENT,"Mozilla/5.0 (Windows;U; 
    Windows NT 5.1; en-US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3"); 
    curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false); 

    $st=curl_exec($ch); 
    $result=json_decode($st,TRUE); 

    echo "<pre>"; 
    var_dump($result); 
    echo "</pre>"; 

    $likes = $result['summary']['total_count']; 
    echo "likes count ". $likes; 
    //echo "<center>"; 
    //echo "<img width='700' height='450' src= ><br><br>"; 
    //echo "<img width='90' height='90' src=http://i.imgur.com/wVAJS8T.png"; 
    //echo "</center>"; 



} 
else 
{ 
    $loginUrl = $helper->getLoginUrl('http://localhost:8080/fb/',$permissions); 
    echo '<a href="'.$loginUrl.'">Login with FB</a>'; 
} 


?> 

我需要以表格的形式顯示在results.php結果

回答

0

你的$result是數組格式。所以只需使用如下的表格標籤。

$result=json_decode($st,TRUE); 
    echo "<table>"; 
foreach ($result as $res) { 
    echo "<tr>"; 
    echo "<td>".$res['data']['id']."</td">; 
    echo "<td>".$res['data']['name']."</td>"; 
    echo "</tr">; 
} 
    echo "</table>" 
+0

反正是有使用PHP JSON結果添加到mysql數據庫? –

1
$data = $result['data']; 

echo '<table>'; 
echo '<tr><td>id</td><td>name</td></tr>'; 
foreach ($data as $item) { 
    echo '<tr>'; 
    echo '<td>' . $item['id'] . '</td>'; 
    echo '<td>' . $item['name'] . '</td>'; 
    echo '</tr>'; 

    addMysql($item['id'], $item['name']); 
} 
echo '</table>'; 

function addMysql($id, $name) { 
    $sql = "INSERT INTO table_name (`id`, `name`) VALUE ($id, $name);"; 
    // instance mysql driver 
    mysql_query($sql); 
} 
+0

謝謝..它的工作.. –

+0

我可以將這個值添加到MySQL數據庫? –

+0

我需要將id和名稱存儲到mysql數據庫 –