PHP cURL API calls POST GET PUT DELETE RESTful CRUD

PHP cURL API calls POST GET PUT DELETE RESTful CRUD</h2

Today, We want to share with you PHP cURL API calls POST GET PUT DELETE RESTful CRUD.
In this post we will show you cURL API calls with PHP and json data (GET – POST – PUT – DELETE), hear for RESTful CRUD using GET, POST, PUT and DELETE we will give you demo and example for implement.
In this post, we will learn about Rest API And HTTP Methods (GET, POST, PUT, DELETE) with an example.

Good And Easy my first release of Configuring CORS for the REST API PhpRestful API Doc I had to Source code develop a Enabling Cross Origin Requests for a RESTful Web Service cross-method solution to allow users Access-Control-Allow-Origin – HTTP to test REST cURL API calls with PHP and json data requesting directly from the documentation and Fetch Main 3 parts of the API response :

Syntax : Access-Control-Allow-Origin
[php]
Access-Control-Allow-Origin: *
Access-Control-Allow-Origin:
[/php]

Handling CORS on the server CORS and caching

  • Header
  • Body
  • HTTP Status Code
  • How to use PHP 4 different methods :

    • DELETE,
    • GET,
    • POST,
    • PUT.

    Also I needed to be able to customize CURL API CALLS WITH PHP MySQLi AND JSON DATA (GET POST PUT DELETE) the desired Data content type to Retrive response (JSON, CSV, XML, etc).

    [php]
    $get_data = callAPI(‘GET’, ‘https://api.infinityknow.com/get_url/’.$user[‘User’][‘user_id’], false);
    $dataresults = json_decode($get_data, true);
    $errors = $dataresults[‘dataresults’][‘errors’];
    $data = $dataresults[‘dataresults’][‘data’][0];
    [/php]

    Here is the commented source code that I wrote to achieve that :

    PHP API Request Url

    Set the call data Request Url (without any Parameters) set here

    [php]
    $web_services_req_url = ‘http://api.infinityknow.com/user/info/’;
    [/php]

    PHP cURL DELETE, GET, POST or PUT request and response Example

    Which API Request Method do I want to use ? Like as a ( DELETE, GET, POST or PUT )

    config.php

    Let’s first of all set all Request Like as a config.php Parameters (api_key, token, user_id, etc)

    [php]
    $web_services_name = ‘GET’;

    $web_services_param = array(
    ‘api_key’ => ‘gF7mcpakaInfodotcom726sL’,
    ‘token’ => ‘hnFGkrupalift8GE8AVTjaydeeporgthinfiNityKnowt’,
    ‘user_id’ => 9898
    );
    [/php]

    PHP Method DELETE, GET, POST or PUT Example

    DELETE, GET, POST or PUT Method PHP API

    [php]
    $liveCurl = curl_init();
    curl_setopt($liveCurl, CURLOPT_RETURNTRANSFER, TRUE);

    if ($web_services_name == ‘DELETE’)
    {
    curl_setopt($liveCurl, CURLOPT_CUSTOMREQUEST, ‘DELETE’);
    curl_setopt($liveCurl, CURLOPT_POSTFIELDS, http_build_query($web_services_param));
    }

    if ($web_services_name == ‘GET’)
    {
    $web_services_req_url .= ‘?’ . http_build_query($web_services_param);
    }

    if ($web_services_name == ‘POST’)
    {
    curl_setopt($liveCurl, CURLOPT_POST, TRUE);
    curl_setopt($liveCurl, CURLOPT_POSTFIELDS, http_build_query($web_services_param));
    }

    if ($web_services_name == ‘PUT’)
    {
    curl_setopt($liveCurl, CURLOPT_CUSTOMREQUEST, ‘PUT’);
    curl_setopt($liveCurl, CURLOPT_POSTFIELDS, http_build_query($web_services_param));
    }
    curl_setopt($liveCurl, CURLOPT_HTTPHEADER, array(‘Accept: application/json’));

    curl_setopt($liveCurl, CURLOPT_URL, $web_services_req_url);

    curl_setopt($liveCurl, CURLOPT_HEADER, TRUE);

    curl_setopt($liveCurl, CURLOPT_SSL_VERIFYPEER, false);

    $api_response = curl_exec($liveCurl);
    $web_services_response = curl_getinfo($liveCurl);
    curl_close($liveCurl);

    $api_response_header = trim(substr($api_response, 0, $web_services_response[‘header_size’]));
    $api_response_body = substr($api_response, $web_services_response[‘header_size’]);

    echo $web_services_response[‘http_code’];

    echo $api_response_header;

    echo $api_response_body;
    [/php]

    We hope you get an idea about PHP HTTP Methods RESTful API Structure
    We would like to have feedback on my Information blog .
    Your valuable any feedback, Good question, Inspirational Quotes, or Motivational comments about this article are always welcome.
    If you liked this post, Please don’t forget to share this as Well as Like FaceBook Page.

    We hope This Post can help you…….Good Luck!.

    READ :  PHP curl POST Sending JSON Data

    Leave a Comment