kamikaze_pilot
kamikaze_pilot

Reputation: 14834

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

Upvotes: 0

Views: 2143

Answers (1)

user1080697
user1080697

Reputation:

Your second version of the code contains:

curl_setopt($h, CURLOPT_HEADER, $headers); 

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

To fix the problem, use:

curl_setopt($h, CURLOPT_HTTPHEADER, $headers);

Upvotes: 1

Related Questions