Facebook阵列进入mysql db

时间:2014-01-26 13:59:17

标签: php mysql arrays facebook

我正在尝试根据总喜欢,分享等为每个网址构建一些数据,以便我可以列出最喜欢和共享等十大列表。

代码不是我的四十,但这是我已经解决但似乎得到了数组但我似乎无法理解我如何捕获数组片段并将它们转换为一个集合表来保存数据。

$fql  = "SELECT url, normalized_url, share_count, like_count, comment_count, ";
$fql .= "total_count, commentsbox_count, comments_fbid, click_count FROM ";
$fql .= "link_stat WHERE url = 'http://www.google.com'";

$apifql="https://api.facebook.com/method/fql.query?format=json&query=".urlencode($fql);
$json=file_get_contents($apifql);

$id = 311;

if(is_array($json)){
foreach($json as $key => $value){

$url = mysql_real_escape_string( $value['url'] );
$share_count = mysql_real_escape_string( $value['share_count'] );
$like_count = mysql_real_escape_string( $value['like_count'] );
$comment_count = mysql_real_escape_string( $value['comment_count'] )

$sql = "UPDATE `fb_counts` SET `url` = $url, `share_count` = $share_count, `like_count` = $like_count, `comment_count` = $comment_count WHERE id = $id" ;
mysql_query($sql) or exit(mysql_error()); 
  }
}

用下面替换了该行,它发现了错误....

$json= json_decode(file_get_contents($apifql));

警告:json_decode()期望参数1为字符串,数组在第17行的C:\ xampp \ htdocs \ test \ FBCount.php中给出

致命错误:无法在第47行的C:\ xampp \ htdocs \ test \ FBCount.php中使用stdClass类型的对象作为数组

我缩短了所要求的信息以使其更简洁,现在阵列更短,但仍然让我感到头疼。阵列确实伤到了我的小脑袋......哈哈

数组([0] => stdClass对象([url] => http://www.munchkins.com/test-a-url-to-see-fb-result [share_count] => 56 [like_count] => 62 [comment_count] => 46))

好的开始变得更有意义......设法让阵列看起来像这样....                                                                                            数组([url] => http://www.google.com [share_count] => 5268340 [like_count] => 1342562 [comment_count] => 1716901)

我无法理解,我花了好几个小时试图阅读它是如何工作的,我想我必须要么太厚或者便士很快就会掉落......是如何得到钥匙=&gt ;数据进入db。

foreach ($fbdata2 as $key => $value) {

$url = mysql_real_escape_string($value[url]);    
$query = "UPDATE videos.fb_data SET 'url' = '$url' WHERE id='$id'";
mysql_query($query) or die( mysql_errno());

我刚刚选择了数组中的第一个键,看看它是否有效,但我得到了它的工作,我已经尝试了各种格式,没有运气,我需要将数据输入数据库。

非常感谢你的帮助 - 我是一个老式计时器,所以事情对我来说并不容易让我头晕目眩......

2 个答案:

答案 0 :(得分:0)

使用json_decode将json转换为PHP valiable。

$json= json_decode(file_get_contents($apifql));

您还可以查看工作代码的这个片段:

<?php
$fql  = "SELECT url, normalized_url, share_count, like_count, comment_count, ";
$fql .= "total_count, commentsbox_count, comments_fbid, click_count FROM ";
$fql .= "link_stat WHERE url = 'http://www.google.com'";
$apifql="https://api.facebook.com/method/fql.query?format=json&query=".urlencode($fql);
$json= json_decode(file_get_contents($apifql), true);
foreach ( $json[0] as $key => $val ) {
   echo $key.' = '.$val."<br/>";
}
?>

答案 1 :(得分:0)

我使用 true 插件来帮助清理数组

$json= json_decode(file_get_contents($apifql),true);

非常感谢您的帮助仍然需要一些大脑的抓挠,但这并不是一件坏事,因为它有助于我更好地理解事物。年纪越来越大,我的大脑不会在韧带上运行