简体   繁体   中英

curl_exec vs curl_multi_getcontent

so I'm trying to get the contents of an image (the first 700 bytes of it) from a remote site:

$headers = array(
"Range: bytes=0-700"
);
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$raw = curl_exec($curl);
curl_close($curl);
@$im = imagecreatefromstring($raw);

where $url is some remote image....and it works just fine

but then when I use curl_multi_getcontent,

$h = curl_init();
$headers = array(
"Range: bytes=0-700"
);
curl_setopt($h, CURLOPT_URL, $url);
curl_setopt($h, CURLOPT_HEADER, $headers); 
curl_setopt($h, CURLOPT_RETURNTRANSFER, 1); //return the image value

$mh = curl_multi_init();
curl_multi_add_handle($mh, $h);

$running = null;
do {
   curl_multi_exec($mh, $running);
} while ($running > 0);

$raw = curl_multi_getcontent($h);
@$im = imagecreatefromstring($raw);

PHP will complain at the @$im = imagecreatefromstring($raw); line that Data is not in a recognized format

what did I do wrong? I would like to use the multi exec option since I can then parallelize it....

I also tried changing this line: curl_setopt($h, CURLOPT_HEADER, $headers); in the multi segment into CURLOPT_HTTPHEADER just like the first one, but instead the connection was reset and once again it was due to imagecreatefromstring since after commenting that line, it didn't get reset

Your second version of the code contains:

curl_setopt($h, CURLOPT_HEADER, $headers); 

This instructs cURL to return HTTP headers among with the result (ie CURLOPT_HEADER, TRUE)

To fix the problem, use:

curl_setopt($h, CURLOPT_HTTPHEADER, $headers);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM