2017-04-26 6 views
0

私は簡単なAPIをSlimで開発しました。これはオンラインサーバーに保存されています。私は、ブラウザからそれをチェックするときには正常に動作しますが、私はiPhoneアプリからそれを取得する場合、それは私に次のエラーが表示さ:iphone&IOSアプリケーションからオンラインスリムAPIにアクセス

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 

私は多くのことを試みたが、無駄にすべて。いずれかがこの問題に直面した場合、またはこれについて知っている場合は、私にこの案内をしてください。前もって感謝します。

私のコードは、あなたがここにあなたのiOSのコードを配置する必要があり

公共/ 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); 

}); 

答えて

1

以下の通りです。あなたのスリムから

、私は置くでしょう:あなたの応答コードで

$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の観点からは、コードなしで何かを言うのは難しいです。

+0

ブラザー私はこのコードをどこに置くべきかわかりません。あなたが私をこの中で導くなら、私はそれを実装することができます。 ** index.php、questions.php、またはiOSコード?** – Skylink

+0

このコードはSlim 3では機能しません**代わりに '$ response-> withJson($ questions); 'を使用すると、 Content-Typeヘッダー、ステータスコード、json_encodeなどのデータが表示されます。 – meun5

+0

これはあなたのPHPコードです。応答のためにエコーを行っているところで、この関数を呼び出します。あなたはあなたの質問にそれを置くことができます – GIJOW

関連する問題