• May
  • 16
  • 2012

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:
1) JSON
2) XML.
Here we are progressing with output in JSON.

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

$con=mysqli_connect("localhost","my_user","my_password","my_db"); 
if(mysqli_select_db($con,"test")) 
{echo "success";}
else{die();}
$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);
$results=json_decode($data);

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 ====================*/
<?php

$page=1;
do{
$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."
&format=json";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec ($ch);
curl_close ($ch);

$results=json_decode($data);
$last_hit=$results->results->last_hit;

if($last_hit%50==0)
{
$flag=1;
$page++;
}

$data='';
$data=$results->results->reviews;

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

 

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.

Popular Blogs

Among the many digital tools that are being used with increasing frequency, the one that has the greatest...

Having a Best Website like opening a door and inviting…

In this internet era, the business website is the most…

The Live chat is the common thing now days, if…

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.