Chetan Gawai
Chetan Gawai

Reputation: 2401

Curl Post In Php

I am firing CURL POST request with json data but still the content type of response is text/plain. Where am I going wrong?

$data = array(
            "name" => "aaaaa",
            "email" => "[email protected]",
            "password" =>"aaa",
            "gender" => "male",
                           );

$data1 = json_encode($data);

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, "http://xxxx.xxx.xxx/register");
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$data1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  $result = curl_exec($ch);
  $last = curl_getinfo($ch);
  print_r($result);
  print_r($last);

Response : -

Array
(
    [url] => http://xxxx.xxx.xxx/register
    [content_type] => text/plain
    [http_code] => 403
    [header_size] => 358
    [request_size] => 277
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 0.067338
    [namelookup_time] => 2.2E-5
    [connect_time] => 0.000448
    [pretransfer_time] => 0.007352
    [size_upload] => 161
    [size_download] => 9
    [speed_download] => 133
    [speed_upload] => 2390
    [download_content_length] => 9
    [upload_content_length] => 161
    [starttransfer_time] => 0.06731
    [redirect_time] => 0
    [redirect_url] => 

)

Upvotes: 0

Views: 67

Answers (1)

Rick Bakker
Rick Bakker

Reputation: 76

That's the response. So that means the response from the server is in text/plain. Your request instead is application/json. If you want to set the mime-type of your own script you can use Header();

Upvotes: 2

Related Questions