what does curlopt_binarytranfer exactly mean?

我们两清 提交于 2019-12-06 23:04:23

问题


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.



来源:https://stackoverflow.com/questions/6007363/what-does-curlopt-binarytranfer-exactly-mean

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!