Crud API发布,放置和删除

时间:2017-06-06 05:24:29

标签: php json api crud

我正在尝试这个PHP-CRUD api,但是如何使用它来发布?是通过以下网址:

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

1 个答案:

答案 0 :(得分:0)

json_encode看起来是正确的,如果有任何问题,请面对一些问题吗?