Rt
Originally used fopen + fread ($fp, read bytes) Get data SAE does not support just want to change to curl
I just need to match the value of the title to go to the first 800 bytes of the file is OK, curl parameters are numerous, do not know which to set.
After all, it takes a lot of time to get the entire HTML file, as long as the first 800 bytes are OK, so it should save a little time, I don't have much time to test it with Microtime, but it's different.
Reply content:
Rt
Originally used fopen + fread ($fp, read bytes) Get data SAE does not support just want to change to curl
I just need to match the value of the title to go to the first 800 bytes of the file is OK, curl parameters are numerous, do not know which to set.
After all, it takes a lot of time to get the entire HTML file, as long as the first 800 bytes are OK, so it should save a little time, I don't have much time to test it with Microtime, but it's different.
Curl has a range option, and the unit of measure is byte and can be set in the following ways:
curl_setopt($ch, CURLOPT_RANGE, '0-799');
But this does not necessarily work, it just sends a request header, how to return the data or is determined by the sender, if the sender supports the Shard return will take effect, otherwise, or full return. The stream can also be implemented, and the header information for the range is sent, so the result should be the same:
$context = stream_context_create(array('http' => array ('header'=> 'Range: bytes=0-799')));$data = file_get_contents("http://example.com/file.html", FALSE, $context);
RFC document on range header: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35
You can do this. But every time you read it, it's likely to exceed the number you set, so you can judge it.
$url, CURLOPT_WRITEFUNCTION => 'receivePartial', ));curl_exec($ch);curl_close($ch);function receivePartial($ch, $chunk) { global $data; $data .= $chunk; $len = strlen($chunk); echo 'had receive ', $len, ' bytes', PHP_EOL; //判断每次读取,如果总数大于1000,就不再往下读了. if (strlen($data) >= 1000) { return -1; } //返回值是告知CURL,是否已够了,要不要再读啦. return $len;}echo $data;
title
is it more appropriate to use a function, depending on the need to get the page file_get_contents
?
$content = file_get_contents (' http://www.baidu.com ', false, NULL,-1, 800 if (mb_detect_encoding ($content) = = ' GB2312 ') $content = Iconv (' GB2312 ', ' UTF-8 ', $content);p reg_match ("/. *& lt;\/title>/", $content, $title); </code></pre>