【问题标题】:Get facebook education into $_SESSION将 Facebook 教育纳入 $_SESSION
【发布时间】:2015-03-21 06:35:15
【问题描述】:
<?php
session_start();

require 'config.php';
require 'facebook.php';

$facebook = new Facebook(array(
        'appId'     =>  $appID,
        'secret'    => $appSecret,
        ));
$user = $facebook->getUser();

if($user){

    try{
        $user_profile = $facebook->api('/me');
        $params = array('next' => 'website' );
        $logout =$facebook->getLogoutUrl($params);
        $_SESSION['User']=$user_profile;
        $college = null;
        foreach ($user_profile['education'] as $education) { 
            if ($education['type'] == "College") { //store the name data
                $college = $education;
                break;
            }
        }

        if(empty($college)) {
    echo "College information was not found!";
} else {
    // var_dump($college);
    echo "name" . json_encode($college);
}

?>

返回页面

name{"school":{"id":"1234567890","name":"Universiti Tunku Abdul Rahman"},"type":"College"}

我从 Facebook sdk 获取大学数据,我只想要学校名称并存储到 $_SESSION 我该怎么做?是一个数组,我不知道如何将数据存储到 $_SESSION。

【问题讨论】:

  • Hej,查看 php.net 网站。它非常酷,你应该在尝试编写 PHP 脚本之前阅读一些内容。
  • 先学几年PHP的基础知识!如果您在不了解超级基础知识的情况下尝试构建高级事物,那么您只会遇到巨大的问题。

标签: php facebook-php-sdk


【解决方案1】:

使用json_decode读取数组

$myArray = json_decode($data, true);
echo $myArray[0]['id']; // Fetches the first ID
echo $myArray[0]['name']; // Fetches the first name

【讨论】:

  • $myArray = json_decode($college, true);回声 $myArray[0]['id']; // 获取第一个 ID echo $myArray[0]['name']; // 获取名字
  • 它返回 json_decode() 期望参数 1 是字符串,数组在
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-28
  • 1970-01-01
  • 2015-01-28
  • 2019-04-29
相关资源
最近更新 更多