dsgixl5195
2013-02-15 10:08 阅读 31
已采纳

如何在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 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);
    
    点赞 评论 复制链接分享
  • dongyun6003 dongyun6003 2013-02-15 10:20
    //curl_setopt($ch, CURLOPT_HTTPHEADER,array("Content-Type : text","Content-lenght:".strlen($data_string)));
    

    NOTE: Uncomment the line may be.

    See the example of post data using curl.

    function post($requestJson) {
    
            $postUrl = $this->endpoint."?access_token=".$this->token;
    
            //Get length of post
            $postlength = strlen($requestJson);
    
            //open connection
            $ch = curl_init();
    
            //set the url, number of POST vars, POST data
            curl_setopt($ch,CURLOPT_URL,$postUrl);
            curl_setopt($ch,CURLOPT_POST,$postlength);
            curl_setopt($ch,CURLOPT_POSTFIELDS,$requestJson);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    
            $response = curl_exec($ch);
    
            //close connection
            curl_close($ch);
    
            return $response;
        }
    

    Checkout some links for more information,

    Execute a HTTP POST Using PHP CURL

    Submitting a form post with PHP and CURL

    Post to a URL Using cURL and PHP

    Setup a simple cURL connection to POST data using PHP

    Edit:

    Restler always returning error 404 not found

    may help you.

    点赞 评论 复制链接分享

相关推荐