PHP CURL POST Request with Headers Example
Php 05-Aug-2021

PHP CURL POST Request with Headers Example

In this PHP curl post example tutorial, we will learn how to POST data with PHP POST cURL requests. In this tutorial, we will take two examples of PHP post curl, First, one sends data to the server using the PHP CURL POST and second is send push notification to a mobile device with POST PHP CURL with curl post example with headers.

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 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' => 'tutsmake',
      'email' => 'tutsmake@gmail.com',
      'mobile' => '9898989898',
  );
    
  $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.