Reputation: 4556
What is the fastest way to read a file in PHP? Specifically I'm reading a url, I'm reading a file using fgets, the size of the url is about 1MB and reading 5 url's will took me 20 seconds at most. I am only getting a line of string, which located at the end portion of the file. I've actually use fseek to move the pointer to the end of the url but it only works on files(not url). Any brilliant ideas?
heres my sample code
$fp=fopen("http://url.com", "r");
if(is_bool($fp)){
exit;
}
while(!feof($fp)) {
$data = fgets($fp);
if($data=="this is what i've wanted")
{
// codes...
}
}
fclose($fp);
Upvotes: 1
Views: 1644
Reputation: 33432
You could use cUrl with support for resume download to retrieve only the last part of the file:
function retrieve_tail($remoteFile, $localFile = null, $bytes = 1000) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RANGE, intval($bytes)."-");
$fp = fopen($localFile, "w+");
curl_setopt($ch, CURLOPT_FILE, $fp);
$result = curl_exec($ch);
curl_close($ch);
fclose($fp);
return $result;
}
Then call:
$result = retrieve_tail("http://url.com", "local_copy.txt", 20000);
print_r($result);
And you will have what you want on the local file you specified. Also, you could set
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
to get the contents directly.
Upvotes: 1
Reputation: 2034
use CURL to get a remote url:
function curlRequest($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curl, CURLOPT_ENCODING, true);
curl_setopt($curl, CURLOPT_HEADER, false);
$a = new stdclass;
$a->body = curl_exec ($curl);
$a->status = curl_getinfo ($curl, CURLINFO_HTTP_CODE);
$a->effectiveURL = curl_getinfo ($curl, CURLINFO_EFFECTIVE_URL);
curl_close ($curl);
return $a;
}
Upvotes: 0
Reputation: 19552
In this situation you can't* just skip all of the content. The way network transfers work is that you define a content-length and have to read everything before the part you want.
In short, you can't just skip it. Just read it in, grab what you need and move on with life.
*Note: Well, you can if the resource supports partial-content and range-requests. Not likely.
Upvotes: 2