• PHP传输base64数据不完整解决方法


    前言

    最近在做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'];
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    替换为以下代码:

    <?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); 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    即可解决

  • 相关阅读:
    云上未来:探索云计算的技术变革与应用趋势
    codeblocks快捷键
    模块化设计瞎谈
    两个队列实现一个栈
    julia笔记:函数
    iOS基础开发介绍
    搜索引擎分布式系统思考实践
    视频一键转码:批量转换MP4视频的技巧
    java毕业设计奖助学金评审(附源码、数据库)
    CDH 01CentOS配置(markdown新版二)
  • 原文地址:https://blog.csdn.net/qq_35230125/article/details/139098660