1

I'm try to below code for send request in post method with json data and header constant type : application/json

$ch_get = curl_init($url_get);
        $jsonData1_get_r = array(
            'customerMobileNo'=>'9040845440',
            'recipientMobileNo'=>'7008565316',
            'recipientName'=>'Name Test Test',
            'accountNo'=>'5928374737328009',
            'bankName'=>'HDFC',
            'accIfsc'=>'HDFC0002058',
            'transactionType'=>'IMPS',
            'amount'=>'100'
            );
        $jsonDataEncoded_get = json_encode($jsonData1_get_r);

        curl_setopt($ch_get, CURLOPT_POST, 1);
        curl_setopt($ch_get, CURLOPT_POSTFIELDS, $jsonDataEncoded_get);
        curl_setopt($ch_get, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
        curl_setopt($ch_get, CURLOPT_HTTPHEADER, array('Authorization: '.$token.''));
        curl_setopt($ch_get, CURLOPT_RETURNTRANSFER, TRUE);
        $result_get = curl_exec($ch_get);
        curl_close($ch_get);

when use above code below error comes

Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

bizz website
  • 85
  • 2
  • 8
  • 3
    I'm not sure, but have you tried setting both headers at once, in the same array? The way it's currently used, it would seem that the `Content-Type` is being overriden. – FirstOne Feb 02 '18 at 12:10
  • 1
    Something like `curl_setopt($ch_get, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: '.$token));` – FirstOne Feb 02 '18 at 12:11
  • [I think you must look at here.](https://stackoverflow.com/questions/21271140/curl-and-php-how-can-i-pass-a-json-through-curl-by-put-post-get) –  Feb 02 '18 at 12:13

1 Answers1

1

You can Use below code to achieve your goal

$jsonData1_get_r = array(
        'customerMobileNo'=>'9040845440',
        'recipientMobileNo'=>'7008565316',
        'recipientName'=>'Name Test Test',
        'accountNo'=>'5928374737328009',
        'bankName'=>'HDFC',
        'accIfsc'=>'HDFC0002058',
        'transactionType'=>'IMPS',
        'amount'=>'100'
        ); 

$jsonDataEncoded_get  = json_encode($jsonData1_get_r );
$ch_get = curl_init($url_get);
curl_setopt($ch_get, CURLOPT_CUSTOMREQUEST, "POST"); 
curl_setopt($ch_get, CURLOPT_POSTFIELDS, $jsonDataEncoded_get );
curl_setopt($ch_get, CURLOPT_RETURNTRANSFER, true);    
curl_setopt($ch_get, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Authorization: ' . $token)                                                                       

);      

Hope you this will helps you !!

Thanks & Regards

Shishil Patel
  • 3,449
  • 2
  • 12
  • 16