GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 20-Oct-2004, 03:31
Darksat Darksat is offline
Awaiting Email Confirmation
 
Join Date: Aug 2004
Location: London
Posts: 97
Darksat is on a distinguished road

XML feed in PHP


I cant get this to work, can anyone help me.

Code:
PHP Code:

<?php 
$feed = 'http://www.incorporateacompany.com/new/prv/XML.xml'; 

ini_set('allow_url_fopen', true); 
$fp = fopen($feed, 'r'); 
$xml = ''; 
while (!feof($fp)) { 
   $xml .= fread($fp, 128); 
} 
fclose($fp); 

function untag($string, $tag) { 
   $tmpval = array(); 
   $preg = "|<$tag>(.*?)</$tag>|s"; 

   preg_match_all($preg, $string, $tags); 
   foreach ($tags[1] as $tmpcont){ 
      $tmpval[] = $tmpcont; 
   } 
   return $tmpval; 
} 
$forecastData = untag($xml, 'forecastData'); 

$html = '<p>'; 
foreach ($forecastData as $forecastData) { 
   $forecastLocation = untag($item, 'forecastLocation'); 
   $forecastTime = untag($item, 'forecastTime'); 

   $html .= '<b>' . $forecastTime[0] . $forecastLocation[0] . "</b><br />\n"; 
} 
$html .= '</p>'; 

echo $html;




This is something I need help with.
It wont work, its a PHP script to display a weather feed, if anyone can help with this it would be appreciated.
I got it off this page

martin.f2o.org

and modified it accordingly but.
The page just brings up

"">
"">
"">
"">
"">
"">

repeatedly

and the source for it is

Code:
<p><b>""></b><br />
<b>""></b><br />
<b>""></b><br />
<b>""></b><br />
<b>""></b><br />
Last edited by JdS : 22-Oct-2004 at 06:47. Reason: Please insert your php code between [php] [/php] tags
  #2  
Old 21-Oct-2004, 01:06
JasonMichael's Avatar
JasonMichael JasonMichael is offline
Awaiting Email Confirmation
 
Join Date: Jul 2004
Posts: 135
JasonMichael has a spectacular aura about
Those forecast locations in the XML feed remind me of Ireland.

Your main error is in the following foreach loop:


PHP Code:

foreach ($forecastData as $forecastData) { 
   $forecastLocation = untag($item, 'forecastLocation'); 
   $forecastTime = untag($item, 'forecastTime'); 

   $html .= '<b>' . $forecastTime[0] . $forecastLocation[0] . "</b><br />\n"; 
} 



For one, you can't have foreach($forecastData as $forecastData)... these must be different variables. In the example on the website that you found the website, their example is listed as using foreach ($items as $item). Notice how we are looping through the multiple $items of the array, to process one $item, within this foreach loop.

I also think that you have a typo in the string you are using to create the $html variable.

Here is some code that I think will solve the problems:

PHP Code:

$forecastData= untag($xml, 'forecast'); // use this, or you will never get forecastLocation, since it is within the forecast tag of the XML

$html = '<p>';
foreach ($forecastData as $forecastDataItem) {
    $location= untag($forecastDataItem, 'forecastLocation');
    $time= untag($forecastDataItem, 'forecastTime');

    $html .= '<a> Forecast Location:&nbsp;'. $location[0] .'&nbsp;Time:'.$time[0].'</a><br>\n';
}
$html .= '</p>';

echo $html; 



But this only gives you the very first time for each location.


This script works better (see it in action at www.jmrtechnet.com)

PHP Code:

<?
$feed = 'http://www.incorporateacompany.com/new/prv/XML.xml'; 

ini_set('allow_url_fopen', true); 
$fp = fopen($feed, 'r'); 
$xml = ''; 
while (!feof($fp)) { 
   $xml .= fread($fp, 128); 
} 
fclose($fp); 

function untag($string, $tag) { 
   $tmpval = array(); 
   $preg = "|<$tag>(.*?)</$tag>|s"; 

   preg_match_all($preg, $string, $tags); 
   foreach ($tags[1] as $tmpcont){ 
      $tmpval[] = $tmpcont; 
   } 
   return $tmpval; 
} 

$forecastData= untag($xml, 'forecast'); // use this, or you will never get forecastLocation, since it is within the forecast tag of the XML

$html = '<p>';
foreach ($forecastData as $forecastDataItem) {
    $location= untag($forecastDataItem, 'forecastLocation');
    $time= untag($forecastDataItem, 'forecastTime');

    $html .= '<BR><a> Forecast Location: &nbsp;' . $location[0]."<HR>";
    
    foreach($time as $timeItem) {
                                     $html.= '&nbsp;Time:'.$timeItem.'</a><br>';
    }
}
$html .= '</p>';

echo $html;
?>


I hope this helps.
  #3  
Old 21-Oct-2004, 13:06
Darksat Darksat is offline
Awaiting Email Confirmation
 
Join Date: Aug 2004
Location: London
Posts: 97
Darksat is on a distinguished road
Thank you very much for your help.
I have managed to get the script working but if I put in to many variables it pulls up this message.

Fatal error: Maximum execution time of 30 seconds exceeded in /hsphere/local/home/surfingi/surfingireland.net/forecast.php on line 39

the code Im use is this.
Its fine until you add to many variables and then it goes tits up.

PHP Code:

<? 
$feed = 'XML.xml'; 

ini_set('allow_url_fopen', true); 
$fp = fopen($feed, 'r'); 
$xml = ''; 
while (!feof($fp)) { 
   $xml .= fread($fp, 128); 
} 
fclose($fp); 

function untag($string, $tag) { 
   $tmpval = array(); 
   $preg = "|<$tag>(.*?)</$tag>|s"; 

   preg_match_all($preg, $string, $tags); 
   foreach ($tags[1] as $tmpcont){ 
      $tmpval[] = $tmpcont; 
   } 
   return $tmpval; 
} 

$forecastData= untag($xml, 'forecast'); // use this, or you will never get forecastLocation, since it is within the forecast tag of the XML 

$html = '<p>'; 
foreach ($forecastData as $forecastDataItem) { 
  $location= untag($forecastDataItem, 'forecastLocation'); 
  $time= untag($forecastDataItem, 'forecastTime'); 
  $windspeed= untag($forecastDataItem, 'windspeed'); 
  $winddirection= untag($forecastDataItem, 'winddirection'); 
  $pressure= untag($forecastDataItem, 'pressure'); 


  $html .= '<BR><a> Forecast Location: &nbsp;' . $location[0]."<HR>"; 
   
  foreach($time as $timeItem)
  foreach($windspeed as $windspeedItem) 
  foreach($winddirection as $winddirectionItem) 
  foreach($pressure as $pressureItem){ 
                   $html.= '&nbsp;Time:'.$timeItem.'&nbsp;Windspeed:'.$windspeedItem.'&nbsp;Winddirection:'.$winddirectionItem.'&nbsp;Pressure:'.$pressureItem.'<br>'; 
  } 
} 
$html .= '</p>'; 

echo $html; 
?>

  #4  
Old 21-Oct-2004, 15:09
misunderstood misunderstood is offline
Member
 
Join Date: Jun 2003
Posts: 121
misunderstood is on a distinguished road
JasonMichael
http://www.jmrtechnet.com/ is showing as cannot find server 10pm UK time.
  #5  
Old 21-Oct-2004, 17:11
JasonMichael's Avatar
JasonMichael JasonMichael is offline
Awaiting Email Confirmation
 
Join Date: Jul 2004
Posts: 135
JasonMichael has a spectacular aura about
Hi Darksat,

Try putting this in an .htaccess file in the directory with your script:

Code:
php_value max_execution_time 120

This should allow for 2 minutes (120 seconds) to elapse before PHP times out. By putting this value in an .htacess file, you are overriding the default PHP setting in the php.ini file. By default, I think PHP allows for code to execute 30 seconds.
  #6  
Old 21-Oct-2004, 17:12
JasonMichael's Avatar
JasonMichael JasonMichael is offline
Awaiting Email Confirmation
 
Join Date: Jul 2004
Posts: 135
JasonMichael has a spectacular aura about
Thanks for that update, misunderstood. I have a pretty good uptime on that site, but once in a great while, that happens.
  #7  
Old 23-Oct-2004, 05:19
Darksat Darksat is offline
Awaiting Email Confirmation
 
Join Date: Aug 2004
Location: London
Posts: 97
Darksat is on a distinguished road
Jason thanks for your help but now I have another problem.

I get this error
Fatal error: Call to undefined function: xslt_create() in /hsphere/local/home/surfingi/surfingireland.net/newsite/feed/xsl.php on line 13

in this script.

PHP Code:

<?php

class XslTransformer {
  var $parameters = array();
  
  function set_parameter($name, $value) {
    $this->parameters[$name] = $value;
  }
  
  function transform($xmlURL, $xslURL) {

    // Allocate a new XSLT processor
    $xh = xslt_create();

    // Set the path to the document root
    $path = $_SERVER['PATH_TRANSLATED'];
    $path = substr($path,0,strrpos($path, '\\')+1);
    xslt_set_base($xh, "file://".$path);

    // Read xml & xsl files int0 memory
    $xml = file_get_contents($xmlURL) or exit("Unable to open xml: $xmlURL");
    $xsl = file_get_contents($xslURL) or exit("Unable to open xsl: $xslURL");

    // Fix DOCTYPE error in BBC xml feed
    $xml = str_replace('<!DOCTYPE rss SYSTEM', '<!DOCTYPE rss PUBLIC "-"', $xml);
    //print "<xmp>".$xml."</xmp>";

    // Define argument list from xslt processor
    $arguments = array('/_xml' => $xml,'/_xsl' => $xsl);

    // Process the document
    //$result = xslt_process($xh, 'arg:/_xml', 'arg:/_xsl', NULL, $arguments, $parameters);
    $result = xslt_process($xh, 'arg:/_xml', 'arg:/_xsl', NULL, $arguments, $this->parameters);

    // Display output
    if (!$result) {
      print "<br/>";
        print "XSLT process error.<br/>";
        print "Error code: ".xslt_errno($xh)."<br/>";
        print "Reason: ".xslt_error($xh)."<br/>";
    }
    $result = str_replace('<?xml version="1.0" encoding="UTF-8"?>', '', $result);

    // Free the processor
    xslt_free($xh);

    return $result;
  }
}

?>


it works on XAMP but not when I ftp to a linux server.
I have changed permissions and everything, but I dont think thats the problem.
  #8  
Old 23-Oct-2004, 21:09
JasonMichael's Avatar
JasonMichael JasonMichael is offline
Awaiting Email Confirmation
 
Join Date: Jul 2004
Posts: 135
JasonMichael has a spectacular aura about
Create a short script in a filenamed "info.php" like the one below, to see if your hosting provider configured PHP to include the --enable-xslt and --with-xslt-sablot options. Without these options available, your script won't work.


PHP Code:

phpinfo(); 



If *you* happen to be the one owning that Linux server, then you'll need to do some more work configuring it, to get XSLT working. You'll need to install the Sablotron library (from my previous experience, this was a REAL pain). Hopefully, you can easily do an RPM install.

I recommend reading the reference to the us2.php.net at the PHP.net website and go over their instructions for doing the installation, as a start.
  #9  
Old 25-Oct-2004, 07:11
Darksat Darksat is offline
Awaiting Email Confirmation
 
Join Date: Aug 2004
Location: London
Posts: 97
Darksat is on a distinguished road
I dont own the server.

I will try that when I get home, my firewall in work stops me FTPing.
cheers.
 
 

Recent GIDBlogWriting a book by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
XML feed into PHP graph Darksat MySQL / PHP Forum 2 27-Sep-2004 09:33
uisng php to display php dopee MySQL / PHP Forum 6 14-May-2004 18:40
php software dopee MySQL / PHP Forum 0 04-May-2004 11:26
[Linux] Installing PHP --with-mcrypt JdS Web Hosting Forum 0 20-Aug-2003 08:40
All the big PHP script collections that matter jrobbio MySQL / PHP Forum 5 06-Jun-2003 16:14

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:41.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.