-1

i am writing a script to reach out to a website that downloads a csv into cache and then parses the data into an array.

 $base_url = "http://www.collincad.org/ccad/propertysearch/download.php?situs_num=1707&situs_street=university&situs_street_suffix=&isd%5B%5D=any&city%5B%5D=any&prop_type%5B%5D=R&prop_type%5B%5D=P&prop_type%5B%5D=MH&active%5B%5D=1&year=2018";

$handle = fopen($base_url, "r");
$flag = true;  
while(!feof($handle))
{
  $text = fgetcsv($handle, 1024, ",");
   if($flag) { $flag = false; continue; }

   print $text[1]. " <br>";
}

mysql_close($connect);

When performing the query this way it has the first row and a row of other data and ignores the comma.

 $base_url = "export5.csv";

 $handle = fopen($base_url, "r");
 $flag = true;  
 while(!feof($handle))
 {
  $text = fgetcsv($handle, 1024, ",");
   if($flag) { $flag = false; continue; }

   print $text[1]. " <br>";
}

mysql_close($connect);

but when i manually download the csv file it and read it from the local folder it works as expected... i would prefer not to make this a two step process... im thinking that reading direct from the site with php is the issue, just can figure out how to resolve it.

Thanks

1 Answers1

2

First and foremost when using fopen with a web url, make sure your server is configured to allow it (http://php.net/manual/en/filesystem.configuration.php#ini.allow-url-fopen). Once that is out of the way you should be fine with your code.

The issue though is the CSV format itself.

Looking at the CSV return of that url, its delimiters are tabs, not commas. And I see no enclosures too. So you need to change your fgetcsv to:

$text = fgetcsv($handle, 1024, "\t", '');

And it should begin to return results like this (for each $text):

Array
(
    [0] => 15071
    [1] => 2018
    [2] => P
    [3] => Personal
    [4] => P-9000-288-0243-1
    [5] => N
    [6] => ZZZZZZZ BPP @ 1707 W UNIVERSITY DR
    [7] => 
    [8] => 
    [9] => 
    [10] => 
    [11] => 
    [12] => BPP AT 1707 W UNIVERSITY DR
    [13] => KROGER #488
    [14] => 1707
    [15] => UNIVERSITY DR
    [16] => MCKINNEY
    [17] => 1707 W University Dr | McKinney, TX  75069
    [18] => 844925
    [19] => THE KROGER CO
    [20] => CMC
    [21] => MCKINNEY CITY
    [22] => SMC
    [23] => MCKINNEY ISD
    [24] => 
    [25] => Active
    [26] => No
    [27] => 
)

Also, the first line in the csv file is this:

Line 1:

Array
(
    [0] => sep=
    [1] => 
)

So you may want to skip the first TWO lines (the second line being the column headers).

Line 2: (column headers)

Array
(
    [0] => Property ID
    [1] => Year
    [2] => Property Type Code
    [3] => Property Type Description
    [4] => Geographic ID
    [5] => Abstract Or Subdivision Code
    [6] => Abstract Or Subdivision Description
    [7] => Block
    [8] => Tract Or Lot
    [9] => Mobile Home Park Code
    [10] => Mobile Home Park Description
    [11] => Mobile Home Park Space
    [12] => Legal Description
    [13] => Doing Business As
    [14] => Street Number
    [15] => Street Name
    [16] => City
    [17] => Complete Address
    [18] => Owner ID
    [19] => Owner Name
    [20] => Taxing City Code
    [21] => Taxing City Name
    [22] => Taxing School District Code
    [23] => Taxing School District Name
    [24] => Market Value
    [25] => Property Status
    [26] => Certified Data
    [27] => 
)
IncredibleHat
  • 4,000
  • 4
  • 15
  • 27