Power Automate中API返回图片无响应问题排查求助
问题:API在Power Automate中返回空响应(Postman正常)
我的目标是让API直接返回图片,无需存储在服务器上。流程为:API接收GIF图片,转换为PNG格式后返回。该功能在Postman中可正常运行,但在Power Automate中返回体为空。经测试代码已执行,但图片未成功返回。
业务代码
//header("Content-type: image/".$params['format']); require('image_converter.php'); //create object of image converter class $obj = new Image_converter(); //echo file_get_contents('php://input'); //$data = json_decode(file_get_contents('php://input'), true); $data = file_get_contents('php://input'); echo $data;//["\$multipart"];//[1]["body"]["\$content"]; //$converted_image = $obj->convert_image($params['format'], $data["\$multipart"][1]["body"]["\$content"]); //echo $data; $converted_image = $obj->convert_image($params['format'], json_decode($data, true)["\$content"]); echo $converted_image;
图片转换类核心代码
$binary = imagecreatefromstring(base64_decode($image)); //to png if($convert_type == 'png'){ $image_quality = floor(10 - ($image_quality / 10)); ob_start(); ImagePNG($binary, NULL, $image_quality); $result = ob_get_contents(); ob_end_clean(); return $result; }
排查与修复建议
恢复响应头设置
你注释掉了header("Content-type: image/".$params['format']);,Power Automate对响应头的识别比Postman更严格,缺失正确的Content-Type会导致它无法解析返回的二进制图片数据。恢复这行代码,确保返回Content-Type: image/png。清理干扰输出
代码中保留的echo $data;会在图片二进制数据前输出原始请求内容,导致响应数据损坏。Postman可能自动忽略这类干扰,但Power Automate无法解析混合文本与二进制的响应,必须删除这行调试代码。校验请求数据格式
Postman和Power Automate发送的请求体格式可能存在差异:当前代码假设请求是包含$content字段的JSON,但Power Automate可能直接发送图片二进制或多部分表单数据。取消注释echo file_get_contents('php://input');,查看Power Automate实际发送的请求内容,再调整解析逻辑。添加错误捕获
代码缺乏错误检查逻辑,无法定位问题环节。在关键步骤后添加校验:$decoded = base64_decode($image, true); if(!$decoded) { http_response_code(400); echo "无效的Base64编码数据"; exit; } $binary = imagecreatefromstring($decoded); if(!$binary) { http_response_code(400); echo "无法从字符串创建图片"; exit; }
内容的提问来源于stack exchange,提问作者brouckaertd
相关产品推荐
相关产品推荐

