...

View Full Version : fopen on a webpage doesn't yield the array



MrBiggZ
07-02-2010, 05:31 PM
Greetings!

This is my goal to be able to read a webpage into a file then strip out the addresses of the locations. It's so I can make POI file for my GPS.

Now the strange thing is .. the file gets created but never gets anything put into it. The web URL is valid.



<?php
/***********************************************************************/

ini_set('display_errors', 1);
ini_set('error_reporting', E_ALL);
ini_set('display_startup_errors', 1);

/***********************************************************************/

$filename = "kroger.txt";
$web_url = "http://services.kroger.com/StoreLocator/StoreLocatorResults.aspx?AddressLineOne=&AddressLineTwo=&City=Indianapolis&State=IN&ZipCode=&Radius=50&Banner=KROGER&Organics=False&OneHourPhoto=False&Pharmacy=False&Pharmacy24=False&FuelCenter=False&Bank=False&Marketplace=False&PharmacyServices=False&FluFinder=False&Generics=False";

if (file_exists($filename) == FALSE)
{
touch($filename);
for ($i=1; $i <= $pages; $i++ )
{
$fileArray = file($web_url,FILE_SKIP_EMPTY_LINES);
$fileHandle = fopen($filename, "a");
foreach ($fileArray as $line) fwrite($fileHandle, $line);
}
fclose($fileHandle);
}

?>


I don't get any errors messages .. just a blank screen the file that is created is empty. I've used this same bit of code before when I was stripping out items from my youtube playlists and that works great! Is this because the website doesn't want me imposing this type of behavior on them? :eek:

Thanks for any help! I'll keep plugging away while waiting! :thumbsup:

tomws
07-02-2010, 06:26 PM
After you run it the first time, it will never do anything again because the file already exists. It will be a blank screen no matter what happens since you don't have any output. You should include some output at least for debugging so you'll know what's going on.

Fou-Lu
07-02-2010, 07:54 PM
I may be a bit blind here, where is $pages defined? Also, fclose will need to go inside the for loop.

MrBiggZ
07-03-2010, 01:30 AM
I may be a bit blind here, where is $pages defined? Also, fclose will need to go inside the for loop.

Guess I was too! Blind that is! Grrrrr!


<?php
/***********************************************************************/

ini_set('display_errors', 1);
ini_set('error_reporting', E_ALL);
ini_set('display_startup_errors', 1);
ini_set('tidy.clean_output', 1);

/***********************************************************************/

$filename = "kroger.txt";
$web_url = "http://services.kroger.com/StoreLocator/StoreLocatorResults.aspx?AddressLineOne=&AddressLineTwo=&City=Indianapolis&State=IN&ZipCode=&Radius=50&Banner=KROGER&Organics=False&OneHourPhoto=False&Pharmacy=False&Pharmacy24=False&FuelCenter=False&Bank=False&Marketplace=False&PharmacyServices=False&FluFinder=False&Generics=False";
sleep(10);
$fileArray = file($web_url,FILE_SKIP_EMPTY_LINES);
$fileHandle = fopen($filename, "w");
foreach ($fileArray as $line) fwrite($fileHandle, $line);
fclose($fileHandle);

?>

There we go! But ... it seems that page loads something and I don't what I had intended to use :( If you actually plug that URL in a browser you'll see the page in its complete form. Unless somebody knows a trick to get the page to load completely before loading it into the array.

I get this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Error</title>
<link href="/css/banner.css" type="text/css" rel="stylesheet" />
</head>
<body>
<div id="main">
<div id="grayBlock">
<div id="header">
<div id="branding">
<img src="/SiteCollectionImages/multibanner/banner_logo_header.gif" alt="The Kroger Co Family of Stores" />
</div>
</div>
<div id="contentBox">
<div id="boxTop"></div>
<div class="body">
<h1>Error!</h1>
<br />
<p>We’re sorry, but we are unable to process your request at this time. Please try again later.</p>
</div>
<div id="boxBottom"></div>
</div>
</div>
<div id="clear"></div>
<div id="legalFooter">
<div class="body">
<div>All Comments © Copyright 2009 The Kroger Co. All Rights Reserved</div>
</div>
</div>
</div>
</body>
</html>

Fou-Lu
07-03-2010, 07:43 AM
The page does load before file. What it returns is the same html provided to a browser with the same input. Since the result don't match, the input must be at fault. Try playing with a combination of htmlspecialchars and urlencode on the url. If I had to guess'er, I'd say the & is the problem.
Also, given what you are doing, I'd just use fopen (we know url is enabled), and write directly as a string using fread or fpassthru on the buffer. Much faster.

MrBiggZ
07-04-2010, 01:24 AM
The page does load before file. What it returns is the same html provided to a browser with the same input. Since the result don't match, the input must be at fault. Try playing with a combination of htmlspecialchars and urlencode on the url. If I had to guess'er, I'd say the & is the problem.
Also, given what you are doing, I'd just use fopen (we know url is enabled), and write directly as a string using fread or fpassthru on the buffer. Much faster.

Yeah .. I tried a bunch of different things all got the same results. Googled my butt off! I always ended up with that "ERROR! We're sorry, but we are unable to process your request at this time. Please try again later." I'm guessing that its because of the time it takes the search results to be posted to the page (takes a couple of seconds). Oh well!

Hey .. you wouldn't happen to know of a way to extract the text from a PDF document would you??

Fou-Lu
07-04-2010, 01:37 AM
You can either read up on pdf file structure and write your own handler, or you can use php's pdf library.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum