Search code examples
facebook-graph-api-v2.0fosfacebookbundle

Insert facebook profile picture into blob column mysql


I'm trying to integrate facebook connect into our website (Symfony) for this we use FOSFacebookBundle.

I was able to do that and getting my facebook id, first_name, and last_name and insert them into a new user in our fos_user column.

Regarding the user profile picture I keep receive an exception :

An exception occurred while executing 'INSERT INTO fos_user (username, username_canonical, email, email_canonical, enabled, salt, password, last_login, locked, expired, expires_at, confirmation_token, password_requested_at, roles, credentials_expired, credentials_expire_at, nom, prenom, solde, path, sexe, date_Naissance, facebookId, photo) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' with params ["1223800560964254", "1223800560964254", "******@gmail.com", "*********@gmail.com", 1, "", "", null, 0, 0, null, null, null, "a:1:{i:0;s:12:\"ROLE_PORTEUR\";}", 0, null, "Mohamed", "Elloumi", null, null, null, null, "1223800560964254", {"data":{"is_silhouette":false,"url":"https:\/\/fbcdn-profile-a.akamaihd.net\/hprofile-ak-ash2\/v\/t1.0-1\/c85.25.315.315\/s50x50\/580232_622196307791352_1230572455_n.jpg?oh=17c02ae42bb18d3ddbe3cfdf04239049&oe=57AC3250&__gda__=1474408706_227fd87692ead8189d332a4898571e62"}}]:

Notice: Array to string conversion

And I think that it's because I'm trying to add the full picture api Graph response into the blob column:

{
  "picture": {
    "data": {
      "is_silhouette": false,
      "url": "https://fbcdn-profile-a.akamaihd.net/****"
    }
  },
  "id": "792374454106869"
}

I guess that I only need the insert this line into my blob column :

"url": "https://fbcdn-profile-a.akamaihd.net/****"

I'm using this php function to insert into the fos_user table :

public function setFBData($fbdata) // C'est dans cette méthode que vous ajouterez vos informations
    {
        if (isset($fbdata['id'])) {
            $this->setFacebookId($fbdata['id']);

            $this->addRole('ROLE_PORTEUR');
        }
        if (isset($fbdata['first_name'])) {
            $this->setNom($fbdata['first_name']);
        }
        if (isset($fbdata['last_name'])) {
            $this->setPrenom($fbdata['last_name']);
        }
        if (isset($fbdata['email'])) {
            $this->setEmail($fbdata['email']);
        }
        if (isset($fbdata['picture'])) {
            $this->setPhoto($fbdata['picture']);
        }
    }

Solution

  • why a blob? A varchar(255) should suffice for a url. You should be able to access the url in the picture array like this:

    $fbdata['picture']['data']['url']