PHP CURL POST Request with Headers Example

This post will give you example of php curl post request with headers example. In this article, we will implement a how to send headers in curl php. i explained simply step by step how to send headers in php curl. we will help you to give example of php curl post request header. Here, Creating a basic example of php curl post data header.

PHP cURL have set of curl function like curl_init(), curl_setopt(), curl_exec() etc. using cURL we will call post api with headers to getting json data and we can use their data in our project.

What is PHP CURL?

Answer: The PHP cURL is a library used for making HTTP requests to any web server.

In PHP CURL, There are 4 common steps in every PHP cURL script:

  • Initialize PHP cURL.
  • Set the options, the target URL, POST data and such. There are a ton of possible options.
  • Execute the cURL, handle any PHP CURL errors.
  • Close the PHP cURL connection.

In this example, we post data with PHP CURL. We will convert array data to JSON data using json_encode() method and then we will post data with PHP CURL.

Example 1 – php curl post request JSON data

Let’s see the below example to post JSON data with PHP Curl:

<?php
 
 // A sample PHP Script to POST data using cURL
  
  $data = array(
      'name' => 'CodeOne',
      'email' => 'CodeOne@gmail.com',
      'mobile' => '9393939393',
  );
    
  $post_data = json_encode($data);
    
  // Prepare new cURL resource
  $crl = curl_init('https://example.com/api/user');
  curl_setopt($crl, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($crl, CURLINFO_HEADER_OUT, true);
  curl_setopt($crl, CURLOPT_POST, true);
  curl_setopt($crl, CURLOPT_POSTFIELDS, $post_data);
    
  // Set HTTP Header for POST request 
  curl_setopt($crl, CURLOPT_HTTPHEADER, array(
      'Content-Type: application/json',
      'Content-Length: ' . strlen($payload))
  );
    
  // Submit the POST request
  $result = curl_exec($crl);
    
  // handle curl error
  if ($result === false) {
      // throw new Exception('Curl error: ' . curl_error($crl));
      //print_r('Curl error: ' . curl_error($crl));
      $result_noti = 0; die();
  } else {
 
      $result_noti = 1; die();
  }
  // Close cURL session handle
  curl_close($crl);
    
?>

If you have any error in calling the PHP Curl, You can use the below code to handle error in PHP CURL:

if ($result === false) {
    // throw new Exception('Curl error: ' . curl_error($crl));
    //print_r('Curl error: ' . curl_error($crl));
    $result_noti = 0; die();
} else {
 
    $result_noti = 1; die();
}

Example 2 – Send Push Notification using FCM in PHP CURL

If you want to send push notification on the mobile device. You can see the below example for that.

function send_notification_FCM($notification_id, $title, $message, $id,$type) {
    $accesstoken = 'fcm_key';
 
    $URL = 'https://fcm.googleapis.com/fcm/send';
 
 
        $post_data = '{
            "to" : "' . $notification_id . '",
            "data" : {
              "body" : "",
              "title" : "' . $title . '",
              "type" : "' . $type . '",
              "id" : "' . $id . '",
              "message" : "' . $message . '",
            },
            "notification" : {
                 "body" : "' . $message . '",
                 "title" : "' . $title . '",
                  "type" : "' . $type . '",
                 "id" : "' . $id . '",
                 "message" : "' . $message . '",
                "icon" : "new",
                "sound" : "default"
                },
 
          }';
        // print_r($post_data);die;
 
    $crl = curl_init();
 
    $headr = array();
    $headr[] = 'Content-type: application/json';
    $headr[] = 'Authorization: ' . $accesstoken;
    curl_setopt($crl, CURLOPT_SSL_VERIFYPEER, false);
 
    curl_setopt($crl, CURLOPT_URL, $URL);
    curl_setopt($crl, CURLOPT_HTTPHEADER, $headr);
 
    curl_setopt($crl, CURLOPT_POST, true);
    curl_setopt($crl, CURLOPT_POSTFIELDS, $post_data);
    curl_setopt($crl, CURLOPT_RETURNTRANSFER, true);
 
    $rest = curl_exec($crl);
 
    if ($rest === false) {
        // throw new Exception('Curl error: ' . curl_error($crl));
        //print_r('Curl error: ' . curl_error($crl));
        $result_noti = 0;
    } else {
 
        $result_noti = 1;
    }
 
    //curl_close($crl);
    //print_r($result_noti);die;
    return $result_noti;
}

In the above example, we have created function name send_notification_FCM() with following parameter $notification_id, $title, $message, $id,$type. When you want to send push notification to the mobile device, that time you need to call the send_notification_FCM() with specified parameters.

Leave a Reply

Your email address will not be published. Required fields are marked *