首页 > 解决方案 > 查询 API 返回空响应

问题描述

大家好,我与 xampp 和 PHPMyAdmin 一起工作了几个月,以从本地主机上的 MySQL 数据库发送和获取数据,从那一周开始,当我发送请求时,我得到了一个空的正文响应,我不知道为什么我没有t 更改我的 PHP 代码中的任何内容,让我感到沮丧的是 Select 请求 API 无法正常工作,其他所有工作正常更新、插入......当我在 MySQL 数据库上运行相同的查询时,它工作正常我会很高兴从你们那里得到一些帮助。这是我在 index.php 中的代码

                $db = new DbOperation();
                                $Bars = $db->Bars_Listing_name_adress();
            
                if(count($Bars)<=0){
                    $response['error'] = true; 
                    $response['message'] = 'Nothing found in the database';
                }else{
                    $response['error'] = false; 
                    $response['Bars'] = $Bars;
                }
            break; 

这是 dboperations.php 中的代码



    //get the list of the bars name and adresse for the api 
    public function Bars_Listing_name_adress(){
        $stmt = $this->con->prepare("SELECT Bar_Name,Adress,Zip_code,City,Country FROM Bars");
        $stmt->execute();
        $stmt->bind_result($Bar_Name,$Adress,$Zip_code,$City,$Country );
        $Bars = array();
        
        while($stmt->fetch()){
            $temp = array(); 
            
            $temp['Bar_Name'] = $Bar_Name; 
                    $temp['Adress'] = $Adress; 
            $temp['Zip_code'] = $Zip_code; 
                    $temp['City'] = $City;
                    $temp['Country'] = $Country;
        
            array_push($Bars, $temp);
        }
        return $Bars; 

当我在 Postman 中调用它时

http://localhost/WebApi/v1/?op=Bars_Listing_name_adress

我得到一个空洞的回应,这很奇怪

POST
http://localhost/WebApi/v1/?op=Bars_Listing_name_adress&=
16:17:11.176
Pretty
Raw
Request Headers:
Content-Type:"application/x-www-form-urlencoded"
cache-control:"no-cache"
Postman-Token:"1093116d-58aa-4933-a571-99e42c0b203d"
User-Agent:"PostmanRuntime/7.6.0"
Accept:"*/*"
Host:"localhost"
accept-encoding:"gzip, deflate"
content-length:""
Response Headers:
Date:"Sat, 31 Oct 2020 15:17:11 GMT"
Server:"Apache/2.4.43 (Unix) OpenSSL/1.1.1g PHP/7.3.16 mod_perl/2.0.8-dev Perl/v5.16.3"
X-Powered-By:"PHP/7.3.16"
Content-Length:"0"
Keep-Alive:"timeout=5, max=100"
Connection:"Keep-Alive"
Content-Type:"text/html; charset=UTF-8"
Response Body:

但是当我在同一个 PHP 文件中调用任何其他请求时,它可以工作,所以问题只发生在没有 where 子句的 select 上,我很乐意从你们那里得到一些帮助,谢谢!

标签: phpxampppostman

解决方案


感谢您的回答。我刚刚解决了这个问题。这是一个解码问题,我用以下方法解决了它utf8_encode

function utf8ize($d) {
    if (is_array($d)) {
        foreach ($d as $k => $v) {
            $d[$k] = utf8ize($v);
        }
    } else if (is_string ($d)) {
        return utf8_encode($d);
    }
    return $d;
}
echo json_encode(utf8ize($response));
    //echo json_encode($response);


推荐阅读