[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1960
  • Last Modified:

PHP: fOpen url problem

When I include this line in my php page,

$fp = fopen ("http://www.yahoo.com","r");

It would take a long long time to load the page. If I remove this line, it will loads just real fast.
There's no error, warning or anything. Does anyone know what could be the problem?

The php.ini file has set the fopen_url thing enabled. I cant see what is wrong.
0
jtcy
Asked:
jtcy
  • 9
  • 8
2 Solutions
 
Aleksandar BradarićSoftware DeveloperCommented:
> $fp = fopen ("http://www.yahoo.com","r");

This line is accessing Yahoo and reading their homepage. What do you do with it afterwards?
0
 
jtcyAuthor Commented:
I am doing :

$fp = fopen ("http://www.yahoo.com","r");
$data = fgetcsv ($fp, 3000, ",")
0
 
Aleksandar BradarićSoftware DeveloperCommented:
Is `fgetcsv` in a loop? Are you treating the whole Yahoo homepage as a CSV file?

When you remove it, it loads faster because all operations are local. When turned on, the code goes to Yahoo, waits for the page to load completely and only then continues the operation. That's why it's slower.

Now, does this sound like what you wanted to achieve? How long does it take to load the Yahoo homepage?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
jtcyAuthor Commented:
Actually, I am getting a csv file from a website, and then read it....
0
 
Aleksandar BradarićSoftware DeveloperCommented:
OK. How large is this CSV file? What's your connection speed? And how long does the execution of the PHP script take?
0
 
jtcyAuthor Commented:
<?php
//stock quote script
$fp = fopen ("http://download.finance.yahoo.com/d/quotes.csv?s=LML.AX&f=sl1d1t1c1ohgv&e=.csv","r");
//this uses the fgetcsv function to store the quote info in the array $data
$data = fgetcsv ($fp, 3000, ",")

?>


It took about 30, 40 seconds...sometimes even more....
0
 
Aleksandar BradarićSoftware DeveloperCommented:
> It took about 30, 40 seconds...sometimes even more....

Well, if the CSV file is large or if you're on a slower connection, then it might be using the time to download the file...
0
 
jtcyAuthor Commented:
No, it's not the csv problem. I tried fopen google.com and it also the same...It used to work fine, but since this week, it happens so...
0
 
Aleksandar BradarićSoftware DeveloperCommented:
I've ran the code from my local machine and it executes in 1-2 seconds (retrieveing just the 3000 bytes - no loop).

There's nothing wrong with the code. Have you changed anything in your configuration (PHP, web server)?
0
 
jtcyAuthor Commented:
Yeah....i think it must be that the hosting people done smth...in the configuration file. But i need to at least report that to the people by saying smth like enabling smth or increase smth...The problem is, I dont know what needs to be checked or changed....
0
 
Aleksandar BradarićSoftware DeveloperCommented:
If that's a shared hosting server, it may be that something else on the server is slowing down all other activity, including your script. Can you look up servers CPU and memory usage, etc?
0
 
jtcyAuthor Commented:
I cant see any server resources, it's hosted on shared server over HSphere panel.

I think it must be the run out of memory space, dont u think?
0
 
jtcyAuthor Commented:
But it really isnt smth big to read....It's fine if I fOpen(local file), but it would jam if it is opening a url. If i remove the fopen code, it will just run as fast...
0
 
Aleksandar BradarićSoftware DeveloperCommented:
My guess is that it doesn't matter what you read or where from. It seems to be having a delay when making outbound requests. Have you tried doing something else from that server, e.g. issuing `wget` from the shell of something similar? How fast does it download the file this way? Is there an initial delay?
0
 
Aleksandar BradarićSoftware DeveloperCommented:
> it would jam if it is opening a url.

Does it return any results at all? Or it fails (timeout)?
0
 
jtcyAuthor Commented:
Um, most time it would just load like.....40, 50 seconds...and then show everything...including the data i read from fopen. Few times, it would just load for few minutes and show blank page. Few times, it would load for 40, 50 seconds and then show everything, but not the data i read from fopen.
0
 
Aleksandar BradarićSoftware DeveloperCommented:
My best guess would be that there is a problem on the server and you should certainly contact the support and see what they say. It may be a connection problem, issues with memory or temporary files. It might even go away after a restart. From what you've said, it does not look like you're doing anything wrong...
0
 
Richard QuadlingSenior Software DeverloperCommented:
Do you need the file EVERY time?

Maybe a better solution is to not try and access the file as CSV data, but to save it to a local copy first.

<?php
file_put_contents('./local.csv', file_get_contents('http://download.finance.yahoo.com/d/quotes.csv?s=LML.AX&f=sl1d1t1c1ohgv&e=.csv'));

$fp_Local = fopen('./local.csv', 'r');
...
?>
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now