I want to do web scraping with PHP. There is a json data in the URL, I want to pull this data and save it to the postgreSQL database. This is the code:
<?php $ ch = curl_init(); $ url = "https://kurumsal.sokmarket.com.tr/ajax/servis/sehirler"; 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); } //your database connection here $ host = "localhost"; $ user = "postgres"; $ password = "****"; $ dbname = "sok"; // Create connection try{ $ this->linkid = @pg_connect("host=$ this->host port=5432 dbname=$ this->dbname user=$ this->user password=$ this->password"); if (! $ this->linkid) throw new Exception("Could not connect to PostgreSQL server."); } catch (Exception $ e) { die($ e->getMessage()); } foreach ($ array_data as $ row) { $ sql = "INSERT INTO il_adi (il) VALUES (decoded)"; $ conn->query($ sql); } $ conn->close(); ?>
I can view the data I have captured in the array in the terminal. How can I save this to the database?