Curl With Post Method in PHP

Curl With Post Method in PHP

    $post_data['name'] = 'test;
    $post_data['mobile_number'] = 9999999999;

    $data = json_encode($post_data);
    $url = 'https://www.postsparrow.com/api';
    $header = array(
        'Content-Type: application/json'
    );

    $curl_arr = curl_init($url);
    curl_setopt($curl_arr, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl_arr, CURLOPT_MAXREDIRS, 10);
    curl_setopt($curl_arr, CURLOPT_POST, true);
    curl_setopt($curl_arr, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($curl_arr, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($curl_arr, CURLOPT_POSTFIELDS, $data);
    curl_setopt($curl_arr, CURLOPT_HTTPHEADER, $header);
    $result = curl_exec($curl_arr);
    curl_close($curl_arr);

    $response = json_decode($result);

	echo "<pre>";print_r($response);die();

if You want to Add Basic Authorization then header

$header = array(
        'Content-Type: application/json',
        'Authorization: Basic '. base64_encode("admin:12345")
    );

0 Comments

Leave a Reply

You must be logged in to post a comment.