我正在嘗試這個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));
首先驗證是否API是POST或GET版本。那麼,你可以檢查並看看會發生什麼?如果出現錯誤,請將其發佈到此處。那麼我們可以看到 –