You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过PHP从SOAP Web Service下载Base64格式图片

解决PHP保存SOAP返回的Base64图片无法打开的问题

问题背景

我对PHP和SOAP了解有限,需通过供应商的SOAP Web Service获取电商平台产品图片(Base64编码返回)。已编写代码调用服务并获取数据,但保存到WordPress插件目录后的图片无法正常打开:Paint提示“无法读取该文件,不是有效的位图文件或格式不被支持”,电商网站仅显示白点。

错误原因

  1. 格式不匹配:你提供的Base64字符串开头为/9j/,这是JPEG/JPG格式的标识(PNG格式Base64开头为iVBORw0KGgo=),但代码中强制将所有图片保存为.png格式,导致文件格式混乱损坏。
  2. 多余的前缀处理:代码中尝试移除data:image/png;base64,前缀,但返回的Base64字符串本身不带该前缀,此操作无意义。
  3. 返回值错误:save_image函数返回file_put_contents的字节数而非图片路径,导致CSV中存储的是数字而非图片标识,无法正确显示。

修复后的代码

主调用代码(无改动)

$wsdl       = "https://sinex-saas.com/B00UWSUG9Z34TN22/Services/WebServiceStandard.asmx?WSDL";
$client     = new SoapClient($wsdl);
$password   = "????";
$username   = "????";
$reffab     = 4;
$codefab    = 2;
$i          = 0;
$fp         = fopen(plugin_dir_path(__FILE__).'file.csv', 'w+');

$dataRecupererEtatStock     = RecupEtatStock($username, $password, $client);

foreach ($dataRecupererEtatStock as $fields) {
    $dataRecupPhotoProduit  = RecupPhotoProduit($username, $password, $client, $fields->CodeFab, $fields->RefFab, $i);
    $fields->image          = $dataRecupPhotoProduit;

    fputcsv($fp, get_object_vars($fields));
    $i+=1;
}
fclose($fp);

修改后的RecupPhotoProduit函数

function RecupPhotoProduit($pUsername, $pPassword, $pClient, $pCodeFab, $pReffab, $pi) {
    $optionsRecupererPhotoProduit = array(
        'user'      => $pUsername,
        'password'  => $pPassword,
        'codeFab'   => $pCodeFab,
        'refFab'    => $pReffab,
    );

    $responseRecupererPhotoProduit = $pClient->RecupererPhotoProduit($optionsRecupererPhotoProduit);
    
    $array = get_object_vars($responseRecupererPhotoProduit);
    if(empty($array)) {
        return null;
    } 
    
    $base64_img = $responseRecupererPhotoProduit->RecupererPhotoProduitResult;
    $saved_file_path = save_image($base64_img, $pi);
    
    // 返回保存后的图片路径(或根据需求返回文件名)
    return $saved_file_path;
}

修改后的save_image函数

function save_image( $base64_img, $title ) {
    // 移除所有可能的data URI前缀(兼容带前缀/不带前缀的情况)
    $base64_img = preg_replace('#^data:image/[^;]+;base64,#', '', $base64_img);
    // 替换空格为+,避免Base64解码失败
    $base64_img = str_replace(' ', '+', $base64_img);
    
    // 根据Base64开头判断图片格式
    $file_ext = '';
    $first_chunk = substr($base64_img, 0, 4);
    switch($first_chunk) {
        case '/9j/':
            $file_ext = 'jpg';
            break;
        case 'iVBOR':
            $file_ext = 'png';
            break;
        case 'R0lGOD':
            $file_ext = 'gif';
            break;
        default:
            // 无法识别格式时返回null
            return null;
    }
    
    // 解码Base64字符串
    $decoded_data = base64_decode($base64_img);
    if(!$decoded_data) {
        // 解码失败返回false
        return false;
    }
    
    $save_path = plugin_dir_path(__FILE__) . $title . '.' . $file_ext;
    // 写入文件并返回路径(写入成功则返回路径,失败返回false)
    if(file_put_contents($save_path, $decoded_data)) {
        return $save_path;
    } else {
        return false;
    }
}

额外验证步骤

  • 检查WordPress插件目录的写入权限:可添加var_dump(is_writable(plugin_dir_path(__FILE__)));验证,若返回false需调整目录权限。
  • 运行代码后,直接打开生成的图片文件,确认是否能正常显示。
  • 查看CSV文件的image字段,确认存储的是正确的图片路径而非数字。

内容的提问来源于stack exchange,提问作者Gab

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 17:18:25