RESTp - Update Example Rest API to perform update operation

RESTp update function provides a simplest way to update the data into database by passing the data in form of associative array. You can easily update a record using the PUT method and by passing data needs to be updated. PUT APIs are primarily to update existing resource. If you perform a `PUT` request, the server updates an entry in the database and tells you whether the update is successful. In other words, a `PUT` request performs an `UPDATE` operation. On success, it returns the number of records updated.

Please note that if you are sending data as json format using json_encode function then you must specify the content type header ('Content-Type:application/json') as default content type is post array. Also, you must specify the where condition if you intend to update a single resource else all resources will be updated. You can pass where condition either in data array or in url.

  
                            //Dummy data to update. Please note the format of data
                            // without where key - in this case you need to pass it in url i.e. end point
                            $data = array("data" => array("order_date" => date("Y-m-d"), "order_amount" => 600, "order_status" => "Completed"));
                            // with where key - in this case you don't need to pass it in url i.e. end point
                            //$data = array("data" => array("order_date" => date("Y-m-d"), "order_amount" => 60, "order_status" => "Completed"),
                            //     "where" => array('order_no,2222,"eq"'));
                            //Option 1: Convert data array to json if you want to send data as json
                            $data = json_encode($data);
                            //Option 2: else send data as post array.
                            //$data = urldecode(http_build_query($data));
                            /****** curl code ****/
                             //init curl
                            $ch = curl_init();
                            // URL to be called
                            curl_setopt($ch, CURLOPT_URL, "http://pdocrud.com/RESTP/api/orders/42");
                            //set http headers - if you are sending as json data (i.e. option 1) else comment this 
                            curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
                            //set CURLOPT_CUSTOMREQUEST =  PUT to do a  HTTP PUT
                            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
                            //send post data
                            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
                            //return as output instead of printing it
                            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                            //execute curl request
                            $result = curl_exec($ch);
                            //close curl connection
                            curl_close($ch);
                            //print result
                            print_r($result);
                        
{"message":"Error","error":[null],"data":0}