Facebook用户表fql到json_encode [英] Facebook user table fql to json_encode
问题描述
下面的代码是一个例子,但是我不支持代码。
用户表 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 yourforeach
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屋!