forked from openva/va-decoded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.php
104 lines (82 loc) · 2.22 KB
/
scraper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
/*
* The subdirectory where all of the raw XML will be stored. Omit the trailing
* slash.
*/
$output_dir = 'xml';
/*
* Initialize cURL.
*/
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
/*
* Request a list of all tiles.
*/
$url = 'http://law.lis.virginia.gov/LawPortalWebService/CodeofVAGetTitleList';
curl_setopt($curl, CURLOPT_URL, $url);
$xml = curl_exec($curl);
if ($xml === FALSE)
{
die('Fatal error: Could not retrieve a list of titles.');
}
/*
* Turn our XML into an object.
*/
$parser = xml_parser_create();
xml_parse_into_struct($parser, $xml, $titles, $index);
xml_parser_free($parser);
/*
* Iterate through the titles. Note that this isn't *really* a list of titles
* -- it's a list of XML nodes, some of which are titles, but most of which are
* noise, for our purposes.
*/
foreach ($titles as $title)
{
/*
* If this is a title number.
*/
if ($title['tag'] == 'TITLENUMBER')
{
$title_number = $title['value'];
$url = 'http://law.lis.virginia.gov/LawPortalWebService/CodeofVAGetChapterList/'
. $title_number;
curl_setopt($curl, CURLOPT_URL, $url);
$xml = curl_exec($curl);
if ($xml === FALSE)
{
echo 'Error: Title ' . $title_number . ' does not exist (' . $url . ')';
}
/*
* Turn our XML into an object.
*/
$parser = xml_parser_create();
xml_parse_into_struct($parser, $xml, $sections, $index);
xml_parser_free($parser);
/*
* Iterate through the chapters. Note that this isn't *really* a list
* of chapters -- it's a list of XML nodes, some of which are chapters,
* but most of which are noise, for our purposes.
*/
foreach ($sections as $section)
{
/*
* If this is a section number.
*/
if ($section['tag'] == 'SECTIONNUMBER')
{
$section_number = $section['value'];
$url = 'http://law.lis.virginia.gov/LawPortalWebService/CodeofVAGetSectionDetails/'
. $section_number;
curl_setopt($curl, CURLOPT_URL, $url);
$xml = curl_exec($curl);
if ($xml === FALSE)
{
echo 'Error: Section ' . $section_number . ' does not exist ('
. $url . ')';
}
$filename = $output_dir . '/' . str_replace(':', '_', $section_number) . '.xml';
file_put_contents($filename, $xml);
}
}
}
}