1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

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,519
    Likes Received:
    1,960
    Occupation:
    Skype : TayaxBHW
    Location:
    France
    Home Page:
    Hi guys,

    I'm trying to use an API ( https://github.com/etienne-gauvin/api-allocine-helper ) in order to get informations concerning movies.

    Code:
    [COLOR=#000000]require_once [/COLOR][COLOR=#800000]"allocine/api-allocine-helper.php"[/COLOR][COLOR=#000000];[/COLOR][COLOR=#000000]
    
            $helper [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#00008B]new[/COLOR][COLOR=#2B91AF]AlloHelper[/COLOR][COLOR=#000000];[/COLOR][COLOR=#000000]
            $code [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800000]'20054'[/COLOR][COLOR=#000000];[/COLOR][COLOR=#000000]
            $profile [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800000]'small'[/COLOR][COLOR=#000000];[/COLOR][COLOR=#000000]
            $affiche[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800000]''[/COLOR][COLOR=#000000];[/COLOR][COLOR=#000000]
            [/COLOR][COLOR=#00008B]try[/COLOR][COLOR=#000000]
    [/COLOR][COLOR=#000000]{[/COLOR][COLOR=#000000]
    
        $movie [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] $helper[/COLOR][COLOR=#000000]->[/COLOR][COLOR=#000000]movie[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000] $code[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] $profile [/COLOR][COLOR=#000000]);[/COLOR][COLOR=#000000]
        $affiche [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] $movie[/COLOR][COLOR=#000000]->[/COLOR][COLOR=#000000]poster[/COLOR][COLOR=#000000];[/COLOR][COLOR=#000000]
    
    
    [/COLOR][COLOR=#000000]}[/COLOR][COLOR=#000000]
    [/COLOR][COLOR=#00008B]catch[/COLOR][COLOR=#000000]([/COLOR][COLOR=#2B91AF]ErrorException[/COLOR][COLOR=#000000] $error [/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]
    [/COLOR][COLOR=#000000]{[/COLOR][COLOR=#000000]
        echo [/COLOR][COLOR=#800000]"Erreur n°"[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] $error[/COLOR][COLOR=#000000]->[/COLOR][COLOR=#000000]getCode[/COLOR][COLOR=#000000](),[/COLOR][COLOR=#800000]": "[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] $error[/COLOR][COLOR=#000000]->[/COLOR][COLOR=#000000]getMessage[/COLOR][COLOR=#000000](),[/COLOR][COLOR=#000000] PHP_EOL[/COLOR][COLOR=#000000];[/COLOR][COLOR=#000000]
    [/COLOR][COLOR=#000000]}[/COLOR][COLOR=#000000]
    $my_post [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] array[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]
              [/COLOR][COLOR=#800000]'post_title'[/COLOR][COLOR=#000000]=>[/COLOR][COLOR=#800000]'example'[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000]           
              [/COLOR][COLOR=#800000]'post_status'[/COLOR][COLOR=#000000]=>[/COLOR][COLOR=#800000]'publish'[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000]
              [/COLOR][COLOR=#800000]'post_author'[/COLOR][COLOR=#000000]=>[/COLOR][COLOR=#800000]1[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000]
              [/COLOR][COLOR=#800000]'post_content'[/COLOR][COLOR=#000000]=>[/COLOR][COLOR=#800000]''[/COLOR][COLOR=#000000]
            [/COLOR][COLOR=#000000]);[/COLOR][COLOR=#000000]
            $post_id [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] wp_insert_post[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000] $my_post [/COLOR][COLOR=#000000]);[/COLOR][COLOR=#000000]
    
    youtube[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800000]'test'[/COLOR][COLOR=#000000];[/COLOR][COLOR=#000000]
    add_post_meta[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]$post_id[/COLOR][COLOR=#000000],[/COLOR][COLOR=#800000]'youtube'[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] $youtube[/COLOR][COLOR=#000000],[/COLOR][COLOR=#00008B]true[/COLOR][COLOR=#000000]);[/COLOR][COLOR=#000000]
    add_post_meta[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]$post_id[/COLOR][COLOR=#000000],[/COLOR][COLOR=#800000]'affiche'[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] $affiche[/COLOR][COLOR=#000000],[/COLOR][COLOR=#00008B]true[/COLOR][COLOR=#000000]);[/COLOR][COLOR=#000000]
    echo $affiche[/COLOR][COLOR=#000000];[/COLOR]

    The result is, in the database I have a serialized object while the echo show me the good value (the address to the movie poster)

    I have no idea on how to 'unserialize' this data.

    Code:
    [LEFT][COLOR=#222222][FONT=Helvetica Neue]O:9:"AlloImage":5:{s:20:"AlloImageimageIcon";b:0;s:22:"AlloImageimageBorder";b:0;s:20:"AlloImageimageSize";b:0;s:20:"AlloImageimageHost";s:21:"fr.web.img1.acsta.net";s:20:"AlloImageimagePath";s:34:"pictures/14/11/14/11/08/371396.jpg";}[/FONT][/COLOR][/LEFT]
    
    When i try to manually unserialize this data online, I get errors.

    Any help ?
     
  2. tiyowan

    tiyowan Regular Member

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

    TayaX Jr. VIP Jr. VIP

    Joined:
    Dec 13, 2010
    Messages:
    3,519
    Likes Received:
    1,960
    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:
    137
    Post the error.
     
  5. TayaX

    TayaX Jr. VIP Jr. VIP

    Joined:
    Dec 13, 2010
    Messages:
    3,519
    Likes Received:
    1,960
    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:
    12,468
    Likes Received:
    11,170
    Occupation:
    CHEAP
    Location:
    DATASETS
    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,519
    Likes Received:
    1,960
    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,519
    Likes Received:
    1,960
    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:
    137
    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,519
    Likes Received:
    1,960
    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:
    137
    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,519
    Likes Received:
    1,960
    Occupation:
    Skype : TayaxBHW
    Location:
    France
    Home Page:
    It is not working either.

    Code:
                  $helper = new AlloHelper;
                $code = $_GET['allocine'];
                $profile = 'small';
                $affiche='';
                try
        {
            // Envoi de la requête
            $movie = $helper->movie( $code, $profile );
    
            // Afficher le titre
            $affiche = $movie->poster;
            
             require_once "spyc-master/spyc.php";
            $dataa = Spyc::YAMLLoad($movie); 
            var_dump($dataa);
    
            // Afficher toutes les données
        }
        catch( ErrorException $error )
        {
            // En cas d'erreur
            echo "Erreur n°", $error->getCode(), ": ", $error->getMessage(), PHP_EOL;
        }    
    
     
  14. TayaX

    TayaX Jr. VIP Jr. VIP

    Joined:
    Dec 13, 2010
    Messages:
    3,519
    Likes Received:
    1,960
    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:
    137
    What's the output of var_dump($dataa);?
     
  16. TayaX

    TayaX Jr. VIP Jr. VIP

    Joined:
    Dec 13, 2010
    Messages:
    3,519
    Likes Received:
    1,960
    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:
    137
    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,519
    Likes Received:
    1,960
    Occupation:
    Skype : TayaxBHW
    Location:
    France
    Home Page:
  19. bartosimpsonio

    bartosimpsonio Jr. VIP Jr. VIP Premium Member

    Joined:
    Mar 21, 2013
    Messages:
    12,468
    Likes Received:
    11,170
    Occupation:
    CHEAP
    Location:
    DATASETS
    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