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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog