I am getting Undefined index error. I saw a similar type of question in the same site. I am unable to fix this error can anyone help me
<?php
if (isset($_GET['query']) && $_GET['query'] != '') {
$url = 'https://contextualwebsearch-websearch-v1.p.rapidapi.com/api/Search/NewsSearchAPI';
$query_fields = ['autoCorrect' => 'true',
'pageNumber' => 1,
'pageSize' => 10,
'safeSearch' => 'false',
'q' => $_GET['query']
];
$curl = curl_init($url . '?' . http_build_query($query_fields));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, [
'X-RapidAPI-Host: contextualwebsearch-websearch-v1.p.rapidapi.com',
'X-RapidAPI-Key: 7xxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
]);
$response = json_decode(curl_exec($curl), true);
curl_close($curl);
$news = $response['value']; //Error get in this line
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>News Searcher</title>
</head>
<body>
<form action="new2.php" method="GET">
<label for="query">Enter your query string:</label>
<input id="query" type="text" name="query" />
<br />
<button type="submit" name="submit">Search</button>
</form>
<br />
<?php
if (!empty($news)) {
echo '<b>News by Your query:</b>';
foreach ($news as $post) {
echo '<h3>' . $post['title'] . '</h3>';
echo '<a href="' . $post['url'] . '">Source</a>';
echo '<p>Date Published: ' . $post['datePublished'] . '</p>';
echo '<p>' . $post['body'] .'</p>';
echo '<hr>';
}
}
?>
</body>
</html>