在下面的场景中使用substr的时候, 有时候会报超出内存fatal error ,当curl读取的内容过大的时候
$header_size = curl_getinfo($curl_handle, CURLINFO_HEADER_SIZE);
$this->response_headers = substr($this->response, 0, $header_size);
$this->response_body = substr($this->response, $header_size);
$this->response_code = curl_getinfo($curl_handle, CURLINFO_HTTP_CODE);
$this->response_info = curl_getinfo($curl_handle);
用substr切分curl返回的内容 , 分出header头和body体
1. 测试一下:
<?php
$start=memory_get_usage();
$str="123";
var_dump(memory_get_usage()-$start);
//$a=substr($str,0,1);
$a=$str;
var_dump(memory_get_usage()-$start-24);
$b=$str;
//$b=mb_substr($str,1);
var_dump(memory_get_usage()-$start-24);
$c=$str;
//$b=mb_substr($str,1);
var_dump(memory_get_usage()-$start-24);
这个变量赋值,占48字节
2.使用substr切分后
<?php
$start=memory_get_usage();
$str="123";
var_dump(memory_get_usage()-$start);
//$a=substr($str,0,1);
$a=$str;
var_dump(memory_get_usage()-$start-24);
//$b=$str;
$b=substr($str,0,1);
var_dump(memory_get_usage()-$start-24);
//$c=$str;
$c=substr($str,1);
var_dump(memory_get_usage()-$start-24);
切分后赋值,占到了92字节 和 96字节 , 比原变量涨了一倍多