没有不值得去解决的问题,也没有不值得去学习的技术!

How to Determine Success or Failure When the Get Unlimited Mini Program Code API Responds With an Image Buffer or JSON String

When the response is not an image Buffer, the data written to the image file is incorrect. The image cannot be opened. I finally decided to distinguish the response type using the Content-Type response header

1. The PHP code implementation is as follows:

PHP

    /**
     * 获取不限制的小程序码
     * @param $accessToken
     * @param $scene
     * @param $page
     * @param $width
     * @return mixed
     * @throws ServerErrorHttpException
     */
    public function getUnlimitedQRCode($accessToken, $scene, $page = null, $width = 430)
    {
        $response = $this->httpClient->createRequest()
            ->setMethod('POST')
            ->setUrl('wxa/getwxacodeunlimit?access_token=' . $accessToken)
            ->setFormat(Client::FORMAT_JSON)
            ->setData([
                'scene' => $scene,
                'page' => $page,
                'width' => $width,
                'check_path' => false,
                'env_version' => 'trial'
            ])
            ->send();

        // 设置响应格式为 raw-urlencoded(二进制数据)
        $response->format = Client::FORMAT_RAW_URLENCODED;

        // 检查响应状态码是否等于20x
        if ($response->isOk) {
            // 检查业务逻辑是否成功
            if (isset($response->data['errcode']) && isset($response->data['errcode'])) {
                throw new ServerErrorHttpException('获取不限制的小程序码失败,errcode:' . $response->data['errcode'] . ',errmsg:' . $response->data['errmsg'], 20021);
            }
        } else {
            throw new ServerErrorHttpException('获取不限制的小程序码失败,status_code:' . $response->statusCode, 20022);
        }

        return $response->content;
    }

2. When the response is not an image Buffer, the data written to the image file is incorrect. The image cannot be opened. I finally decided to distinguish the response type using the Content-Type response header. See Figure 1.

When the response is not an image Buffer, the data written to the image file is incorrect. The image cannot be opened. I finally decided to distinguish the response type using the Content-Type response header
Figure 1
Plaintext

application/json; encoding=utf-8
image/jpeg

PHP

    /**
     * 获取不限制的小程序码
     * @param $accessToken
     * @param $scene
     * @param $page
     * @param $width
     * @return mixed
     * @throws ServerErrorHttpException
     */
    public function getUnlimitedQRCode($accessToken, $scene, $page = null, $width = 430)
    {
        $response = $this->httpClient->createRequest()
            ->setMethod('POST')
            ->setUrl('wxa/getwxacodeunlimit?access_token=' . $accessToken)
            ->setFormat(Client::FORMAT_JSON)
            ->setData([
                'scene' => $scene,
                'page' => $page,
                'width' => $width,
                'check_path' => false,
                'env_version' => 'trial'
            ])
            ->send();

        // 检查响应状态码是否等于20x
        if ($response->isOk) {
            $contentType = $response->getHeaders()->get('Content-Type');
            if (strpos($contentType, 'image/') !== false) {
                return $response->content;
            } elseif (strpos($contentType, 'application/json') !== false && isset($response->data['errcode']) && isset($response->data['errcode'])) {
                throw new ServerErrorHttpException('获取不限制的小程序码失败,错误码:' . $response->data['errcode'] . ',错误信息:' . $response->data['errmsg'], 20021);
            } else {
                // 未知响应类型
                throw new ServerErrorHttpException('获取不限制的小程序码失败,未知响应类型:' . $contentType, 20023);
            }
        } else {
            throw new ServerErrorHttpException('获取不限制的小程序码失败,HTTP 状态码:' . $response->statusCode, 20022);
        }
    }

3. When the response is not an image Buffer, yii\web\ServerErrorHttpException: Failed to get unlimited mini program code, error code: 40001, error message: invalid credential. See Figure 2.

When the response is not an image Buffer, yii\web\ServerErrorHttpException: Failed to get unlimited mini program code, error code: 40001, error message: invalid credential
Figure 2
Plaintext
{
    "code": 10000,
    "message": "生成微信小程序二维码成功",
    "data": {
        "url": "/mini-program-qr-codes/644d4a4c0fff836ec9ac17d86d0911aa.png"
    }
}


yii\web\ServerErrorHttpException: 获取不限制的小程序码失败,错误码:40001,错误信息:invalid credential, access_token is invalid or not latest, could get access_token by getStableAccessToken, more details at https://mmbizurl.cn/s/JtxxFh33r rid: 67d90e17-47a89f95-26c1c8de 

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系