V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Famio
V2EX  ›  程序员

请教,这种数据接口怎么写?

  •  
  •   Famio · 2017-09-18 13:26:12 +08:00 · 3294 次点击
    这是一个创建于 2404 天前的主题,其中的信息可能已经有所发展或是发生改变。

    给的 demo 是:xxx.com/api/?id=123,抛出 json 数据格式。

    我只会写:xxx.com/api/index.php?id=123,用的 POST 方法,不会抛出 json 数据格式。

    有没有大神教我一下,随便给个例子就行,我一看就懂。

    第 1 条附言  ·  2017-09-18 14:10:18 +08:00
    谢谢各位,发现光 echo 出来没用,还要加 header('content-type:application/json');改变下头
    20 条回复    2017-09-18 22:24:57 +08:00
    inmyfree
        1
    inmyfree  
       2017-09-18 13:30:25 +08:00
    你需要学习下.htaccess 规则
    icedx
        2
    icedx  
       2017-09-18 13:31:46 +08:00
    你需要学习下nginx.conf 的写法
    Famio
        3
    Famio  
    OP
       2017-09-18 13:33:31 +08:00
    @inmyfree @icedx 我知道你们的意思,但是我不是要 rewrite。重点在于如何抛出 json,我不会这块。
    sculley
        4
    sculley  
       2017-09-18 13:34:20 +08:00
    demo 的 url 要有路由才行,要不是 nginx 配置,要不就是 index.php 入口后自己写路由判定,然后 call function
    littleylv
        5
    littleylv  
       2017-09-18 13:35:47 +08:00
    如何抛出 json ?最简单暴力的:

    $arr = [
    'a' => 'aaa',
    'b' => 'bbb'
    ];
    echo json_encode($arr);
    exit(0);

    当然这么写一点都不优雅
    inmyfree
        6
    inmyfree  
       2017-09-18 13:36:00 +08:00
    @Famio #3

    抛出 json ?

    php 直接 echo 啊,想要啥 格式就啥格式啊
    sculley
        7
    sculley  
       2017-09-18 13:36:52 +08:00
    再加个 header ContentType
    pathletboy
        8
    pathletboy  
       2017-09-18 13:37:03 +08:00   ❤️ 5
    不小心点开了楼主的示例网站,然后就。。。
    inmyfree
        9
    inmyfree  
       2017-09-18 13:37:59 +08:00
    你要理解,http 返回的其实都是文本,里面内容你自己写就可以,要 html 就输入 <html>....要 json 就直接 echo,一样的
    inmyfree
        10
    inmyfree  
       2017-09-18 13:38:59 +08:00
    @pathletboy #8 额...这就尴尬了,不说我还不知道,xxx.com 。。。默哀,前后左右还有妹子吗
    chuhemiao
        11
    chuhemiao  
       2017-09-18 13:51:29 +08:00
    json_encode 解决不了问题?
    Immortal
        12
    Immortal  
       2017-09-18 14:05:34 +08:00
    我草...在公司不小心点开了
    zjp
        13
    zjp  
       2017-09-18 14:08:03 +08:00 via Android
    惨案,不只一两次了…示例网站用 example.com 不要用 xxx.com🌚
    KomeijiSatori
        14
    KomeijiSatori  
       2017-09-18 14:10:31 +08:00
    ![photo_2017-09-18_14-09-24.jpg]( https://i.loli.net/2017/09/18/59bf635007b90.jpg)
    kltt22
        15
    kltt22  
       2017-09-18 14:27:36 +08:00
    index.php 是默认页面啊,可以忽略。echo 不加头也可以返回正确 json,我一直是这样做的。
    scofieldpeng
        16
    scofieldpeng  
       2017-09-18 14:34:12 +08:00
    php 抛出 json 不是就是 json_encode?然后 header 加上 application/json 就行了?遇到问题第一先 google
    paw
        17
    paw  
       2017-09-18 14:36:19 +08:00
    靠。。。。。。好奇心杀死猫
    scofieldpeng
        18
    scofieldpeng  
       2017-09-18 14:36:38 +08:00
    @kltt22 有时候客户端比如浏览器,如果不加有些前端框架不会认为是 json,因此严格来讲,最好加上 json 的 header 头
    yanzixuan
        19
    yanzixuan  
       2017-09-18 14:38:17 +08:00
    @inmyfree 不一定,之前跟 java 的哥们联调我的接口的时候,直接报错。如果 header 没有指定 json 的话,java 的对应库会比较严格,不认为是个 json...
    wizardoz
        20
    wizardoz  
       2017-09-18 22:24:57 +08:00
    提问请讲清楚问题描述,比如以多大的初速度和多大角度抛出 json ?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3324 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 11:45 · PVG 19:45 · LAX 04:45 · JFK 07:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.