2013-06-02 43 views
0

我正在使用Restler 3庫,我想從php發出一個請求。
我試着這樣說:如何在Restler Client中創建POST請求?

POST操作JSON數據使用PHP捲曲

$data=array('name'=>'ddddd','email'=>'[email protected]');                 
$data_string = json_encode($data);                     

$ch = curl_init('http://192.168.0.101/Restler-3rc3-Stable/public/examples/_007_crud/Authors');                  
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: application/json',                     
    'Content-Length: ' . strlen($data_string))                  
);                             

$result = curl_exec($ch); 

也這樣無捲曲

//Pick your own method.by uncommenting one of the VERBS 
//$method='GET'; 
$method='POST'; 
//$method='PUT'; 
//$method='DELETE'; 


$id=21; 
$response_header=array(); 

//Point this URL to your own service 
$url='http://192.168.0.101/Restler-3rc3-Stable/public/examples/_007_crud/Authors.php'; 


//READ 
if ($method=='GET') 
{ 
    $url=$url.$id; 
    $data_array =array(); 
} 
//CREATE 
if ($method=='POST') 
{ 
    $data_array =array('id'=>$id); 
} 
//UPDATE 
if ($method=='PUT') 
{ 
    $data_array =array('id'=>$id); 
} 
//DELETE 
if ($method=='DELETE') 
{ 
    $url=$url.$id; 
    $data_array =array(); 
} 
$data_array=array('name'=>'ddddd','email'=>'[email protected]'); 
$data = http_build_query($data_array); 
$response=do_request($url,$data,NULL,$method,$response_header); 

//Output the response and some debug info 
echo 'Response Body='. $response; 
echo '<hr><pre>Header= : '; 
var_dump($response_header); 


//Call the the rest service 
function do_request($url, $data, $optional_headers = null , $method='GET', &$var) 
{ 

    $params = array('http' => array(
       'method' => $method, 
       'content' => $data 
      )); 
    if ($optional_headers !== null) { 
    $params['http']['header'] = $optional_headers; 
    } 
    $ctx = stream_context_create($params); 
    $fp = @fopen($url, 'rb', false, $ctx); 

    if (!$fp) { 
     $var=$http_response_header; 
    throw new Exception("Problem with $url, $php_errormsg"); 
    } 
    $response = @stream_get_contents($fp); 
    echo "response".$response; 
    if ($response === false) { 
     $var=$http_response_header; 
    throw new Exception("Problem reading data from $url, $php_errormsg"); 
    } 
    $var=$http_response_header; 
    return $response; 
} 

但既不的兩個工作。 我不知道我是否在這裏錯過了一些東西。我試圖測試示例7「CRUD」 我是否必須將其包含在CRUD示例的index.php中?或者我必須把index.php代碼放在你的例子中?
其中的index.php是:

require_once '../../../vendor/restler.php'; 
use Luracast\Restler\Restler; 

$r = new Restler(); 
$r->addAPIClass('Authors'); 
$r->handle(); 

誰能幫助我通過這個問題?

回答

1
<?php 
$data = array("name" => "Another", "email" => "[email protected]"); 
$data_string = json_encode($data); 

$ch = curl_init('http://restler3.luracast.com/examples/_007_crud/index.php/authors'); 
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: application/json', 
     'Content-Length: ' . strlen($data_string)) 
); 

$result = curl_exec($ch); 
echo($result); 

給你

{ 
    "name": "Another", 
    "email": "[email protected]", 
    "id": 5 
} 
+0

謝謝!現在,當我嘗試獲取它時發佈POST信息我發現新添加的用戶沒有添加到Session DB中。 – SafeY

+0

這是因爲session.php中的安裝函數 – SafeY

相關問題