Page 1 of 1
file()
Posted: Sat Oct 18, 2008 11:11 pm
by katier0se
I have to write a function that reads a URL and writes its lines into a sequential array. It is called ReadURL and the argument is $url. This is what I have tried:
Code: Select all
<?php
function readURL($url) {
$FileArray = file($url);
return $FileArray;
}
readURL('http://www.nytimes.com/services/xml/rss/nyt/Business.xml');
print_r $FileArray;
?>
I want to see the array to make sure it is formatted correctly before I move on in my program. This doesn't output anything. Any suggestions?
Re: file()
Posted: Sat Oct 18, 2008 11:35 pm
by requinix
When you return something from a function you have to assign the value somewhere - you don't magically get a variable. Like what you had to do with file(): it returned something and you assigned it to $FileArray.
You have to do the same thing when using your own functions.
Code: Select all
$FileArray = readURL('http://www.nytimes.com/services/xml/rss/nyt/Business.xml');
Just so you know, the $FileArray in the function and the $FileArray outside are completely different variables.
Re: file()
Posted: Sun Oct 19, 2008 4:15 pm
by katier0se
Thanks, that worked. I'm getting an array now. But not every key has something assigned to it. The lines that seem to be missing are the ones specific to xml files, such as <item> and </item> ... I'm looking for information that lies between these two tags and need to be able to write a function that only does something once I know I'm 'inside' the <item>
Re: file()
Posted: Sun Oct 19, 2008 4:24 pm
by requinix
Just a guess, but try
Code: Select all
<?php
header("Content-Type: text/plain");
function readURL($url) {
$FileArray = file($url);
return $FileArray;
}
$FileArray = readURL('http://www.nytimes.com/services/xml/rss/nyt/Business.xml');
print_r($FileArray);
?>
and see if you still have that problem.
Re: file()
Posted: Sun Oct 19, 2008 4:40 pm
by katier0se
nope :/
Re: file()
Posted: Sun Oct 19, 2008 4:55 pm
by requinix
How does your output differ from
Code: Select all
Array
(
[0] => <?xml version="1.0"?>
[1] => <rss xmlns:dc="http://dublincore.org/documents/dcmi-namespace/" xmlns:media="http://search.yahoo.com/mrss" xmlns:nyt="http://www.nytimes.com/namespaces/rss/2.0" version="2.0">
[2] => <channel>
[3] => <title>NYT > Business</title>
[4] => <link>http://www.nytimes.com/pages/business/index.html?partner=rssnyt</link>
[5] => <description/>
[6] => <language>en-us</language>
[7] => <copyright>Copyright 2008
[8] => The New York Times Company</copyright>
[9] => <lastBuildDate>Sun, 19 Oct 2008 21:45:08 GMT
[10] => </lastBuildDate>
[11] => <image>
[12] => <title>NYT > Business</title>
[13] => <url>http://graphics.nytimes.com/images/section/NytSectionHeader.gif</url>
[14] => <link>http://www.nytimes.com/pages/business/index.html</link>
[15] => </image>
[16] => <item>
[17] => <title>The Reckoning: Building Flawed American Dreams</title>
[18] => <link>http://www.nytimes.com/2008/10/19/business/19cisneros.html?partner=rssnyt&emc=rss</link>
[19] => <guid isPermaLink="true">http://www.nytimes.com/2008/10/19/business/19cisneros.html</guid>
[20] => <media:group>
[21] => <media:content>
[22] => <media:url>http://graphics8.nytimes.com/images/2008/10/18/business/19cisneros-75.jpg</media:url>
[23] => <media:medium>image</media:medium>
[24] => <media:height>75</media:height>
[25] => <media:width>75</media:width>
[26] => </media:content>
[27] => <media:description>Henry G. Cisneros</media:description>
[28] => <media:credit>Erich Schlegel for The New York Times</media:credit>
[29] => </media:group>
[30] => <description>For years Henry G. Cisneros helped low-income families buy homes, part of a trend with dire consequences.<br/><br/><span class="advertisement"> <a href="http://www.pheedo.com/click.phdo?x=b4d3f98f99d448c0ae34d28d64593f7b&u=http://www.nytimes.com/2008/10/19/business/19cisneros.html"><img src="http://www.pheedo.com/img.phdo?x=b4d3f98f99d448c0ae34d28d64593f7b&u=http://www.nytimes.com/2008/10/19/business/19cisneros.html" border="0"/></a></span>
[31] => </description>
[32] => <dc:creator>By DAVID STREITFELD and GRETCHEN MORGENSON</dc:creator>
[33] => <pubDate>Sun, 19 Oct 2008 16:25:08 GMT</pubDate>
[34] => <category domain="http://www.nytimes.com/namespaces/keywords/nyt_per">Cisneros, Henry G</category>
[35] => <category domain="http://www.nytimes.com/namespaces/keywords/des">Subprime Mortgage Crisis</category>
[36] => <category domain="http://www.nytimes.com/namespaces/keywords/des">Housing</category>
[37] => <category domain="http://www.nytimes.com/namespaces/keywords/des">Mortgages</category>
[38] => <category domain="http://www.nytimes.com/namespaces/keywords/nyt_org_all">KB Home|KBH|NYSE</category>
[39] => <category domain="http://www.nytimes.com/namespaces/keywords/mdes">Foreclosures</category>
[40] => <category domain="http://www.nytimes.com/namespaces/keywords/nyt_org_all">Countrywide Financial Corp</category>
[41] => <category domain="http://www.nytimes.com/namespaces/keywords/nyt_org_all">Housing and Urban Development Department</category>
[42] => </item>
...
Re: file()
Posted: Sun Oct 19, 2008 5:11 pm
by katier0se
never mind ... I was looking at the browser output instead of the debug output... thanks!