Libcurl-php curl Inside can you specify the number of bytes to fetch when retrieving the HTML data for the page?

Source: Internet
Author: User
Tags fread
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;

titleis 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>  
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.