douwen7516 2015-01-06 22:45
浏览 51
已采纳

php中的Slim API POST

I made an API through Slim framework and I was testing it using the Advanced Rest Client. Now I want to implement in my php website the API rest. How Can I do that? Is better use AJAX or PHP for that? Can you give a small code demo?

Thanks in advance, I really appreciate.

I tried this code,is this the best solution: My problem here is that I need to add a header with an authorization key.

$data = array("title" => "otro libro", "isbn" => "998-84-8184-1", "author" => "otro autor :)");
    $ch = curl_init("http://localhost/slimrest/books");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
    $response = curl_exec($ch);
    curl_close($ch);
    if(!$response) 
    {
        return false;
    }
    else
    {
        var_dump($response);
    }
  • 写回答

1条回答 默认 最新

  • douji8033 2015-01-06 23:37
    关注

    You want to use AJAX. I guess its popular to use a javascript framework like jquery or angular.

    You can then just pass your token along as a part of the body. I am most familar with jquery so heres a link to the docs.

    http://api.jquery.com/jquery.post/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程