douyun8885 2016-06-28 07:38
浏览 40

超薄路线传递到另一个文件返回值

I have a folder structure like this :

|/app 
|-/db/users.php
|/Slim
|/vendor
|index.php

and inside index.php:

require 'vendor/autoload.php';

$app = new Slim\App();

$app->get('/{action}/{type}/{props}', function ($request, $response, $args) {
    $jenis_user = strtolower($args['type']);
    $aksi = strtolower($args['action']);
    $prop = strtolower($args['props']);

    $target_loc = "";

    if($aksi == 'get'){

        // the parameter is using the following format
        // action=XX&type=YY&props=ZZ;
        $target_loc = "./app/db/users.php?action=". $aksi . "&type=" . $jenis_user . "&props=" . $prop ;

        // #Tips :: data extracted are in Array Format ::
        $file = file_get_contents($target_loc);
        $response->write(json_encode($file));

    }

    return $response;
});

My purpose is to get an integer value returned if I use the following path into the browser:

http://api.myweb.com/get/seller/totalNumber

But it seems the way I wrote the routing of passing parameter is mistaken: I got a warning of

Warning: file_get_contents(./app/db/users.php?action=get&type=seller&props=totalnumber): failed to open stream

How could I fix it?

  • 写回答

1条回答 默认 最新

  • 普通网友 2016-06-29 16:32
    关注

    You are trying to get a file with the name users.php?action=get&type=seller&props=totalnumber which doesn't exist, in the file:// protocol, when you really what to do this you need to extra specify the http or https protocol like this:

    $content = file_get_contents('http://example.com/users.php?x=y');
    

    and it will succeed.

    Notice: this is a bad structur, it would be better when you define a function or class for this, it could be like this:

    functions.php

    function getUsers($type, $props) {
        // do your stuff
        return $yourUsers;
    }
    

    index.php or route file

    include 'functions.php'; // include the functions
    
    $app = new Slim\App();
    $app->get('/{action}/{type}/{props}', function ($request, $response, $args) {
        $jenis_user = strtolower($args['type']);
        $aksi = strtolower($args['action']);
        $prop = strtolower($args['props']);
    
        if($aksi == 'get'){
    
            $users = getUsers($jenis_user, $prop); // execute the function to collect the user
            $response->write(json_encode($users ));
        }
    
        return $response;
    });
    

    Then you wouldn't need the file_get_contents().

    评论

报告相同问题?

悬赏问题

  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿