function getProducts() { $conn = new mysqli($servername, $username, $password, $dbname); $sql = "SELECT * FROM products"; $result = $conn->query($sql); $products = array(); if ($result->num_rows > 0) { while($row = $result->fetch_assoc()) { $product = array( "id" => $row["id"], "name" => $row["name"], "price" => $row["price"] ); array_push($products, $product); } } $conn->close(); return $products; }
function getProducts() { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, "https://api.example.com/products"); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $response = curl_exec($curl); curl_close($curl); return json_decode($response, true); }This example shows how to retrieve products from a REST API and return them as an array of associative arrays, where each associative array represents a single product. This code uses the curl package to send a GET request to the API and receive the response as a JSON string. The json_decode function is used to convert the JSON string into a PHP array.