douwen7516 2015-01-06 14: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 15: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/

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部