我有使用Slim开发的简单API,该API存储在Online Server中。当我从浏览器中检查它时,它工作正常,但是当我从Iphone应用程序中检索它时,它显示以下错误:

PeopleAlsoAsk[12607:2219389] Error Domain=AFNetworkingErrorDomain Code=-1011 "Expected status code in (200-299), got 500" UserInfo={NSLocalizedRecoverySuggestion="this is index pagpe, Specific questions are retrieved successfully", NSErrorFailingURLKey=http://upvc.pk/test2/public/, AFNetworkingOperationFailingURLRequestErrorKey=<NSMutableURLRequest: 0x608000001060> { URL: http://upvc.pk/test2/public/ }, AFNetworkingOperationFailingURLResponseErrorKey=<NSHTTPURLResponse: 0x60800003d6c0> { URL: http://upvc.pk/test2/public/ } { status code: 500, headers {
    "Accept-Ranges" = bytes;
    Connection = "Keep-Alive";
    "Content-Encoding" = gzip;
    "Content-Length" = 81;
    Date = "Wed, 26 Apr 2017 12:34:31 GMT";
    Server = LiteSpeed;
    Vary = "Accept-Encoding";
    "X-Powered-By" = "PHP/7.0.17";
} }, NSLocalizedDescription=Expected status code in (200-299), got 500}
2017-04-26 17:34:31.462 PeopleAlsoAsk[12607:2219389] Error function called


我尝试了很多,但都徒劳无功。如果有人遇到此问题或对此有所了解,请在此方面指导我。提前致谢。

我的代码如下

public / index.php

<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

require '../vendor/autoload.php';

$app = new \Slim\App;

//Questions Routes
require '../src/routes/questions.php';

$app->run();


路线/questions.php

<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

$app = new \Slim\App;

//Get All Questions
$app->get('/api/questions', function(Request $request, Response $response){

$questions = "All questions are retrieved successfully";

        echo json_encode($questions);

});

// Get specific Questions
$app->get('/api/questions/{app_id}', function(Request $request, Response $response){

$questions = "Specific questions are retrieved successfully";

echo json_encode($questions);

});

最佳答案

您应该将iOS代码放在这里。

在您的Slim中,我将放置:

$app->status($status_code);


在您的响应代码中,您可以添加以下功能以提供所有客户端响应:

function echoResponse($status_code, $response) {
    $app = \Slim\Slim::getInstance();
    // Http response code
    $app->status($status_code);
    // setting response content type to json
    $app->contentType('application/json');
    echo json_encode($response);
}


用法很简单:

echoResponse(200, "your response");


从iOS的角度来看,没有代码很难说些什么。

10-07 23:11