<?php 
include ("../db.php");

$url = 'http://graph.facebook.com/';

$pgid = array (0);
$pic = array (0);
$link = array (0);
$title = array (0);
$category = array (0);


$res = mysql_query ("SELECT Id, PageId FROM page WHERE Verified = 1 ");
$nr = mysql_num_rows ($res);
while ($nr > 0) {
	$row = mysql_fetch_array ($res);
	extract ($row);
	
	if ($PageId != NULL) {
		$pageUrl = "" . $url . $PageId . "?fields=picture,link,name,category";
		//echo $pageUrl;

		// create curl resource
		$ch = curl_init();

		// set url
		curl_setopt($ch, CURLOPT_URL, $pageUrl);
		curl_setopt($ch,CURLOPT_HTTPHEADER,array ("Content-Type: text/xml; charset=utf-8"));
		//return the transfer as a string
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

		// $output contains the output string
		$output = curl_exec($ch);
		//$output = utf8_encode($output);
		//echo $output . "<br />";

		$param = json_decode($output, true);

		if ($param['id']!=NULL) {
			array_push($pgid,$Id);
			array_push ($pic, $param['picture']);
			array_push ($link, $param['link']);
			array_push ($title, $param['name']);
			array_push ($category, str_replace('_', ' ', $param['category']));		
		}
		// close curl resource to free up system resources
		curl_close($ch);   
		
	}
	
	$nr--;	
}

for ($i=0; $i<sizeof($pic); $i++) {
	echo $pic[$i] . "<br />";
	$resUPD = mysql_query (sprintf("UPDATE page SET Image = '%s', Link = '%s', Category='%s', Title='%s' WHERE Id = $pgid[$i];",
	mysql_real_escape_string($pic[$i]),	
	mysql_real_escape_string($link[$i]),	
	mysql_real_escape_string($category[$i]),	
	mysql_real_escape_string($title[$i]))) or die ($pgid[$i] . "<br />" . $pic[$i] . "<br />" . $link[$i] . "<br />" . $title[$i] . "<br />" . $category[$i] . "<br /> error 2");	
}

?>