Bonjour,
Moi c'est Ludovic j'ai 28 ans.
Débutant en PHP, j'essaie de m'intéresser à la manipulation d'API.
J'essaie d'afficher les données d'une API mais ... je n'y arrive pas (évidemment)
Je viens évidemment sur le forum pour avoir de l'aide (évidemment avant j'ai essayé de parcourir les sites pour essayer de trouver des infos pour me dépatouiller).
J'ai construit mon code de la façon suivante :
J'ai mis un script cURL en début de fichier (script qui fonctionne puisque j'ai bien les datas brutes de l'API qui s'affichent avec l'echo du $response. Mon problème c'est pour les afficher)
Voici les données affichées par le $response
Mon script curl en entête
Ensuite j'ai fait la construction de ma page (avec le squelette HTML classique) et dans le body j'ai fait le test sur le genre_ids avec un script pour essayer d'afficher des données
Donc j'ai des messages d'erreur disant
Warning: Attempt to read property "results" on int in [l'adresse de mon site] online 38
Warning: Attempt to read property "genre_ids" on null in [l'adresse de mon site] online 38
Warning: Attempt to read property "results" on array in [l'adresse de mon site] online 38
J'ai dû coder comme un goret (ne sachant pas comment faire, j'ai tâtonné à droite à gauche pour essayer d'obtenir des infos et j'ai dû me perdre)
Merci pour l'attention et l'aide que vous me donnerez
Ludo
Moi c'est Ludovic j'ai 28 ans.
Débutant en PHP, j'essaie de m'intéresser à la manipulation d'API.
J'essaie d'afficher les données d'une API mais ... je n'y arrive pas (évidemment)
Je viens évidemment sur le forum pour avoir de l'aide (évidemment avant j'ai essayé de parcourir les sites pour essayer de trouver des infos pour me dépatouiller).
J'ai construit mon code de la façon suivante :
J'ai mis un script cURL en début de fichier (script qui fonctionne puisque j'ai bien les datas brutes de l'API qui s'affichent avec l'echo du $response. Mon problème c'est pour les afficher)
Voici les données affichées par le $response
{
"page": 1,
"results": [
{
"adult": false,
"backdrop_path": "/yrdAamkeqXHm0UYukk8xgoCvc7G.jpg",
"genre_ids": [
12,
28,
878
],
"id": 11,
"original_language": "en",
"original_title": "Star Wars",
"overview": "Princess Leia is captured and held hostage by the evil Imperial forces in their effort to take over the galactic Empire. Venturesome Luke Skywalker and dashing captain Han Solo team together with the loveable robot duo R2-D2 and C-3PO to rescue the beautiful princess and restore peace and justice in the Empire.",
"popularity": 73.589,
"poster_path": "/6FfCtAuVAW8XJjZ7eWeLibRLWTw.jpg",
"release_date": "1977-05-25",
"title": "Star Wars",
"video": false,
"vote_average": 8.2,
"vote_count": 17698
},
{
"adult": false,
"backdrop_path": "/vV5knD9jlW8QaOhCgf4129hbIIh.jpg",
"genre_ids": [
10751,
16,
35,
878
],
"id": 980804,
"original_language": "en",
"original_title": "LEGO Star Wars Summer Vacation",
"overview": "Looking for a much-needed break, Finn arranges a surprise vacation for his friends Rey, Poe, Rose, Chewie, BB-8, R2-D2, and C-3PO, aboard the luxurious Halcyon. However, Finn's plan to have one last hurrah together quickly goes awry.",
"popularity": 139.165,
"poster_path": "/2SatEFCs04oFRqkZuY1fODYXeFI.jpg",
"release_date": "2022-08-05",
"title": "LEGO Star Wars Summer Vacation",
"video": false,
"vote_average": 5.8,
"vote_count": 30
},
{
"adult": false,
"backdrop_path": "/8BTsTfln4jlQrLXUBquXJ0ASQy9.jpg",
"genre_ids": [
12,
28,
878,
14
],
"id": 140607,
"original_language": "en",
"original_title": "Star Wars: The Force Awakens",
"overview": "Thirty years after defeating the Galactic Empire, Han Solo and his allies face a new threat from the evil Kylo Ren and his army of Stormtroopers.",
"popularity": 65.197,
"poster_path": "/wqnLdwVXoBjKibFRR5U3y0aDUhs.jpg",
"release_date": "2015-12-15",
"title": "Star Wars: The Force Awakens",
"video": false,
"vote_average": 7.3,
"vote_count": 17334
},
{
"adult": false,
"backdrop_path": "/5Iw7zQTHVRBOYpA0V6z0yypOPZh.jpg",
"genre_ids": [
12,
28,
878
],
"id": 181808,
"original_language": "en",
"original_title": "Star Wars: The Last Jedi",
"overview": "Rey develops her newly discovered abilities with the guidance of Luke Skywalker, who is unsettled by the strength of her powers. Meanwhile, the Resistance prepares to do battle with the First Order.",
"popularity": 60.349,
"poster_path": "/ySaaKHOLAQU5HoZqWmzDIj1VvZ1.jpg",
"release_date": "2017-12-13",
"title": "Star Wars: The Last Jedi",
"video": false,
"vote_average": 6.9,
"vote_count": 13337
},
{
"adult": false,
"backdrop_path": "/f53Jujiap580mgfefID0T0g2e17.jpg",
"genre_ids": [
16,
10751,
878,
35
],
"id": 857702,
"original_language": "en",
"original_title": "LEGO Star Wars Terrifying Tales",
"overview": "Poe Dameron and BB-8 must face the greedy crime boss Graballa the Hutt, who has purchased Darth Vader’s castle and is renovating it into the galaxy’s first all-inclusive Sith-inspired luxury hotel.",
"popularity": 67.226,
"poster_path": "/fYiaBZDjyXjvlY6EDZMAxIhBO1I.jpg",
"release_date": "2021-10-01",
"title": "LEGO Star Wars Terrifying Tales",
"video": false,
"vote_average": 6.8,
"vote_count": 197
},
{
"adult": false,
"backdrop_path": "/SPkEiZGxq5aHWQ2Zw7AITwSEo2.jpg",
"genre_ids": [
12,
28,
878
],
"id": 181812,
"original_language": "en",
"original_title": "Star Wars: The Rise of Skywalker",
"overview": "The surviving Resistance faces the First Order once again as the journey of Rey, Finn and Poe Dameron continues. With the power and knowledge of generations behind them, the final battle begins.",
"popularity": 58.345,
"poster_path": "/db32LaOibwEliAmSL2jjDF6oDdj.jpg",
"release_date": "2019-12-18",
"title": "Star Wars: The Rise of Skywalker",
"video": false,
"vote_average": 6.4,
"vote_count": 8067
},
{
"adult": false,
"backdrop_path": "/ojHCeDULAkQK25700fhRU75Tur2.jpg",
"genre_ids": [
878,
12,
28
],
"id": 348350,
"original_language": "en",
"original_title": "Solo: A Star Wars Story",
"overview": "Through a series of daring escapades deep within a dark and dangerous criminal underworld, Han Solo meets his mighty future copilot Chewbacca and encounters the notorious gambler Lando Calrissian.",
"popularity": 38.081,
"poster_path": "/3IGbjc5ZC5yxim5W0sFING2kdcz.jpg",
"release_date": "2018-05-15",
"title": "Solo: A Star Wars Story",
"video": false,
"vote_average": 6.6,
"vote_count": 7278
},
{
"adult": false,
"backdrop_path": "/6t8ES1d12OzWyCGxBeDYLHoaDrT.jpg",
"genre_ids": [
28,
12,
878
],
"id": 330459,
"original_language": "en",
"original_title": "Rogue One: A Star Wars Story",
"overview": "A rogue band of resistance fighters unite for a mission to steal the Death Star plans and bring a new hope to the galaxy.",
"popularity": 44.208,
"poster_path": "/i0yw1mFbB7sNGHCs7EXZPzFkdA1.jpg",
"release_date": "2016-12-14",
"title": "Rogue One: A Star Wars Story",
"video": false,
"vote_average": 7.5,
"vote_count": 13446
},
{
"adult": false,
"backdrop_path": "/xEdnGUc8KeZ7h0eEgSN3ERhjgL0.jpg",
"genre_ids": [
16,
28,
878,
12
],
"id": 12180,
"original_language": "en",
"original_title": "Star Wars: The Clone Wars",
"overview": "Set between Episode II and III, The Clone Wars is the first computer animated Star Wars film. Anakin and Obi Wan must find out who kidnapped Jabba the Hutt's son and return him safely. The Seperatists will try anything to stop them and ruin any chance of a diplomatic agreement between the Hutts and the Republic.",
"popularity": 27.506,
"poster_path": "/ywRtBu88SLAkNxD0GFib6qsFkBK.jpg",
"release_date": "2008-08-05",
"title": "Star Wars: The Clone Wars",
"video": false,
"vote_average": 6.1,
"vote_count": 1641
},
{
"adult": false,
"backdrop_path": "/5fu7fzy4NZTsL1Jap00UBIInAuB.jpg",
"genre_ids": [
12,
28,
878
],
"id": 1893,
"original_language": "en",
"original_title": "Star Wars: Episode I - The Phantom Menace",
"overview": "Anakin Skywalker, a young slave strong with the Force, is discovered on Tatooine. Meanwhile, the evil Sith have returned, enacting their plot for revenge against the Jedi.",
"popularity": 34.236,
"poster_path": "/6wkfovpn7Eq8dYNKaG5PY3q2oq6.jpg",
"release_date": "1999-05-19",
"title": "Star Wars: Episode I - The Phantom Menace",
"video": false,
"vote_average": 6.5,
"vote_count": 12531
},
{
"adult": false,
"backdrop_path": "/2UEKQtkx35hGCkgfA5lPVu7GaAm.jpg",
"genre_ids": [
16,
878,
12
],
"id": 782054,
"original_language": "ja",
"original_title": "Doraemon: Nobita's Little Star Wars 2021",
"overview": "One day during summer vacation, a palm-sized alien named Papi appears from a small rocket that Nobita picks up. He is the president of Pirika, a small planet in outer space, and has come to Earth to escape the rebels. Doraemon and his friends are puzzled by Papi’s small size, but as they play together using the secret tool “Small Light”, they gradually become friends. However, a whale-shaped space battleship comes to earth and attacks Doraemon, Nobita and the others in order to capture Papi. Feeling responsible for getting everyone involved, Papi tries to stand up to the rebels. Doraemon and his friends leave for the planet Pirika to protect their dear friend and his home.",
"popularity": 18.804,
"poster_path": "/48gKZioIDeUOI0afbYv3kh9u9RQ.jpg",
"release_date": "2022-03-04",
"title": "Doraemon: Nobita's Little Star Wars 2021",
"video": false,
"vote_average": 5,
"vote_count": 3
},
{
"adult": false,
"backdrop_path": "/1Lhc32P0a62BgMFgd20wXR1osR3.jpg",
"genre_ids": [
16,
10751,
12,
35,
878
],
"id": 732670,
"original_language": "en",
"original_title": "LEGO Star Wars Holiday Special",
"overview": "As her friends prep for a Life Day holiday celebration, Rey journeys with BB-8 on a quest to gain a deeper knowledge of the Force at a mysterious Jedi Temple. There, she embarks on a cross-timeline adventure through beloved moments in Star Wars history, coming into contact with iconic heroes and villains from all eras of the saga. But will she make it back in time for the Life Day feast?",
"popularity": 20.728,
"poster_path": "/zyzJSI7UZZzz5Toj10rYGF5689z.jpg",
"release_date": "2020-11-17",
"title": "LEGO Star Wars Holiday Special",
"video": false,
"vote_average": 6.7,
"vote_count": 248
},
{
"adult": false,
"backdrop_path": "/rkKx3O4e1tNDDpS3eERWZHumSMm.jpg",
"genre_ids": [
12,
28,
878
],
"id": 1895,
"original_language": "en",
"original_title": "Star Wars: Episode III - Revenge of the Sith",
"overview": "The evil Darth Sidious enacts his final plan for unlimited power -- and the heroic Jedi Anakin Skywalker must choose a side.",
"popularity": 36.372,
"poster_path": "/xfSAoBEm9MNBjmlNcDYLvLSMlnq.jpg",
"release_date": "2005-05-17",
"title": "Star Wars: Episode III - Revenge of the Sith",
"video": false,
"vote_average": 7.4,
"vote_count": 11766
},
{
"adult": false,
"backdrop_path": "/pXnNSeyTCUebjpHTiZt7v6FZId0.jpg",
"genre_ids": [
12,
28,
878
],
"id": 1894,
"original_language": "en",
"original_title": "Star Wars: Episode II - Attack of the Clones",
"overview": "Following an assassination attempt on Senator Padmé Amidala, Jedi Knights Anakin Skywalker and Obi-Wan Kenobi investigate a mysterious plot that could change the galaxy forever.",
"popularity": 31.458,
"poster_path": "/oZNPzxqM2s5DyVWab09NTQScDQt.jpg",
"release_date": "2002-05-15",
"title": "Star Wars: Episode II - Attack of the Clones",
"video": false,
"vote_average": 6.5,
"vote_count": 11346
},
{
"adult": false,
"backdrop_path": "/uNjBnOmdjZoiWTLQ938YJZ1cYVU.jpg",
"genre_ids": [
16,
35,
10751,
878,
12,
10770
],
"id": 392216,
"original_language": "en",
"original_title": "Phineas and Ferb: Star Wars",
"overview": "Phineas and Ferb become the galaxy's unlikeliest last hope when they must return the Death Star plans to the Rebel Alliance.",
"popularity": 11.903,
"poster_path": "/xomphpz7MIasqVluPX83TjoTL8G.jpg",
"release_date": "2014-07-26",
"title": "Phineas and Ferb: Star Wars",
"video": false,
"vote_average": 7.1,
"vote_count": 155
},
{
"adult": false,
"backdrop_path": "/ae9xlnkS2qb5Dy9Mtlu68AWh42O.jpg",
"genre_ids": [
12,
35,
10751,
878
],
"id": 74849,
"original_language": "en",
"original_title": "The Star Wars Holiday Special",
"overview": "Luke Skywalker and Han Solo battle evil Imperial forces to help Chewbacca reach his imperiled family on the Wookiee planet - in time for Life Day, their most important day of the year!",
"popularity": 14.539,
"poster_path": "/1TY4OAkcHRovlHDxSLW7UDJlild.jpg",
"release_date": "1978-12-01",
"title": "The Star Wars Holiday Special",
"video": false,
"vote_average": 3.3,
"vote_count": 370
},
{
"adult": false,
"backdrop_path": null,
"genre_ids": [
35,
878,
16
],
"id": 51888,
"original_language": "en",
"original_title": "Robot Chicken: Star Wars Episode III",
"overview": "Robot Chicken: Star Wars Episode III, directed by Chris McKay, combines the satirical sensibilities of Green and Matthew Senreich's Robot Chicken with characters of the Star Wars universe.",
"popularity": 13.208,
"poster_path": "/mi2lVho2zpfwcxI6yC1QYJi435D.jpg",
"release_date": "2010-12-19",
"title": "Robot Chicken: Star Wars Episode III",
"video": false,
"vote_average": 7.4,
"vote_count": 119
},
{
"adult": false,
"backdrop_path": null,
"genre_ids": [
16,
35,
878
],
"id": 42979,
"original_language": "en",
"original_title": "Robot Chicken: Star Wars",
"overview": "A series of 30 sketches, following the hilarious antics of various characters from a galaxy, far, far away.",
"popularity": 7.401,
"poster_path": "/h44WN4mVJ6wEpJgLaaNoFjv0NAo.jpg",
"release_date": "2007-07-17",
"title": "Robot Chicken: Star Wars",
"video": false,
"vote_average": 7.2,
"vote_count": 206
},
{
"adult": false,
"backdrop_path": "/c8XIFuJCPTtTJdSRpqnUoMr6eK1.jpg",
"genre_ids": [
878
],
"id": 667574,
"original_language": "en",
"original_title": "Battle Star Wars",
"overview": "When the leader of the evil Coalition threatens to destroy a Rebel planet for its resources, his daughter will have no choice but to join the Rebel side and fight for what is right.",
"popularity": 6.476,
"poster_path": "/ocA0ECiFoB4d1HITyEDQlLk7x84.jpg",
"release_date": "2020-01-28",
"title": "Battle Star Wars",
"video": false,
"vote_average": 5.1,
"vote_count": 33
},
{
"adult": false,
"backdrop_path": null,
"genre_ids": [
99
],
"id": 378386,
"original_language": "en",
"original_title": "Star Wars: Greatest Moments",
"overview": "Alex Zane counts down the top 20 Star Wars moments as voted by the public. Includes contributions from famous fans as well as the stars and crew of the intergalactic saga.",
"popularity": 6.181,
"poster_path": "/zIffPwISrW48qSmvAXEV27lBTMA.jpg",
"release_date": "2015-12-26",
"title": "Star Wars: Greatest Moments",
"video": false,
"vote_average": 6.6,
"vote_count": 27
}
],
"total_pages": 7,
"total_results": 139
}
Mon script curl en entête
<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://url de mon api/",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response."<br />";
}
?>
Ensuite j'ai fait la construction de ma page (avec le squelette HTML classique) et dans le body j'ai fait le test sur le genre_ids avec un script pour essayer d'afficher des données
<?php
$resultat = json_decode($response,true);
foreach($resultat as $film) {
$id_genre = $film->results->genre_ids;
echo $id_genre;
}
?>
Donc j'ai des messages d'erreur disant
Warning: Attempt to read property "results" on int in [l'adresse de mon site] online 38
Warning: Attempt to read property "genre_ids" on null in [l'adresse de mon site] online 38
Warning: Attempt to read property "results" on array in [l'adresse de mon site] online 38
J'ai dû coder comme un goret (ne sachant pas comment faire, j'ai tâtonné à droite à gauche pour essayer d'obtenir des infos et j'ai dû me perdre)
Merci pour l'attention et l'aide que vous me donnerez
Ludo