jeudi 28 juillet 2016

Traffic API code is not updating city, province and country into my database, sometimes it does and sometimes its not using PHP

I am using this code to update traffic from developer here traffic api. sometimes it stores the traffic well with city, province and country. and sometimes it stores traffic but not stores city province and country. city, province and country is automatically updated via latitude and longitude that are coming from traffic api.

Here is the code view

<?php
include("config.php");
$str = file_get_contents('http://ift.tt/2az9vEK');
$data = json_decode($str);
//for($i=0;$i<count($data);$i++)
//{
//  $data->TRAFFICITEMS[0];
//}
//print_r($data->TRAFFICITEMS);
foreach($data->TRAFFICITEMS as $key=>$value)
{
    //echo $key;




    for($i=0;$i<count($value);$i++)
    {
    //print_r($value[$i]);
    $trafficItemId = $value[$i]->ORIGINALTRAFFICITEMID;        

    $eventType=$value[$i]->TRAFFICITEMTYPEDESC;


    $user_tz = 'GMT';

    $schedule_starttime = new DateTime(date('Y-m-d H:i:s',strtotime($value[$i]->STARTTIME)), new DateTimeZone($user_tz) );
    $schedule_starttime->setTimeZone(new DateTimeZone('Africa/Johannesburg'));
    $STARTTIME =  $schedule_starttime->format('Y-m-d H:i:s');

    $schedule_endtime = new DateTime(date('Y-m-d H:i:s',strtotime($value[$i]->ENDTIME)), new DateTimeZone($user_tz) );
    $schedule_endtime->setTimeZone(new DateTimeZone('Africa/Johannesburg'));
    $ENDTIME =  $schedule_endtime->format('Y-m-d H:i:s');


    $schedule_entrytime = new DateTime(date('Y-m-d H:i:s',strtotime($value[$i]->STARTTIME)), new DateTimeZone($user_tz) );
    $schedule_entrytime->setTimeZone(new DateTimeZone('Africa/Johannesburg'));
    $ENTRYTIME =  $schedule_entrytime->format('Y-m-d H:i:s');


    $todaydate = new DateTime(date('Y-m-d H:i:s'), new DateTimeZone($user_tz));
    $todaydate->setTimeZone(new DateTimeZone('Africa/Johannesburg'));
    $currentdate = $todaydate->format('Y-m-d H:i:s');


$eventDescription=$value[$i]->TRAFFICITEMDESCRIPTION[1]->content;
//echo $des.'<br>';
//print_r($value[$i]->LOCATION);
foreach($value[$i]->LOCATION as $key1=>$value1)
{
    //echo $key1.'<br>';
    if($key1=='DEFINED')
    {
        //$eventType=$value1->ORIGIN->ROADWAY->DESCRIPTION[0]->TYPE;
     // echo $type.'<br>';
         // echo $lon=$value1->ORIGIN->LONGITUDE.'<br>';
        $address =  $value1->ORIGIN->ROADWAY->DESCRIPTION[0]->content;
        //$city = $value1->ORIGIN->DIRECTION->DESCRIPTION[0]->content;

    }

    if($key1=='GEOLOC')
    {
      $latitude=$value1->ORIGIN->LATITUDE;
      $longitude=$value1->ORIGIN->LONGITUDE;


$geolocation = $latitude.','.$longitude;



$province='';

$request = 'http://ift.tt/1kDofho'.$geolocation.'&sensor=false&key=AIzaSyDk8Z2vIiQHljqoBCEFhuRMX-cZahhDWnE'; 
$file_contents = file_get_contents($request);
$json_decode = json_decode($file_contents);

if(isset($json_decode->results[0])) {
    $response = array();
    foreach($json_decode->results[0]->address_components as $addressComponet) {


        $response[] = $addressComponet->long_name; 

        if(in_array('locality', $addressComponet->types)) {
                $city = $addressComponet->long_name; 
        }

        if(in_array('administrative_area_level_1', $addressComponet->types)) {
                $province = $addressComponet->long_name; 
        }        

        if(in_array('country', $addressComponet->types)) {
                $country = $addressComponet->long_name; 
        }

    }


  }
echo '<br>';

    }
    if($key1=='INTERSECTION')
    {


    }

}

$UnixIncidentTime=strtotime($ENTRYTIME); 
$UnixExpireTime=strtotime($ENDTIME);

$rows = mysql_num_rows(mysql_query("select * from IncidentReportnew where OriginalIncidentID = '".$trafficItemId."'"));
         if($rows <= 0){
            $resultset = mysql_query("select statusCode from `StatusCode` where `Here` = '".$eventType."' ");
            $fetchStatusCode = mysql_fetch_assoc($resultset);

        $query = mysql_query("insert into IncidentReportnew(`eventname`, `eventType` ,`OriginalIncidentID`, `device_id`,`eventDescription`,`longitude`,`latitude`,`address`,`city`,`province`,`country`,`vehicleRegistration`,`speed`,`makeVisible`,`picture`,`providedby`,`areaCode`,`updatedDate`,`expirationDate`,`datetime`,`UnixIncidentTime`,`UnixExpireTime`)VALUES('".$eventType."', '".$fetchStatusCode['statusCode']."' ,'".$trafficItemId."', '','".addslashes($eventDescription)."','".$longitude."','".$latitude."','".$address."','".addslashes($city)."','".addslashes($province)."','".$country."','".$county."',' ','0',' ','Here',' ','".$ENTRYTIME."','".$ENDTIME."','".$currentdate."','".$UnixIncidentTime."','".$UnixExpireTime."')") or die(mysql_error());
         }

   }

}

?>




Aucun commentaire:

Enregistrer un commentaire