2013-02-15 49 views
1

我已經在restler框架中創建了webservices,我試圖在我創建的客戶端lib中使用curl在curl中插入數據。如何在PHP中使用Curl進行POST數據?

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

用於測試目的從客戶端: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); 

但它拋出。請幫忙。

+0

可能是它沒有得到url路徑。 – 2013-02-15 10:12:25

+0

我正在使用GET方法獲得成功。 – 2013-02-15 10:14:47

+0

那麼,404只是一個沒有找到答案。你是否檢查過該網址是否正確?在瀏覽器中或從命令行嘗試它。 – Clyde 2013-02-15 10:15:29

回答

0

你不應該映射所有參數的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); 
} 

這樣的API只能接受URL如

http://localhost/Restler/public/examples/news/addcomment/900150983cd24fb0d6963f7d28e17f72/[email protected]/commentusingcurl/2 

更改您的API方法

/** 
* 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); 
} 

然後你的cURL例子應該工作正常

$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); 
+0

hy thanx以確認答案:) – 2013-02-16 11:25:04

1
//curl_setopt($ch, CURLOPT_HTTPHEADER,array("Content-Type : text","Content-lenght:".strlen($data_string))); 

注:取消對線可以是。

使用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的鏈接,瞭解更多信息,

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

編輯:

Restler always returning error 404 not found

可以幫助你。

+0

你有沒有在restler中定義POST參數?我懷疑我可能不正確。 – 2013-02-15 10:24:27

+0

@ketulshah結帳我的答案http://stackoverflow.com/questions/10506060/facebook-curl-posting-as-me/10514420#10514420 – 2013-02-15 10:36:17

+0

@ketulshah我從來沒有使用restler框架,但這不是問題。 – 2013-02-15 10:39:00

相關問題