0

I have simple API developed in Slim, which is stored in Online Server. It works fine when I check it from browser, but when I retrieve it from Iphone app, it show me the following error:

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

I tried a lot, but all in vain. If any one faced this problem, or know about this, then please guide me in this. Thanks in advance.

My code is given below

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();

routes/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);

});
Skylink
  • 83
  • 1
  • 9

1 Answers1

1

You should put your iOS code here.

From your Slim, I would place:

$app->status($status_code);

In your response code, you can add this function to give all client response:

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);
}

Usage very simple:

echoResponse(200, "your response"); 

From iOS point of view, hard to say something without the code.

GIJOW
  • 2,307
  • 1
  • 17
  • 37
  • Brother I don't know where to put this code. If you guide me in this, then I will be able to implement it. **In index.php, questions.php or iOS code?** – Skylink Apr 27 '17 at 12:10
  • This code will **not** work for Slim 3. Instead you could use `$response->withJson($questions);` which will set the Content-Type header, status code, and also json_encode the data for you. – meun5 Apr 27 '17 at 14:39
  • This is your PHP code. Call this function where you are doing your echo for response. You can place it in your questions – GIJOW Apr 27 '17 at 15:30