dsgixl5195 2013-02-15 10:08
浏览 32
已采纳

如何在php中使用Curl发布数据?

I have created webservices in restler framework and I am trying to insert data using curl in php from client lib that I have created.

api.php

/**
     * Manually routed method. we can specify as many routes as we want
     *
     * @url POST addcomment/{token}/{email}/{comment}/{story_id}
     */
    function addComment($token,$email,$comment,$story_id){
        return $this->dp->insertComment($token,$email,$comment,$story_id);
    }

for testing purpose from client : testing.php

$data_string = "token=900150983cd24fb0d6963f7d28e17f72&email=kamal@gmail.com&comment=commentusingcurl&story_id=2";                                                                                   

$ch = curl_init('http://localhost/Restler/public/examples/news/addcomment');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
//curl_setopt($ch, CURLOPT_HTTPHEADER,array("Content-Type : text","Content-lenght:".strlen($data_string)));                                                                                                                                     
$result = curl_exec($ch);

but it's throwing 404. Please help.

  • 写回答

2条回答 默认 最新

  • dpfps86064 2013-02-16 07:08
    关注

    You should not be mapping all the parameters to URL,

    /**
     * Manually routed method. we can specify as many routes as we want
     *
     * @url POST addcomment/{token}/{email}/{comment}/{story_id}
     */
    function addComment($token,$email,$comment,$story_id){
        return $this->dp->insertComment($token,$email,$comment,$story_id);
    }
    

    By doing so the API can only accept URL such as

    http://localhost/Restler/public/examples/news/addcomment/900150983cd24fb0d6963f7d28e17f72/kamal@gmail.com/commentusingcurl/2
    

    Change you API method to

    /**
     * Manually routed method. we can specify as many routes as we want
     *
     * @url POST addcomment
     */
    function addComment($token,$email,$comment,$story_id){
        return $this->dp->insertComment($token,$email,$comment,$story_id);
    }
    

    Then your cURL example should work fine

    $data_string = "token=900150983cd24fb0d6963f7d28e17f72&email=kamal@gmail.com&comment=commentusingcurl&story_id=2";
    
    $ch = curl_init('http://localhost/Restler/public/examples/news/addcomment');                                                                      
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
    //curl_setopt($ch, CURLOPT_HTTPHEADER,array("Content-Type : text","Content-lenght:".strlen($data_string)));                                                                                                                                     
    $result = curl_exec($ch);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器