how to POST data using Curl in php?

喜你入骨 提交于 2019-12-11 06:29:48

问题


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.


回答1:


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);



回答2:


//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.



来源:https://stackoverflow.com/questions/14892309/how-to-post-data-using-curl-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!