I'm trying to use an API that generates QR Code images and download the generated image using PHP. This is what I have so far:
function generateQrCode($message){
$url = 'https://backend.com/qr/create-image';
$data = array("message" => $message);
$postdata = json_encode($data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
$result = curl_exec($ch);
curl_close($ch);
$file = fopen("qr_code_image.jpg","w");
fwrite($file,$result);
}
I checked the backend (the application that generates the QR Code) and the image is being correctly generated and returned but I'm not being able to download the image as a JPG file from the PHP side. Using CURL to the API:
curl --location --request POST 'https://backend.com/qr/create-image' \
--header 'Content-Type: application/json' \
--data-raw '{
"message":"123"
}'
When I call the API via Postman, I can see the image correctly. But when I call it via PHP, I cannot download the image as a file