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 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题