PHP Rest API And HTTP CURL methods GET, POST, DELETE, PUT

PHP Rest API And HTTP CURL methods GET, POST, DELETE, PUT

In this Post We Will Explain About is PHP Rest API And HTTP CURL methods GET, POST, DELETE, PUT With Example and Demo.Welcome on Pakainfo.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to How to make REST calls in PHP Example

In this post we will show you Best way to implement PHP + curl, HTTP POST sample code, hear for php curl get request with parameterswith Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.

Hear we will show you how to call Rest API And HTTP CURL with get methods, post methods, put methods, delete methods. We have create one function it will call all 4 Curl methods (Like as a DELETE, GET, POST, PUT) in a RESTful API.

Also we wanted to be able to all the methods set what content type I simple wanted to get Like as a(JSON, XML, etc).

Simple Curl 4 different methods :
1_ : DELETE,
2_ : GET,
3_ : POST,
4_ : PUT.

Rest API And HTTP Methods (GET, POST, PUT, DELETE) using PHP

we have develop a HTTPS cross-method solution to simple allow access origin users to simple test REST API requesting all the user directly from the documentation and get simple 3 parts of the API switch response :

function CallAPI($api_method, $curl_url, $res_data) {
     
    $my_curl = curl_init($url);
    curl_setopt($my_curl, CURLOPT_RETURNTRANSFER, true);
    // if api with HTTPS
    curl_setopt($my_curl, CURLOPT_SSL_VERIFYPEER, false);
 
    switch ($api_method) 
    {
        case "GET":
            curl_setopt($my_curl, CURLOPT_POSTFIELDS, json_encode($res_data));
            curl_setopt($my_curl, CURLOPT_CUSTOMREQUEST, "GET");
            break;
        case "POST":
            curl_setopt($my_curl, CURLOPT_POSTFIELDS, json_encode($res_data));
            curl_setopt($my_curl, CURLOPT_CUSTOMREQUEST, "POST");
            break;
        case "PUT":
            curl_setopt($my_curl, CURLOPT_POSTFIELDS, json_encode($res_data));
            curl_setopt($my_curl, CURLOPT_CUSTOMREQUEST, "PUT");
            break;
        case "DELETE":
            curl_setopt($my_curl, CURLOPT_CUSTOMREQUEST, "DELETE"); 
            curl_setopt($my_curl, CURLOPT_POSTFIELDS, json_encode($res_data));
            break;
    }
    $curl_response = curl_exec($my_curl);
    $res_data = json_decode($curl_response);
 
    /* 
    * Pakainfo.com - Check for 404 (any file not found). 
    */
    $statusHtppCode = curl_getinfo($my_curl, CURLINFO_HTTP_CODE);
    //Pakainfo.com - Check the HTTP Status code
    switch ($statusHtppCode) {
        case 200:
            $curl_status_err = "200: Success";
            return ($res_data);
            break;
        case 404:
            $curl_status_err = "404: API Not found";
            break;
        case 500:
            $curl_status_err = "500: servers replied with an error.";
            break;
        case 502:
            $curl_status_err = "502: servers may be down or being upgraded. sorry Hopefully they'll be OK soon!";
            break;
        case 503:
            $curl_status_err = "503: service unavailable. sorry Hopefully they'll be OK soon!";
            break;
        default:
            $curl_status_err = "Undocumented error: sorry" . $statusHtppCode . " : " . curl_error($my_curl);
            break;
    }
    curl_close($my_curl);
    echo $curl_status_err;
    die;
}
 
//Devloped By Pakainfo.com : CALL Get Method
$api_data = array('id'=>$_GET['eid']);
$curl_url = "www.Pakainfo.com/GetCategoryById";
$result = CallAPI('GET', $curl_url, $api_data);
 
//Devloped By Pakainfo.com: CALL Post Method
$curl_url = "www.Pakainfo.com/InsertCategory";
$api_data = array('title'=>$_REQUEST['content_data'],'description'=>$_REQUEST['content_data']);
$result = CallAPI('POST', $curl_url, $api_data);
 
//Devloped By Pakainfo.com CALL Put Method
$curl_url = "www.Pakainfo.com/UpdateCategory";
$api_data = array('id'=>$_REQUEST['content_data'],m'title'=>$_REQUEST['content_data'],'description'=>$_REQUEST['content_data']);
$result = CallAPI('POST', $curl_url, $api_data);
 
//Devloped By Pakainfo.com : CALL Delete Method
$curl_url = "www.Pakainfo.com/DeleteCategory";
$api_data = array('id'=>$_GET['did']);
$result = CallAPI('DELETE', $curl_url, $api_data);

Example

I hope you have Got What is Execute a HTTP POST Using PHP CURL And how it works.I would Like to have FeadBack From My Blog(Pakainfo.com) readers.Your Valuable FeadBack,Any Question,or any Comments abaout This Article(Pakainfo.com) Are Most Always Welcome.


Related FAQ

Here are some more FAQ related to this Article:

  1.   Wordpress Advanced Search query custom fields
  2.   Laravel 6 Sweet Alert Tutorial With Example
  3.   SQL SELECT SUM on multiple INNER JOIN with another table
  4.   Laravel 5.6 csrf token with Blade Directives
  5.   Laravel resource route multiple parameters Example
  6.   Simple Model Popup Box using HTML CSS and JavaScript
  7.   PHP cURL Http Authorization Pass Header Examples
  8.   Simple Laravel CRUD Validation Tutorial
  9.   SQL Inner, Left, Right and Full Joins
  10.   php - SMTP Send mail using PHPMailer PHP Contact Form