0

I have a file (fullsoccer.txt) which has 500 links (online links for XML files) I call those links and then I pass each one to a function that open the link then read the content and then passing the content to parsing function (xml_parser) to parse the content and save it in my database. my problem is that the function is parsing and saving the last file only. I try to debug the code and find that all links are right and being opened but just the last one is being parsed I need your help on this is the problem with my code? or does fopen and fread in my loop receiving the second opening request before finishing the first reading request? this is my code:

function doParse($parser_object) {
  $links=file("./fullsoccer.TXT");
  foreach($links as $link)
  {
      set_time_limit(0);
      //echo 'reading '.$link."\n";
      $fp = fopen($link, "r");
      if ($fp!==false)
      {
          //loop through data
          while ($data = fread($fp, 4096)) {
              //parse the fragment
              xml_parse($parser_object, $data, feof($fp));
              //echo $data;
          }
          //echo "\n";
          fclose($fp);
      } else {
          echo 'Cannot Open Link '.$link."\n";
      }
  }
}

help my on this because I have been living with it for really a long time, please

unique_programmer
  • 1,561
  • 3
  • 11
  • 14

2 Answers2

0

can you try with file_get_contents(); ?
its easier..

  if (($data = file_get_contents($link)) !== FALSE) {
          xml_parse($parser_object, $data/*, feof($fp)*/);
  } else {
      echo 'Cannot Open Link '.$link."\n";
  }

also enable error_reporting(E_ALL); and ini_set("display_errors",1); for some more light..

  • I use this code but the problem is still the same . It just read the content of the last link in my links, I am getting crazy – unique_programmer May 09 '13 at 07:08
  • are you sure you're getting all the links from your .txt file? you verified it with `print_r();` just before the loop ? the code is too simple to have something hidden.. –  May 09 '13 at 07:12
  • yes this is the first think I get sure of it and I am sure that all links are served – unique_programmer May 09 '13 at 07:14
  • cannot think anything else than checking the result code of `xml_parse();` (0=fail) and analyzing it with `xml_error_string($code);` –  May 09 '13 at 07:18
-1

if you need to read text file you can use it like below

<?php
$file = fopen("fullsoccer.txt", "r");
$links = array();

while (!feof($file)) {
   $links[] = fgets($file);
}

fclose($links);

var_dump($links);
?>
liyakat
  • 11,825
  • 2
  • 40
  • 46