2013-10-28 16 views
16

Tôi có một dịch vụ web đơn giản mà tôi muốn kết nối. Để đăng một số XML, sẽ được xử lý đúng cách ở phía dịch vụ web, tôi cần chuẩn bị một yêu cầu thích hợp. Tôi đang sử dụng cURL cho điều này như thế:Loại nội dung PHP cURL không được đặt

try { 
    $ch = curl_init(); 

    if (FALSE === $ch) 
     throw new Exception('failed to initialize'); 

    curl_setopt($ch, CURLOPT_URL,"192.168.1.37"); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
              'Content-Type: application/xml', 
              'Connection: Keep-Alive' 
              )); 
    curl_setopt($ch, CURLOPT_HEADER, 1); 
    curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password); 
    curl_setopt($ch, CURLOPT_PROXY, ''); 
    curl_setopt($ch, CURLOPT_POST, 1); 
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, TRUE); 
    curl_setopt($ch, CURLOPT_HTTPHEADER,array("Expect: ")); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
    curl_setopt($ch, CURLOPT_POSTFIELDS,$xml); 


    $request= curl_getinfo($ch); 
    var_dump($request); 


    $content = curl_exec($ch); 


    if (FALSE === $content) 
     throw new Exception(curl_error($ch), curl_errno($ch)); 



} catch(Exception $e) { 

    trigger_error(sprintf(
     'Curl failed with error #%d: %s', 
     $e->getCode(), $e->getMessage()),E_USER_ERROR); 

} 

Mọi thứ đều ổn. Trước tiên, tôi hiển thị phần thân yêu cầu từ curl_getinfo($ch); và sau đó tôi hiển thị phản hồi từ biến số $content.

Chỉ hạn chế cho yêu cầu được gửi đến máy chủ là Content-Type tiêu đề - nó phải được application/xml khác máy chủ sẽ trả lời với một lỗi (Nó không phải là ý tưởng của tôi và tôi không thể làm bất cứ điều gì về nó)

đây là yêu cầu đầu ra:

array(21) { ["url"]=> string(68) "192.168.1.37" ["content_type"]=> NULL ["http_code"]=> int(0) ["header_size"]=> int(0) ["request_size"]=> int(0) ["filetime"]=> int(0) ["ssl_verify_result"]=> int(0) ["redirect_count"]=> int(0) ["total_time"]=> float(0) ["namelookup_time"]=> float(0) ["connect_time"]=> float(0) ["pretransfer_time"]=> float(0) ["size_upload"]=> float(0) ["size_download"]=> float(0) ["speed_download"]=> float(0) ["speed_upload"]=> float(0) ["download_content_length"]=> float(-1) ["upload_content_length"]=> float(-1) ["starttransfer_time"]=> float(0) ["redirect_time"]=> float(0) ["certinfo"]=> array(0) { } }

và phản ứng là HTTP 400 Bad Request

Những gì tôi có thể nhìn thấy từ đó là ["content_type"]=> NULL nhưng tôi đã đặt biến này trong mã PHP của mình ...

Bất cứ ai có thể cho tôi biết những gì tôi đang thiếu?

+0

Hãy thử xem bài đăng này: [http://stackoverflow.com/questions/9823810/set-http-request-content-type](http://stackoverflow.com/questions/9823810/set-http- request-content-type) –

Trả lời

39

Bạn đang thiết HTTP_HEADER hai lần:

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
              'Content-Type: application/xml', 
              'Connection: Keep-Alive' 
              )); 

curl_setopt($ch, CURLOPT_HTTPHEADER,array("Expect: ")); 

Tôi đoán đó là vấn đề. Lần thứ hai bạn cài đặt, bạn xóa cài đặt đầu tiên.

+2

Tất nhiên là nó! Không thể tin rằng đó là sai lầm ngu ngốc đến thế. Cảm ơn bạn Legionar! – Mithrand1r

+1

Khắc phục sự cố với mã dán bản sao. Tôi đã có cùng một dòng chính xác gây ra cùng một vấn đề chính xác. Đã bỏ phiếu – khuderm

Các vấn đề liên quan