I have a page wherein it will serve as a for that is connected to my API.php. In this page I used Curl PUT verb and passed the URL and the data I need to pass which is in the form of an array.
I tried catching the response in the side of the API but to no avail.
Here's my code.
The URL they need to populate or I think this is the API
http://localhost/sample/index.php/2/users/mil/milastname/09876543212
Legend:
Sample - is the folder where I stored my index.php
2 - is the action. In this case 2 stands for PUT verb.
users - is the table in the database which I want to update.
mil - is my firstname
milastname - is my lastname
09876543212 - is my mobile
MY FORMS
index.php
$request = explode('/', trim($_SERVER['PATH_INFO'],'/'));
if ($request[0] == 2) {
$data = array(
"table" => $request[1],
"fname" => $request[2],
"lname" => $request[3],
"cntct" => $request[4]
);
$url_send ="http://localhost/api.php";
$str_data = json_encode($data);
$post_elements = array('data'=>$data);
if ($request[0] == 2) {
put($url_send, $post_elements);
echo put($url_send, $post_elements);
}
function put($url, $fields)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query($fields));
echo curl_setopt( $ch, CURLOPT_POSTFIELDS, http_build_query($fields));
$response = curl_exec($ch);
curl_close ($ch);
return $response;
}
Here's my API.php
if ($method == 'PUT') {
$data = preg_replace('/[^a-z0-9_]+/i','',array_shift($_POST));
echo $data;
die();
}
switch ($method) {
case 'PUT':
$sql = "update `$table` set $set where id=$key"; break;
case 'POST':
$sql = "insert into `$table` (user_fname, user_lname, user_contact) values ('$fname', '$lname', '$cntct')"; break;
case 'DELETE':
$sql = "delete `$table` where id=$key"; break;
}
$result = mysqli_query($link,$sql);
In the part of if ($method == 'PUT')
I tried inserting an echo and a die to stop the process to that I can see the array So I can arrange the update query in my method case update. But it returns no result.
Thanks for the help and sorry for the long post. Need to detail it so that I can explain my question clearly.
Thanks.