Facebook用户表fql到json_encode [英] Facebook user table fql to json_encode

查看:105
本文介绍了Facebook用户表fql到json_encode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用来自fql的用户表。我将数组传递给json_encode()。 Facebook json格式不是corrct,它排除[]。如何在我的代码中添加这个。



下面的代码是一个例子,但是我不支持代码。



用户表 https://developers.facebook.com/docs/reference/fql/用户



我想要名字,uid。

  //获取相册封面
$ fql2 =从照片中选择src,其中pid ='。 $ values ['cover_pid']。 ;
$ param2 = array(
'method'=>'fql.query',
'query'=> $ fql2,
'callback'=>''
);

$ fqlResult2 = $ facebook-> api($ param2);
$ jsarr = array();
foreach($ fqlResult2 as $ keys2 => $ values2){
}
if($ values ['name']!='Profile Pictures'){
$ jsarr ['src'] = $ album ['src'];

$ count + = 1;

if($ count == 1){
$ outputStr。=[;}
else {
$ outputStr。=,;}
$ outputStr。= json_encode($ values2);
}
}
$ outputStr。=];
$ outputStr = str_replace({,[,$ outputStr);
$ outputStr = str_replace(},],$ outputStr);
echo $ outputStr;
}
?>


解决方案

当您使用Facebook PHP SDK时,它会自动解码将返回的json对象放入PHP数组中。如果您需要一个json对象,则在完成处理之后,您必须使用 json_encode



您的PHP代码有一些主要问题。只需要开始:




  • 您的 foreach 循环一开始就结束

  • 您在哪里定义变量 $ values $ album

  • 您正在引用 $ values2 您的 foreach 循环。

  • 您正在附加到一行未初始化的变量 $ outputStr。=[ ;

  • 如果您通过一个适当的php数据数组,通过 json_encode()一个完整的json对象。您不需要添加任何种类的括号。


I am using user table from fql.I am passing array to json_encode(). Facebook json format is not corrct, it is excluding []. How do I add this in my code.

Below code is a example but I do not under stand that code.

user table https://developers.facebook.com/docs/reference/fql/user

I want name,uid.

//to get album cover
    $fql2    =   "select  src from photo where pid = '" . $values['cover_pid'] . "'";
    $param2  =   array(
     'method'    => 'fql.query',
     'query'     => $fql2,
     'callback'  => ''
    );

    $fqlResult2   =   $facebook->api($param2);
    $jsarr  =   array();
    foreach( $fqlResult2 as $keys2 => $values2){
    }
    if ($values['name'] != 'Profile Pictures'){
        $jsarr['src'] = $album['src'];

            $count += 1;

    if ($count == 1){
    $outputStr .= "[";}
    else {
    $outputStr .= ",";}
    $outputStr .= json_encode($values2);
    }
}
$outputStr .= "]";
$outputStr = str_replace("{","[",$outputStr);
$outputStr = str_replace("}","]",$outputStr);
echo $outputStr;
}
?>

解决方案

When you use the Facebook PHP SDK, it automatically decodes the returned json object into a PHP array. If you need a json object from it, you'll have to use json_encode on it after you finish processing it.

You've got some major problems with your php code. Just to get started:

  • Your foreach loop ends as soon as it starts with the } on the next line.
  • Where do you define the variables $values and $album?
  • You are referencing $values2 outside of your foreach loop.
  • You are appending to an uninitialized variable on your line $outputStr .= "[";.
  • If you pass json_encode() a proper php array of data, it will make a complete json object. You shouldn't need to be appending brackets of any kind.

这篇关于Facebook用户表fql到json_encode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆