2

PHP の組み込み cURL モジュールは、送信前にヘッダー フィールドを変更しているようです。

cURL を使用して、HTTP 要求を介してエンコーダー デバイスと通信するための小さなクラスを開発しました。このコードは Windows では問題なく動作しますが、Debian で実行すると、デバイスが HTTP 406 エラーで応答します。

エラー コードは、サーバーが要求された形式で応答できないことを示します。(詳細)

応答タイプは URL の拡張子によって決定され (可能なモードは xml と json です)、Acceptヘッダーにパラメーターを明示的に設定していないため、これは奇妙です。

パラメータを使用してCURLOPT_VERBOSE、次のデータをダンプします。

* Hostname was NOT found in DNS cache
*   Trying 172.19.0.9...
* Connected to 172.19.0.9 (172.19.0.9) port 1080 (#0)
* successfully set certificate verify locations:
*   CAfile: none
  CApath: /etc/ssl/certs
* SSL connection using TLSv1.2 / ECDHE-RSA-AES256-GCM-SHA384
* Server certificate:
*        subject: C=US; ST=Illinois; L=Lake Forest; O=Haivision Network Video, Inc.; OU=PRODUCT DEVELOPMENT; CN=localhost.localdomai                                    n; emailAddress=support@haivision.com
*        start date: 2016-01-22 14:40:48 GMT
*        expire date: 2026-01-19 14:40:48 GMT
*        issuer: C=US; ST=Illinois; L=Lake Forest; O=Haivision Network Video, Inc.; OU=PRODUCT DEVELOPMENT; CN=localhost.localdomain                                    ; emailAddress=support@haivision.com
*        SSL certificate verify result: self signed certificate (18), continuing anyway.
> POST /ecs/auth.xml HTTP/1.1
Host: 172.19.0.9:1080
Accept: */*
Content-Length: 86
Content-Type: application/x-www-form-urlencoded

* upload completely sent off: 86 out of 86 bytes
< HTTP/1.1 406 Not Acceptable
* Server nginx is not blacklisted
< Server: nginx
< Date: Fri, 01 Apr 2016 08:45:30 GMT
< Content-Type: application/xml
< Content-Length: 135
< Connection: keep-alive
<
* Connection #0 to host 172.19.0.9 left intact

Content-Type: application/xmlに変更されたように見えますがapplication/x-www-form-urlencoded、これがリクエストが惨めに失敗する主な理由だと思います。

関数に渡される配列はcurl_setopts()次のようになります。

array(11) {
  [19913]=>
  bool(true)
  [64]=>
  bool(false)
  [52]=>
  bool(false)
  [68]=>
  int(10)
  [10023]=>
  array(5) {
    ["Authorization"]=>
    string(10) "Basic ==Og"
    ["Cache-Control"]=>
    string(8) "no-cache"
    ["Content-Type"]=>
    string(15) "application/xml"
    ["Connection"]=>
    string(10) "keep-alive"
    ["Content-Length"]=>
    int(86)
  }
  [20079]=>
  array(2) {
    [0]=>
    object(Pest)#43 (6) {
      ["curl_opts"]=>
      array(9) {
        [19913]=>
        bool(true)
        [64]=>
        bool(false)
        [52]=>
        bool(false)
        [68]=>
        int(10)
        [10023]=>
        array(0) {
        }
        [20079]=>
        *RECURSION*
        [81]=>
        int(0)
        [84]=>
        int(2)
        [41]=>
        bool(true)
      }
      ["base_url"]=>
      string(23) "https://172.19.0.9:1080"
      ["last_response"]=>
      NULL
      ["last_request"]=>
      NULL
      ["last_headers"]=>
      NULL
      ["throw_exceptions"]=>
      bool(true)
    }
    [1]=>
    string(13) "handle_header"
  }
  [81]=>
  int(0)
  [84]=>
  int(2)
  [41]=>
  bool(true)
  [10036]=>
  string(4) "POST"
  [10015]=>
  string(86) "<?xml version="1.0" encoding="UTF-8"?>
<user username="#########" password="########"/>
"

ご覧のとおり、Acceptタグはなく、コンテンツ タイプは に設定されていapplication/xmlます。

だからここに私の質問があります: curl がリクエストのヘッダーを変更するのはなぜですか? 問題の根本が別のものである場合、Debian Jessie ではなく Win10 で動作する理由は何ですか?

更新 (16. 04. 04.):

おもしろいことに、同じバージョンの cURL ライブラリは PHP では機能しませんが、cli では機能します。

curl -X POST -H "Authorization: Basic aGFpYWRtaW46bWFuYWdlcg==" -H "Content-Type: application/xml" -H "Cache-Control: no-cache" -H "Postman-Token: 760f1aac-619f-4b64-ec06-0146554fcecf" -d '<?xml version="1.0"?><user username="########" password="#######" />' "https://172.19.0.9:1080/ecs/auth.xml"

<?xml version="1.0"?>
<sessionid value="fd7b8fd0-ac5e-4f72-a01c-142082de24f1"/>

Linux ボックスの CURL バージョンは7.26.0 (x86_64-pc-linux-gnu) libcurl/7.26.0です。

事前に感謝します。テキストの壁で申し訳ありません。

4

0 に答える 0