2017-06-06 30 views
0

我正在嘗試這個PHP-CRUD API,但是如何使用它來發布?是否通過URL如下:Crud API post,放入並刪除

localhost/hris/api.php/employee_details/create?id=2&name=john 

或者我用錯了它?

還是漫長的路?

$url = 'http://localhost/hris/api.php/login'; 

//Initiate cURL. 
$ch = curl_init($url); 

//The JSON data. 
$jsonData = array(
    'emp_id' => '2017-0007', 
    'username' => 'jabril', 
    'password' => 'Donor2017', 
    'type' => 'admin' 
); 


$jsonDataEncoded = json_encode($jsonData); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); 

$result = curl_exec($ch); 

我也試過這個

require('crud.php'); 

function call($method, $url, $data = false) { 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); 
curl_setopt($ch, CURLOPT_URL, $url); 
if ($data) { 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
    $headers = array(); 
    $headers[] = 'Content-Type: application/json'; 
    $headers[] = 'Content-Length: ' . strlen($data); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
} 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
return curl_exec($ch); 
} 

$object = array('emp_id'=>'2017-0007','username'=>'jabril','password'=>'Donor2017','type'=>''); 
$posting = call('POST', 'http://localhost/api.php/login',json_encode($object)); 
+0

首先驗證是否API是POST或GET版本。那麼,你可以檢查並看看會發生什麼?如果出現錯誤,請將其發佈到此處。那麼我們可以看到 –

回答

0

json_encode看起來是正確的,你面臨着一些問題,如果有的話,請分享..

+2

它應該在評論 – varuog

+0

是的,我使用這個api https://github.com/mevdschee/php-crud-api我可以在網址上使用POST方法嗎? – TheTruth

+0

@varuog是對的,你應該學習這個https://stackoverflow.com/help/how-to-answers –