Unserializing & inserting into database ?

Discussion in 'PHP & Perl' started by TayaX, Feb 22, 2015.

  1. TayaX

    TayaX Jr. VIP Jr. VIP

    Joined:
    Dec 13, 2010
    Messages:
    3,646
    Likes Received:
    2,030
    Occupation:
    Skype : TayaxBHW
    Location:
    France
    Home Page:
     
  2. tiyowan

    tiyowan Regular Member

    Joined:
    Aug 7, 2013
    Messages:
    251
    Likes Received:
    138
    That's JSON, mate. Use PHP's json_decode function.
     
  3. TayaX

    TayaX Jr. VIP Jr. VIP

    Joined:
    Dec 13, 2010
    Messages:
    3,646
    Likes Received:
    2,030
    Occupation:
    Skype : TayaxBHW
    Location:
    France
    Home Page:
    I get an error too when i'm using

    Code:
    $affiche = utf8_encode($affiche);
    json_decode($affiche) or die('error');
     
  4. tiyowan

    tiyowan Regular Member

    Joined:
    Aug 7, 2013
    Messages:
    251
    Likes Received:
    138
    Post the error.
     
  5. TayaX

    TayaX Jr. VIP Jr. VIP

    Joined:
    Dec 13, 2010
    Messages:
    3,646
    Likes Received:
    2,030
    Occupation:
    Skype : TayaxBHW
    Location:
    France
    Home Page:
    Code:
    $affiche = utf8_encode($affiche);
    $affiche = json_decode($affiche);
    var_dump(json_last_error());
    gets me

    Code:
    [h=2]int(4)[/h]
     
  6. bartosimpsonio

    bartosimpsonio Jr. VIP Jr. VIP Premium Member

    Joined:
    Mar 21, 2013
    Messages:
    13,953
    Likes Received:
    12,484
    Occupation:
    MACHIN LURNIN
    Location:
    TUVALU
    Home Page:
    That's not json. It's some serialization format that looks like jason, like yaml or something.
     
    • Thanks Thanks x 1
  7. TayaX

    TayaX Jr. VIP Jr. VIP

    Joined:
    Dec 13, 2010
    Messages:
    3,646
    Likes Received:
    2,030
    Occupation:
    Skype : TayaxBHW
    Location:
    France
    Home Page:
    How come I can echo it, yet not able to just insert it in a database ?
     
  8. TayaX

    TayaX Jr. VIP Jr. VIP

    Joined:
    Dec 13, 2010
    Messages:
    3,646
    Likes Received:
    2,030
    Occupation:
    Skype : TayaxBHW
    Location:
    France
    Home Page:
    Also, when I

    Code:
    [COLOR=#333333][FONT=Consolas]print_r($movie->getArray());[/FONT][/COLOR]
    I get

    Array (
    Code:
     => 205450 [movieType] => Array ( [code] => 4002 [$] => Long-m�trage ) [originalTitle] => Fifty Shades of Grey [title] => Cinquante Nuances de Grey [productionYear] => 2015 [nationality] => Array ( [0] => Array ( [code] => 5002 [$] => U.S.A. ) ) [genre] => Array ( [0] => Array ( [code] => 13010 [$] => Erotique ) [1] => Array ( [code] => 13008 [$] => Drame ) [2] => Array ( [code] => 13024 [$] => Romance ) ) [release] => Array ( [releaseDate] => 2015-02-11 ) [runtime] => 7500 [synopsisShort] => L'histoire d'une romance passionnelle, et sexuelle, entre un jeune homme riche amateur de femmes, et une �tudiante vierge de 22 ans. [castingShort] => Array ( [directors] => Sam Taylor-Johnson [actors] => Jamie Dornan, Dakota Johnson, Jennifer Ehle, Eloise Mumford, Victor Rasuk ) [movieCertificate] => Array ( [certificate] => Array ( [code] => 14001 [$] => Interdit aux moins de 12 ans ) ) [poster] => AlloImage Object ( [imageIcon:AlloImage:private] => [imageBorder:AlloImage:private] => [imageSize:AlloImage:private] => [imageHost:AlloImage:private] => fr.web.img1.acsta.net [imagePath:AlloImage:private] => pictures/14/11/14/11/08/371396.jpg ) [trailer] => Array ( [name] => Bande-Annonce Cinquante Nuances de Grey [code] => 19549475 [href] => http://www.allocine.fr/blogvision/19549475 ) [dvdReleaseDate] => 2015-06-16 [bluRayReleaseDate] => 2015-06-16 [link] => Array ( [0] => Array ( [rel] => aco:web [name] => Cinquante Nuances de Grey [href] => http://www.allocine.fr/film/fichefilm_gen_cfilm=205450.html ) [1] => Array ( [rel] => aco:web_casting [name] => Casting Cinquante Nuances de Grey [href] => http://www.allocine.fr/film/fichefilm-205450/casting/ ) [2] => Array ( [rel] => aco:web_trivia [name] => Anecdotes Cinquante Nuances de Grey [href] => http://www.allocine.fr/film/fichefilm-205450/secrets-tournage/ ) [3] => Array ( [rel] => aco:web_showtimes [name] => S�ances Cinquante Nuances de Grey [href] => http://www.allocine.fr/seance/film-205450/ ) [4] => Array ( [rel] => aco:web_publicreviews [name] => Critiques Spectateurs Cinquante Nuances de Grey [href] => http://www.allocine.fr/film/fichefilm-205450/critiques/spectateurs/ ) [5] => Array ( [rel] => aco:web_pressreviews [name] => Critiques presse Cinquante Nuances de Grey [href] => http://www.allocine.fr/film/fichefilm-205450/critiques/presse/ ) [6] => Array ( [rel] => aco:web_shots [name] => Photos Cinquante Nuances de Grey [href] => http://www.allocine.fr/film/fichefilm-205450/photos/ ) [7] => Array ( [rel] => aco:web_trailers [name] => Bandes annonces Cinquante Nuances de Grey [href] => http://www.allocine.fr/videos/fichefilm-205450/toutes/ ) [8] => Array ( [rel] => aco:web_products [name] => Cinquante Nuances de Grey [href] => http://www.allocine.fr/film/fichefilm-205450/vod-dvd/ ) ) ) [/B]
     
  9. tiyowan

    tiyowan Regular Member

    Joined:
    Aug 7, 2013
    Messages:
    251
    Likes Received:
    138
    My bad, bartosimpsonio is right, it's not JSON. Ok, extract the values you
    need from the object and then save them in the database.

    EDIT: It is YAML. Use this: https://code.google.com/p/php-yaml/ and the
    yaml_parse function. You have to install the PHP extension though.
     
    • Thanks Thanks x 1
    Last edited: Feb 22, 2015
  10. TayaX

    TayaX Jr. VIP Jr. VIP

    Joined:
    Dec 13, 2010
    Messages:
    3,646
    Likes Received:
    2,030
    Occupation:
    Skype : TayaxBHW
    Location:
    France
    Home Page:
    PHP is not my thing honestly. But Im trying my best.

    Thanks for the answers. Now instead if installing YAML on my server, how can I extract those infos from the array ?

    the
    Code:
    var_dump($movie->poster); 
    gives me

    Code:
    [h=2][SIZE=2]object(AlloImage)#306 (5) { ["imageIcon":"AlloImage":private]=> bool(false) ["imageBorder":"AlloImage":private]=> bool(false) ["imageSize":"AlloImage":private]=> bool(false) ["imageHost":"AlloImage":private]=> string(21) "fr.web.img1.acsta.net" ["imagePath":"AlloImage":private]=> string(34) "pictures/14/11/14/11/08/371396.jpg" }[/SIZE][/h]
    So i guess I have to get the Image host + imagepath right ?

    How do i pass those parameters for a echo or var_dump ?
     
  11. tiyowan

    tiyowan Regular Member

    Joined:
    Aug 7, 2013
    Messages:
    251
    Likes Received:
    138
    Don't try to write your own parser. :) If you can't install the YAML extension
    on your server, no problem.

    Use Spyc: https://github.com/mustangostang/spyc

    PHP:
    require_once "spyc.php";
    $data Spyc::YAMLLoad($movie);
     
    Last edited: Feb 22, 2015
  12. Tuccinator

    Tuccinator Newbie

    Joined:
    Jul 4, 2013
    Messages:
    40
    Likes Received:
    3
    People are giving you wrong advice. It isn't JSON or YAML, it is the default serialization provided with PHP. The error you are receiving upon 'unserializing' is a problem with storing it in the database, which makes it corrupt.
     
  13. TayaX

    TayaX Jr. VIP Jr. VIP

    Joined:
    Dec 13, 2010
    Messages:
    3,646
    Likes Received:
    2,030
    Occupation:
    Skype : TayaxBHW
    Location:
    France
    Home Page:
     
  14. TayaX

    TayaX Jr. VIP Jr. VIP

    Joined:
    Dec 13, 2010
    Messages:
    3,646
    Likes Received:
    2,030
    Occupation:
    Skype : TayaxBHW
    Location:
    France
    Home Page:

    I didn't store anything in the database, I'm trying to unserialize it directly after the API call.
     
  15. tiyowan

    tiyowan Regular Member

    Joined:
    Aug 7, 2013
    Messages:
    251
    Likes Received:
    138
    What's the output of var_dump($dataa);?
     
  16. TayaX

    TayaX Jr. VIP Jr. VIP

    Joined:
    Dec 13, 2010
    Messages:
    3,646
    Likes Received:
    2,030
    Occupation:
    Skype : TayaxBHW
    Location:
    France
    Home Page:
    It stops there
    $dataa = Spyc::YAMLLoad($movie) or die ('1');
     
  17. tiyowan

    tiyowan Regular Member

    Joined:
    Aug 7, 2013
    Messages:
    251
    Likes Received:
    138
    Ok remove Spyc. Do var_dump($movies), paste in pastebin.com and reply with a link.
     
  18. TayaX

    TayaX Jr. VIP Jr. VIP

    Joined:
    Dec 13, 2010
    Messages:
    3,646
    Likes Received:
    2,030
    Occupation:
    Skype : TayaxBHW
    Location:
    France
    Home Page:
  19. bartosimpsonio

    bartosimpsonio Jr. VIP Jr. VIP Premium Member

    Joined:
    Mar 21, 2013
    Messages:
    13,953
    Likes Received:
    12,484
    Occupation:
    MACHIN LURNIN
    Location:
    TUVALU
    Home Page:
    That's just a prettified PHP data structure.

    You can probably just use $movies as a PHP array which contains that data.
     
  20. hamanchi

    hamanchi Newbie

    Joined:
    Nov 14, 2012
    Messages:
    22
    Likes Received:
    1
    Hi

    Make some change in code and everything working fine.
    i made search movie name "XMEN" and get movie code 111543 and pass to get movie function and got result, i believe that when you passing movie code into get movie detail function it is char not int use type casting.
    I upload code on my testing server i like to post URL here as you know i am not a donor so i can't post URL here if you want help please let me know.
    Cheers