前言

最近在做OCR增值税务处理时,接口是通过图片转base64提交处理然后返回数据的,我通过前端将图片转换为base64提交到后端接收时,通过在线工具进行测试,发现传递过去的数据可以使用,接收到的数据却提示损坏

解决办法

<?php
header('Content-Type: text/html; charset=utf-8');
header('Access-Control-Allow-Origin: *'); // 允许任何网址请求
header('Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE'); // 允许请求的类型
header('Access-Control-Allow-Credentials: true'); // 设置是否允许发送 cookies
header('Access-Control-Allow-Headers: Content-Type, Content-Length, Accept-Encoding, X-Requested-with, Origin'); // 设置允许自定义请求头的字段


// 接收POST数据

$base64=$_POST['base'];

替换为以下代码:

<?php
header('Content-Type: text/html; charset=utf-8');
header('Access-Control-Allow-Origin: *'); // 允许任何网址请求
header('Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE'); // 允许请求的类型
header('Access-Control-Allow-Credentials: true'); // 设置是否允许发送 cookies
header('Access-Control-Allow-Headers: Content-Type, Content-Length, Accept-Encoding, X-Requested-with, Origin'); // 设置允许自定义请求头的字段


// 接收POST数据
$postData = file_get_contents('php://input');
$base64=urldecode($postData);
$new_base64 = substr($base64, 27); 

即可解决

Logo

欢迎加入西安开发者社区!我们致力于为西安地区的开发者提供学习、合作和成长的机会。参与我们的活动,与专家分享最新技术趋势,解决挑战,探索创新。加入我们,共同打造技术社区!

更多推荐