how to call curl with get, post, put, delete in php?

In this post we will show you how to call curl with get, post, put, delete, hear for how to call curl with get, post, put, delete we will give you demo and example for implement.

call curl with get, post, put, delete in php

Hear we will show you how to call curl with get, post, put, delete. wh have create one function it will call all 4 curl methods (DELETE, GET, POST, PUT) in a RESTful API.

PHP cURL Download File From Url Example
PHP cURL Download File From Url Example

we have develop a cross-method solution to allow users to test REST API requesting directly from the documentation and get 3 parts of the API response :

HTTP Status Code, Header, Body

through 4 different methods :

DELETE, GET, POST, PUT

Also Read: curl get post

Also we wanted to be able to set what content type I wanted to get (JSON, XML, etc).

Full Source code for call curl with get, post, put, delete

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

Hope this code and post will helped you for implement call curl get post put delete php. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve pakainfo. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs pakainfo.com

Leave a Comment