Home > Net >  json api call error, what am I doing wrong
json api call error, what am I doing wrong

Time:01-20

I made an api call successfully but am getting errors after indexing it

$json = file_get_contents('https://app.morenaija.ng/@api/search/YouTube/'.$search_term.'');
$yt_data = json_decode($json);

?>

below is the output response

{
    "items": [
        {
            "id": "CevxZvSJLk8",
            "title": "Katy Perry - Roar (Official)",
            "thumbDefault": "https://i.ytimg.com/vi/CevxZvSJLk8/hqdefault.jpg?sqp=-oaymwEYCKgBEF5IVfKriqkDCwgBFQAAiEIYAXAB&rs=AOn4CLDY5ZX7PoN7KNzXXEdQ_Yvch8qqQQ",
            "channelTitle": "Katy Perry",
            "channelId": "UCYvmuw-JtVrTZQ-7Y4kd63Q",
            "publishedAt": "6 years ago",
            "duration": "4:30",
            "viewCount": "3068657449"
        },
        {
            "id": "UW5SMrURVbM",
            "title": "Roar - Katy Perry (Lyrics) ??",
            "thumbDefault": "https://i.ytimg.com/vi/UW5SMrURVbM/hqdefault.jpg?sqp=-oaymwEYCKgBEF5IVfKriqkDCwgBFQAAiEIYAXAB&rs=AOn4CLBFmmujndykkc75SeCIl05VPHaglw",
            "channelTitle": "DopeLyrics",
            "channelId": "UCvR2R7j218tzejtTsb_X6Rw",
            "publishedAt": "1 year ago",
            "duration": "3:51",
            "viewCount": "14073787"
        },
        {
            "id": "FqkfBzRb43o",
            "title": "Katy Perry - Roar : Part 2 (Official Cover by 10 year-old Mariangeli from HitStreak)",
            "thumbDefault": "https://i.ytimg.com/vi/FqkfBzRb43o/hqdefault.jpg?sqp=-oaymwEYCKgBEF5IVfKriqkDCwgBFQAAiEIYAXAB&rs=AOn4CLBnXgcoefqzxawvmhB-QbCvMUBENg",
            "channelTitle": "ShowMobile",
            "channelId": "UCYBFx-THKRWqAxq66qZk_ww",
            "publishedAt": "6 years ago",
            "duration": "3:47",
            "viewCount": "307490768"
        },
        {
            "id": "Rh47oTsRf-w",
            "title": "Katy Perry - Roar (From “The Prismatic World Tour Live”)",
            "thumbDefault": "https://i.ytimg.com/vi/Rh47oTsRf-w/hqdefault.jpg?sqp=-oaymwEYCKgBEF5IVfKriqkDCwgBFQAAiEIYAXAB&rs=AOn4CLAsKxSyujUT_ZCQqsAdpVQAGnxP3Q",
            "channelTitle": "Katy Perry",
            "channelId": "UCYvmuw-JtVrTZQ-7Y4kd63Q",
            "publishedAt": "4 years ago",
            "duration": "4:33",
            "viewCount": "21381621"
        },
        {
            "id": "KRlhDkwJkHU",
            "title": "Katy Perry - Roar (OFFICIAL 1 Hour)",
            "thumbDefault": "https://i.ytimg.com/vi/KRlhDkwJkHU/hqdefault.jpg?sqp=-oaymwEYCKgBEF5IVfKriqkDCwgBFQAAiEIYAXAB&rs=AOn4CLCOvC3IMGpgbitf9nn58BxMFpqJnw",
            "channelTitle": "SONGS 1 HOUR",
            "channelId": "UCEo7MW5ArVEzQCQRpTuNGDg",
            "publishedAt": "4 years ago",
            "duration": "1:01:11",
            "viewCount": "27253"
        },
        {
            "id": "empcJOD-bA0",
            "title": "Roar by Katy Perry Lyrics",
            "thumbDefault": "https://i.ytimg.com/vi/empcJOD-bA0/hqdefault.jpg?sqp=-oaymwEYCKgBEF5IVfKriqkDCwgBFQAAiEIYAXAB&rs=AOn4CLBcizW9oc4KLGEo5o3Z2kOmKbKOkQ",
            "channelTitle": "mulero ibrahim",
            "channelId": "UCEl6sNUx9xHQ8zyypGJnlWQ",
            "publishedAt": "4 years ago",
            "duration": "3:45",
            "viewCount": "423025"
        },
        {
            "id": "K-w2sYAqMKw",
            "title": "Katy Perry - Roar (One Love Manchester)",
            "thumbDefault": "https://i.ytimg.com/vi/K-w2sYAqMKw/hqdefault.jpg?sqp=-oaymwEYCKgBEF5IVfKriqkDCwgBFQAAiEIYAXAB&rs=AOn4CLDjF35LoaRotGJ8WvhB4Nc9oZPvqQ",
            "channelTitle": "BBC Music",
            "channelId": "UCZtDUmC3W7j25XHZWFT_XgQ",
            "publishedAt": "2 years ago",
            "duration": "3:50",
            "viewCount": "12996443"
        },
        {
            "id": "sLZvdAdlQq4",
            "title": "Play Doh ROAR - Katy Perry Barbie Doll Inspired Costume",
            "thumbDefault": "https://i.ytimg.com/vi/sLZvdAdlQq4/hqdefault.jpg?sqp=-oaymwEYCKgBEF5IVfKriqkDCwgBFQAAiEIYAXAB&rs=AOn4CLA-i8iXAj-galqb2-0P1w82JdDXcQ",
            "channelTitle": "Fondanista",
            "channelId": "UCVNrpO7KiVMdwOdFZwLkWRA",
            "publishedAt": "4 years ago",
            "duration": "2:58",
            "viewCount": "14677198"
        },
        {
            "id": "0KSOMA3QBU0",
            "title": "Katy Perry - Dark Horse (Official) ft. Juicy J",
            "thumbDefault": "https://i.ytimg.com/vi/0KSOMA3QBU0/hqdefault.jpg?sqp=-oaymwEYCKgBEF5IVfKriqkDCwgBFQAAiEIYAXAB&rs=AOn4CLDa1ue7JhfY14YUSCsVhgf7o2kWyw",
            "channelTitle": "Katy Perry",
            "channelId": "UCYvmuw-JtVrTZQ-7Y4kd63Q",
            "publishedAt": "6 years ago",
            "duration": "3:45",
            "viewCount": "2773465353"
        },
        {
            "id": "Z0Q8j3HJagE",
            "title": "Katy Perry - Making of the \"Roar\" Music Video",
            "thumbDefault": "https://i.ytimg.com/vi/Z0Q8j3HJagE/hqdefault.jpg?sqp=-oaymwEYCKgBEF5IVfKriqkDCwgBFQAAiEIYAXAB&rs=AOn4CLAj6fVgZFc8eblIa5hC6CwvxasbNQ",
            "channelTitle": "Katy Perry",
            "channelId": "UCYvmuw-JtVrTZQ-7Y4kd63Q",
            "publishedAt": "6 years ago",
            "duration": "22:31",
            "viewCount": "20739183"
        },
        {
            "id": "EyLCXC3rV2M",
            "title": "**LYRICS** Katy Perry - Roar",
            "thumbDefault": "https://i.ytimg.com/vi/EyLCXC3rV2M/hqdefault.jpg?sqp=-oaymwEYCKgBEF5IVfKriqkDCwgBFQAAiEIYAXAB&rs=AOn4CLBGnc2_UQBdmVbEvfzHuZf6GVldlQ",
            "channelTitle": "ilpescepalla",
            "channelId": "UCTnd7uvo-uWwlB9qvh0aucA",
            "publishedAt": "6 years ago",
            "duration": "4:04",
            "viewCount": "2940062"
        },
        {
            "id": "pQQ24JwtRUI",
            "title": "Olivia Sings Roar | The Voice Kids Australia 2014",
            "thumbDefault": "https://i.ytimg.com/vi/pQQ24JwtRUI/hqdefault.jpg?sqp=-oaymwEYCKgBEF5IVfKriqkDCwgBFQAAiEIYAXAB&rs=AOn4CLCKE6eUp7Iye7_Wx8_0YOOPwpWamw",
            "channelTitle": "The Voice Kids Australia",
            "channelId": "UCUDR5r3ow3goI1iZgGlnoyw",
            "publishedAt": "5 years ago",
            "duration": "4:28",
            "viewCount": "95223791"
        },
        {
            "id": "aqhCJW890So",
            "title": "Mariangeli from HitStreak - ROAR : Part 1",
            "thumbDefault": "https://i.ytimg.com/vi/aqhCJW890So/hqdefault.jpg?sqp=-oaymwEYCKgBEF5IVfKriqkDCwgBFQAAiEIYAXAB&rs=AOn4CLCj4EwhFnnFVcjJtF_3hUBYQ83WZw",
            "channelTitle": "ShowMobile",
            "channelId": "UCYBFx-THKRWqAxq66qZk_ww",
            "publishedAt": "6 years ago",
            "duration": "0:33",
            "viewCount": "32701801"
        },
        {
            "id": "KlyXNRrsk4A",
            "title": "Katy Perry - Last Friday Night (T.G.I.F.)",
            "thumbDefault": "https://i.ytimg.com/vi/KlyXNRrsk4A/hqdefault.jpg?sqp=-oaymwEYCKgBEF5IVfKriqkDCwgBFQAAiEIYAXAB&rs=AOn4CLAR2QvImCYQhf_dP5WFJiWVKU00fw",
            "channelTitle": "Katy Perry",
            "channelId": "UCYvmuw-JtVrTZQ-7Y4kd63Q",
            "publishedAt": "8 years ago",
            "duration": "8:11",
            "viewCount": "1239885938"
        },
        {
            "id": "UUT2GTujJI0",
            "title": "Katy Perry - Roar Lyrics",
            "thumbDefault": "https://i.ytimg.com/vi/UUT2GTujJI0/hqdefault.jpg?sqp=-oaymwEYCKgBEF5IVfKriqkDCwgBFQAAiEIYAXAB&rs=AOn4CLDUKvQC8DQIg7dD39mEjDetWdHMsA",
            "channelTitle": "K- Hype",
            "channelId": "UCcoS9-5e2PPrO5gIqC9Fy4Q",
            "publishedAt": "6 years ago",
            "duration": "3:45",
            "viewCount": "90138"
        },
        {
            "id": "QGJuMBdaqIw",
            "title": "Katy Perry - Firework (Official)",
            "thumbDefault": "https://i.ytimg.com/vi/QGJuMBdaqIw/hqdefault.jpg?sqp=-oaymwEYCKgBEF5IVfKriqkDCwgBFQAAiEIYAXAB&rs=AOn4CLCVFJ6nMr1OyDp5P9wNafG76miRoA",
            "channelTitle": "Katy Perry",
            "channelId": "UCYvmuw-JtVrTZQ-7Y4kd63Q",
            "publishedAt": "9 years ago",
            "duration": "3:54",
            "viewCount": "1233775759"
        },
        {
            "id": "ZKmuyy6xEBU",
            "title": "Katy Perry - Firework, Roar & Never Really Over LIVE - SHEIN Together",
            "thumbDefault": "https://i.ytimg.com/vi/ZKmuyy6xEBU/hqdefault.jpg?sqp=-oaymwEYCKgBEF5IVfKriqkDCwgBFQAAiEIYAXAB&rs=AOn4CLAqsqPbUkw6GHnIB_my6JIuS9ondA",
            "channelTitle": "KPEZ",
            "channelId": "UCv1DBOB5sc6vrjYEkwNsHcw",
            "publishedAt": "1 day ago",
            "duration": "11:52",
            "viewCount": "2688"
        },
        {
            "id": "qvBzVWdwZFg",
            "title": "Katy Perry - Roar (Live @ BBC Radio 1's Big Weekend)",
            "thumbDefault": "https://i.ytimg.com/vi/qvBzVWdwZFg/hqdefault.jpg?sqp=-oaymwEYCKgBEF5IVfKriqkDCwgBFQAAiEIYAXAB&rs=AOn4CLDujpTCwyTAWK3t-MozoxcjvBhtjw",
            "channelTitle": "HeyHey Hey",
            "channelId": "UCMYxD0p4_-GxQ1qYYDsK-FQ",
            "publishedAt": "3 years ago",
            "duration": "4:38",
            "viewCount": "98831"
        },
        {
            "id": "Sl8Wl4EFBGQ",
            "title": "KATY PERRY WITH ZERO BUDGET! (Roar PARODY)",
            "thumbDefault": "https://i.ytimg.com/vi/Sl8Wl4EFBGQ/hqdefault.jpg?sqp=-oaymwEYCKgBEF5IVfKriqkDCwgBFQAAiEIYAXAB&rs=AOn4CLCybyCTPmI82feN8Rzcmu_0ERrVpA",
            "channelTitle": "LankyBox",
            "channelId": "UCSf0s2ogUVYpJPuzW1zpAOg",
            "publishedAt": "6 months ago",
            "duration": "1:24",
            "viewCount": "2759481"
        }
    ]
}

below is how I get the things I want for this call

<?php echo $yt_data->items[0]->title; ?>
<?php echo $yt_data->items[0]->channelTitle; ?>
e.t.c

error_log [20-Jan-2023 09:34:50 UTC] PHP Notice: Undefined index: title in /home/patch/aheadv.com/search_main.php on line 11 PHP Notice: Undefined variable: yt_data in /home/patch/aheadv.com/search_main.php on line 81 PHP Notice: Trying to get property 'items' of non-object in /home/patch/aheadv.com/search_main.php on line 81 PHP Notice: Trying to get property 'title' of non-object in /home/patch/aheadv.com/search_main.php on line 81 PHP Notice: Undefined variable: yt_data in /home/patch/aheadv.com/search_main.php on line 82 e.tc

Ho do I solve the error issues

<?php
require_once('admin/config.php');
require_once('includes/url_slug.php'); 
if(isset($_GET['change'])){ header("Location: ".$site_url."/".cano($_GET['q'])); }
$search_term = urlencode($_GET['q']);
$save_search = cano($_GET['q']);
$search_title = str_replace("Audio", " ", $save_search);
$search_title = ucwords($search_title);
$yt_id = $_GET["q"];
$url = 'https://app.morenaija.ng/@api/search/YouTube/'.$search_term;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$json = curl_exec($ch);
curl_close($ch);

if ($json === false) {
    echo "Error: ".curl_error($ch);
} else {
    $yt_data = json_decode($json);
    if (json_last_error() !== JSON_ERROR_NONE) {
        echo "Error: ".json_last_error_msg();
    } else {
        if(isset($yt_data->items[0]->title)){
            $first_item_title = $yt_data->items[0]->title;
            echo $first_item_title;
        }
        if(isset($yt_data->items[0]->channelId)){
            $first_item_channelId = $yt_data->items[0]->channelId;
            echo $first_item_channelId;
        }
    }
}
?>

    <title><?php echo $yt_data->items[0]->title; ?> Mp4 mp3 Download - <?php echo $site_title;?></title>

CodePudding user response:

$search_term = 'your search term here';
$url = 'https://app.morenaija.ng/@api/search/YouTube/'.$search_term;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$json = curl_exec($ch);
curl_close($ch);

if ($json === false) {
    // there was an error with the cURL request
    // you can check curl_error() for more information
} else {
    $yt_data = json_decode($json);
    // do something with the data
}

This code uses the curl_init(), curl_setopt(), curl_exec(), and curl_close() functions to make the GET request and retrieve the response. The CURLOPT_RETURNTRANSFER option is set to true so that the response is returned as a string instead of being printed to the screen. The curl_exec() function returns false on failure, so it's important to check for this and handle it appropriately, you can use the curl_error() function to get more information about the error.

In addition to that, it's a good practice to add some error handling mechanism for the json_decode() function as well, to make sure that the json data is valid and it can be successfully decoded.

$yt_data = json_decode($json, true);
if (json_last_error() !== JSON_ERROR_NONE) {
    // there was an error decoding the JSON
    // you can check json_last_error_msg() for more information
} else {
    // do something with the data
}

This way you are making sure that the json data is valid and can be successfully decoded.

Once the JSON data is decoded, you can access its properties using array notation or object notation, depending on how you passed the second parameter to the json_decode() function.

Here's an example of how to access properties of the decoded JSON data using array notation:

$yt_data = json_decode($json, true);

// access the 'title' property of the first item in the 'items' array
$first_item_title = $yt_data['items'][0]['title'];

// access the 'channelId' property of the first item in the 'items' array
$first_item_channelId = $yt_data['items'][0]['channelId'];

And here's an example of how to access properties of the decoded JSON data using object notation:

$yt_data = json_decode($json);

// access the 'title' property of the first item in the 'items' array
$first_item_title = $yt_data->items[0]->title;

// access the 'channelId' property of the first item in the 'items' array
$first_item_channelId = $yt_data->items[0]->channelId;

You can access nested properties of the JSON data by chaining together the property names in the same way.

It's worth noting that when you use json_decode($json, true) it will return an associative array, and when you use json_decode($json) it will return an object.

Also, if the key you are trying to access doesn't exist in the json data, the code will throw an error, so it's important to check whether the key exists in the json data before trying to access it.

Complete Code:(EDITED)

<?php

require_once('admin/config.php');
require_once('includes/url_slug.php'); 
if(isset($_GET['change'])){ header("Location: ".$site_url."/".cano($_GET['q'])); }
$search_term = urlencode($_GET['q']);
$save_search = cano($_GET['q']);
$search_title = str_replace("Audio", " ", $save_search);
$search_title = ucwords($search_title);
$yt_id = $_GET["q"];
$url = 'https://app.morenaija.ng/@api/search/YouTube/'.$search_term;

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$json = curl_exec($ch);
curl_close($ch);

if ($json === false) {
    echo "Error: ".curl_error($ch);
} else {
    $yt_data = json_decode($json);
    if (json_last_error() !== JSON_ERROR_NONE) {
        echo "Error: ".json_last_error_msg();
    } else {
        if(isset($yt_data->items[0]->title) && $yt_data->items[0]->title !== null){
            $first_item_title = $yt_data->items[0]->title;
            echo $first_item_title;
        }
        if(isset($yt_data->items[0]->channelId) && $yt_data->items[0]->channelId !== null){
            $first_item_channelId = $yt_data->items[0]->channelId;
            echo $first_item_channelId;
        }
    }
}
?>

<title><?php if(isset($first_item_title) && $first_item_title !== null) echo $first_item_title; else echo ""; ?> Mp4 mp3 Download - <?php echo $site_title;?></title>
  • Related