2015-05-04 78 views
0

我正在解僱CURLPOST請求json數據,但其響應的內容類型仍爲text/plain。 我哪裏錯了?Curl Post In Php

$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); 

響應: -

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] => 

) 
+2

正在響應的頁面未設置正確的MIME類型。你用JSON發送了一些東西,他們用純文本回答。他們不必以相同的方式迴應;)如果您張貼到您自己的腳本中,請檢查您是否設置了正確的MIME類型。 – MueR

回答

2

這是響應。所以這意味着服務器的響應是純文本的。你的請求改爲application/json。如果你想設置你自己腳本的MIME類型,你可以使用Header();