I have an API that has 418 pages with data, i am trying to get all the content in the API and put them in my MySQL database.
Currently i can only put the first page content inside the database. How do i solve this?
Here is my Code.
data.php
<?php
include_once './config/Database.php';
$url = "http://exampledomain.com/api/properties";
$ch = curl_init($url);
$headers = array(
'Content-type: application/json',
'API_KEY: 2S7rhsaq9X1cnfkMCPHX64YsWYyfe1he',
);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$resp = curl_exec($ch);
if($e = curl_error($ch)){
echo $e;
}
else {
$decoded = json_decode($resp,true);
// print_r($decoded);
$query =
"INSERT
INTO
details
(
county,
country,
town,
description,
displayable_address,
image,
thumbnail,
latitude,
longitude,
bedrooms,
bathrooms,
price,
-- property_type,
type
)
VALUES
(
:county,
:country,
:town,
:description,
:address,
:image_full,
:image_thumbnail,
:latitude,
:longitude,
:num_bedrooms,
:num_bathrooms,
:price,
-- :property_type.description,
:type
)
";
//Prepare Statement
$stmt = $conn->prepare($query);
//Bind data
$stmt->bindParam(':county', $county);
$stmt->bindParam(':country', $country);
$stmt->bindParam(':town', $town);
$stmt->bindParam(':description', $description);
$stmt->bindParam(':address', $displayable_address);
$stmt->bindParam(':image_full', $image);
$stmt->bindParam(':image_thumbnail', $thumbnail);
$stmt->bindParam(':latitude', $latitude);
$stmt->bindParam(':longitude', $longitude);
$stmt->bindParam(':num_bedrooms', $bedrooms);
$stmt->bindParam(':num_bathrooms', $bathrooms);
$stmt->bindParam(':price', $price);
// $stmt->bindParam(':property_type.description', $property_type);
$stmt->bindParam(':type', $type);
foreach($decoded["data"] as $decode){
$county = $decode['county'];
$country = $decode['country'];
$town = $decode['town'];
$description = $decode['description'];
$displayable_address = $decode['address'];
$image = $decode['image_full'];
$thumbnail = $decode['image_thumbnail'];
$latitude = $decode['latitude'];
$longitude = $decode['longitude'];
$bedrooms = $decode['num_bedrooms'];
$bathrooms = $decode['num_bathrooms'];
$price = $decode['price'];
// $property_type = $decode['property_type']['description'];
$type = $decode['type'];
$stmt->execute();
}
}
curl_close($ch);
Database.php
<?php
$servername = "localhost";
$username = "henry";
$password = "love2020";
$dbname = "api-call";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Database Connected successfully and Record Addedd";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
The above code is only inserting the contents on the first page to the database, i want to insert all the contents from page 1 to 418 to the database.
This is the API parameter
http://exampledomain.com/api/properties?page[number]=418