• May
  • 16
  • 2012

How to get other/external website’s content and store it into your database?

Manier times we come across requirement like fetching/getting content from other website and store that content in our database. So don’t worry now. The code is ready for you. 🙂

Fetch external website’s content and store it into your database:

First of all, make sure that the external site is providing API to fetch/get content from their site. Once you have got the API, Just go through different parameters they are providing for fetching/getting content from the external site. In this way, you will need to make a URL in which you can pass the parameters as per your requirement. We assume that you are ready with the URL(API) of the external website from which you want to fetch/get content and store it into your database.

API is providing content in two formats:
2) XML.
Here we are progressing with output in JSON.

<?php /*=============DATABASE CONNECTION=================*/

{echo "success";}
$url = YOUR API URL;
$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec ($ch);

curl_close ($ch);

Now $results is our resultant array. You can check the results by echoing the $results.You need to run the loop for getting array content and fire the INSERT query for inserting content into your database. You can also display the results on your web page.

mysqli_query(“INSERT into `TABLE_NAME` (`FIELD_NAME`) VALUES (‘VALUE’)”);

Here is the example for Fetching/Getting reviews from citysearch.com.

/*==============REVIEWS FETCHING ====================*/

$flag=0; $url="http://api.citygridmedia.com/content/reviews/v2/search/where?where=92504&what=WHAT_YOU_WANT_TO_SEARCH &days=REVIEWS_DAYS&publisher=PUBLISHER_NAME&rpp=NO_OF_REVIEWS_PER_PAGE&page=".$page."
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec ($ch);
curl_close ($ch);




for($i=0; $i<COUNT($data);$i++)
$review_site='http://www.citysearch.com';mysqli_query("INSERT into `TABLE_NAME` (`FIELD_NAME`) VALUES ('VALUE')");


Ravi Makhija

Ravi Makhija

A writer, an Entrepreneur. Curious about the internet of everything. Interested in the cutting edge landscape of mobile apps and SAAS products. Blogs for Guru Technolabs - A Mobile App Development Company.

Let's Discuss a Project

Please share your unique idea or project requirement with us, our business concern person will get back to you with further details.