what does curlopt_binarytranfer exactly mean?

2019-04-22 20:06发布

问题:

i dont understand what is the difference between

CURLOPT_RETURNTRANSFER AND
CURLOPT_BINARYTRANSFER

i wrote a script to check it

<?php
$image_url = "http://localhost/curl/img1.png";
$ch = curl_init();
$timeout = 0;
curl_setopt ($ch, CURLOPT_URL, $image_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
$image = curl_exec($ch);
curl_close($ch);
header("Content-type: image/jpeg");
print $image;
?>

in this case i get the image displayed in the browser if i remove the line

curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);

i still get the image displayed in the browser.

and now if i remove the line

header("Content-type: image/jpeg");

then iget binary data display in browser(looks like garbage) in both cases whether i remove curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); or i dont remove. then what difference does this option CURLOPT_BINARYTRANSFER make?

回答1:

It looks like CURLOPT_BINARYTRANSFER isn't used by PHP anymore, if I am understanding this PHP bug report and resolution correctly.

https://bugs.php.net/bug.php?id=55635



回答2:

With this kind of curl we are looking at two transfers:

  1. data travels from the target host to our PHP host
  2. it is sent from our PHP host to our browser.

By removing header("Content-type: image/jpeg"); you deny the browser the clue as to how the content that follows should be interpreted. This has nothing to do with CURLOPT_BINARYTRANSFER which affects the first transfer.



标签: php libcurl