首页 > 解决方案 > 在 POST API 中使用 guzzle 时出现问题如何解决?

问题描述

我的框架是 codeigniter,我制作了这样的库:

function checkOut($url, $params){
        $client = new GuzzleHttp\Client([
            'headers' => [ 'Content-Type' => 'application/json' ,'Accept' => 'application/json; charset=utf-8'],
            'verify' => false,
            'cookies' => true
        ]);

        $response = $client->request('POST', $url, [
            'json' => $params
        ]);

        return $response->getBody->getContents();
    }

我在我的控制器中调用这些库,如下所示:

$dataArray is some array was i make,

$response_checkout = $this->corekredivo->checkOut($url, $dataArray)

在视图中,我只是解析为这样查看:

$data = array(
            '_respon' => $response_checkout
        );]

但是,当var_dump();查看结果时:

string(90) "{"status": "ERROR", "error": {"message": "出了点问题。", "kind": "APIException"}}"

在此之前,我尝试过邮递员并成功,并且使用的数组的结构是相同的。

标签: phpjsonguzzle

解决方案


看起来这个字符串 ( "{"status": "ERROR", "error": {"message": "Something went wrong.", "kind": "APIException"}}") 是一个有效的服务器响应。

我的意思是你这边没有错误,你从服务器得到这个响应。服务器应用程序内部出现问题,您收到此消息。

在我看来,您似乎应该意识到这种类型的响应并在您的应用程序中以某种方式处理它们。


推荐阅读