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

连接Zoho CRM与WordPress时,Zoho Rest API返回内容转图片方法咨询

解决Zoho CRM API返回图片二进制数据的显示/转换问题

返回内容是什么?

你看到的乱码是图片的原始二进制数据,比如JPG/PNG这类图片文件的字节流。之所以显示成乱码,是因为你的代码把二进制数据当成文本(字符串)解析输出,而Postman会自动识别响应头里的Content-Type(比如image/jpeg),调用图片渲染器显示内容,所以能正常看到图片。

如何转换为图片?

以WordPress常用的PHP环境为例,处理方式如下:

  1. 正确获取并识别响应头
    先确认API返回的Content-Type头,它会告诉你图片的格式(JPG/PNG等):

    $zohoApiUrl = "你的Zoho文件API请求地址";
    $response = wp_remote_get($zohoApiUrl, [
        'headers' => [
            // 这里带上你的Zoho认证头,比如Authorization: Zoho-oauthtoken 你的令牌
        ]
    ]);
    
    // 获取响应的Content-Type
    $contentType = wp_remote_retrieve_header($response, 'content-type');
    
  2. 直接输出图片到浏览器
    如果要在页面上直接显示图片,设置正确响应头后输出二进制内容即可:

    if (!is_wp_error($response)) {
        header("Content-Type: $contentType");
        echo wp_remote_retrieve_body($response);
        exit; // 停止后续输出,避免破坏图片数据
    }
    
  3. 保存为本地文件
    如果需要把图片保存到WordPress服务器,直接写入文件:

    if (!is_wp_error($response)) {
        $imageData = wp_remote_retrieve_body($response);
        // 生成唯一文件名,比如基于时间戳
        $fileName = wp_upload_dir()['path'] . '/' . time() . '.jpg';
        // 根据Content-Type调整扩展名,比如image/png就用.png
        if (strpos($contentType, 'png') !== false) {
            $fileName = str_replace('.jpg', '.png', $fileName);
        }
        file_put_contents($fileName, $imageData);
    }
    

注意事项

  • 不要对响应体做任何字符串转码操作(比如utf8_encode),这会破坏二进制图片数据。
  • 确保请求时没有设置Accept头为text/plain或application/json,让API返回原始的二进制文件数据。

内容的提问来源于stack exchange,提问作者Mr. Sam

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 01:27:18