我有一个API端点,可根据某些传递数据动态生成图像。我想调用API并将响应下载到文件中。理想情况下,我希望避免在PHP中调用cURL而是使用标准库。什么是在PHP中实现这一目标的最佳方法?
请求在cURL中显示如下:
curl https://localhost:4000/bananas/12345.png \
-O \
-X POST \
-d '[ 1, 2, 3, 4 ]'
答案 0 :(得分:0)
您必须使用标题:
header("Content-Type: image/jpeg");
$url = 'https://example.com/images/12345.jpg';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1)
AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.1 Safari/537.11');
$res = curl_exec($ch);
$rescode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch) ;
echo $res; // This response can be stored in any type of file using below link