开发者

PHP Script to Retrieve RSS Feed, timeout issue

开发者 https://www.devze.com 2023-03-29 16:10 出处:网络
I am running a PHP script which calls out to world weather online to retrieve wave height, wave duration and water temperature by latitude longitude. However my host only allows 10 second 开发者_开发知

I am running a PHP script which calls out to world weather online to retrieve wave height, wave duration and water temperature by latitude longitude. However my host only allows 10 second 开发者_开发知识库script execution. I was wondering if I could trouble the community to give me some ideas / pointers on optimizing my script, as I do not have access to the php_ini on my host. The script works, only updates around 35 records. There are 100 records total. Me thinks I am throwing to much data at the world weather API.

Here is my script:

$connection=mysql_connect('myhost', 'user', 'post');
if (!$connection) {  die('Not connected : ' . mysql_error());}


$db_selected = mysql_select_db('ilovebigbutts', $connection);
if (!$db_selected) {
die ('Can\'t use db : ' . mysql_error());
}

// Here I am selecting the lat long and pri_id from table
$query = "SELECT * FROM beaches";
$result = mysql_query($query);
if (!$result) {
die("Invalid query: " . mysql_error());
}

while ($row = @mysql_fetch_assoc($result)) {
$id = $row["pri_id"];
$lat = $row["lat"];
$lng = $row["lng"];

$feed_url = "http://free.worldweatheronline.com/feed/marine.ashx?  key=xxxxxxxxxxxx&q=".$lat.",".$lng."&format=xml";
$xmlString = file_get_contents($feed_url);  
$xml = new SimpleXMLElement($xmlString); 
$items = $xml->xpath('weather/hourly');
$closeItems = array(); 
$new_array = array(); 
foreach($items as &$item)  
{ 
$waves = $item->swellHeight_m;
$wave_secs = $item->swellPeriod_secs;
$water_temp = $item->waterTemp_F;
$query1 = "UPDATE beaches SET waves = '$waves', wave_secs = '$wave_secs', water_temp = '$water_temp' WHERE pri_id = '$id' LIMIT 1";
  $update_result = mysql_query($query1);
  if (!$update_result) {
    die("Invalid query: " . mysql_error());
  }
}

}


I'm surprised this is taking > 10 seconds. Do you have an index on pri_id ? If you do, you could always put a limit on your original select query and do it in chunks.


Before optimizing, did you check if

<?php
set_time_limit(60); // 1min execution

works ?

0

精彩评论

暂无评论...
验证码 换一张
取 消