@ErnieAlex
I seem to have figured it out but the goal of the task is to make it callable via cli
When i input php index.php
in my command line i get this error
test@Test-MacBook-Pro api-call % php index.php
Connection failed: SQLSTATE[HY000] [2002] No such file or directoryPHP Fatal error: Uncaught Error: Using $this when not in object context in /Users/test/.bitnami/stackman/machines/xampp/volumes/root/htdocs/api-call/index.php:37
Stack trace:
#0 {main}
thrown in /Users/test/.bitnami/stackman/machines/xampp/volumes/root/htdocs/api-call/index.php on line 37
Fatal error: Uncaught Error: Using $this when not in object context in /Users/test/.bitnami/stackman/machines/xampp/volumes/root/htdocs/api-call/index.php:37
Stack trace:
#0 {main}
thrown in /Users/test/.bitnami/stackman/machines/xampp/volumes/root/htdocs/api-call/index.php on line 37
How do i achieve this with my current code?
<?php
$servername = "localhost";
$username = "root";
$password = "root";
try {
$conn = new PDO("mysql:host=$servername;dbname=api-call", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully";
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
$ch = curl_init();
$url = "http://example.com/api/properties";
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$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
)
';
$stmt = $conn->prepare($query);
//Clean Data
$county = htmlspecialchars(strip_tags($county));
//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();
}
- I have this in my api
"current_page": 1,
"data": [
{
"uuid": "d8d9c545-3832-3d85-a25a-aac181479be7",
"property_type_id": "4",
"county": "Oklahoma",
"country": "Palm Front",
"town": "Jigawa",
"description": "Lorem Ipsum",
"address": "1 Skitiam Old Apt. 212",
"property_type": {
"id": 4,
"title": "Terraced",
"description": "Lorem Ipsum",
}
}
]
}
How do i insert the property_type:description into my database?