簡體   English   中英

未設置PHP cURL Content-Type

[英]PHP cURL Content-Type is not set

我有一個簡單的Web服務,我想連接。 為了發布一些XML ,它將在Web服務端正確進行,我需要准備一個正確的請求。 我這樣使用cURL

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

}

一切都應該沒問題。 首先我從curl_getinfo($ch);顯示請求體curl_getinfo($ch); 然后我顯示$content變量的響應。

只有發送到服務器的請求的限制是Content-Type標頭 - 它必須是application/xml否則服務器將響應錯誤(這不是我的想法,我不能做任何事情)

這是輸出請求:

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) { } }

和響應是HTTP 400 Bad Request

我可以從中看到的是["content_type"]=> NULL但我在我的PHP代碼中設置了這個變量...

誰能告訴我我失蹤了什么?

您正在設置兩次HTTP_HEADER:

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

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

我猜這就是問題所在。 第二次設置時,刪除第一個設置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM