Reputation: 423
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 protected]&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.
Upvotes: 1
Views: 4529
Reputation: 8094
//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.
Upvotes: 1
Reputation: 993
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/[email protected]/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 protected]&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);
Upvotes: 0