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 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂