ios - 从 iphone 和 IOS 应用程序访问在线 Slim API

标签 ios iphone slim-3

我在Slim中开发了简单的API,它存储在在线服务器中。当我从浏览器检查它时它工作正常,但是当我从 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 的角度来看,没有代码很难说什么。

关于ios - 从 iphone 和 IOS 应用程序访问在线 Slim API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43639990/

相关文章:

iphone - uicollection View 可重用单元格图像在滚动时重新加载

iphone - 保存和恢复 UIWebView 浏览 session

ios - 无法发现 iPhone 个人热点

php - Slim 3 框架 - 将数据从中间件传递到 Controller - Action 参数

php - Eloquent::一对一,在两个表中均有效,但具有特定角色

php - Slim 3 在中间件中获取当前路由

ios - NotificationCenter 在从 viewcontroller 发布到另一个 ViewController 时第一次不工作

iphone - 从主视图 Controller 调用应用程序委托(delegate)文件方法

iphone - 如何绘制 UITableViewCell 的 contentView?

iphone - 你如何只在 UIView 的填充之外显示阴影?