首页 > 解决方案 > 如何使用 php curl 7.68 修复缺少的 Content-Length?

问题描述

背景:我是一名 Roku 开发人员,使用 PHP 来打包我的应用程序。在我从 Ubuntu 18.04 升级到 20.04 之前,我的 PHP 代码一直运行良好。一旦 curl 升级到 7.68,我的脚本就坏了。当我使用 curl 来访问硬件设备时,我没有更改服务器代码的选项。

问题:使用 PHP curl 时,由于没有 Content-Length,我现在收到 HTTP 411 错误。但是当我使用 curl 命令行时,它工作正常。如何为 curl 7.68 解决此问题?

实际PHP代码:

function remove() {
    global $E;
    pl("Removing dev app from host $E[ROKU_DEV]");
    home();
    $data = [
        'mysubmit'=>'Delete',
        'archive'=>"",
        'passwd'=>""
    ];
    $response = curl_post("http://$E[ROKU_DEV]/plugin_install", $data, $E['USERPASS']);
    $output = filterString($response, "Roku.Message", "trigger('Set message content', '", "').trigger('Render', node);");
    finish("Remove: $output");
}

function curl_post($url, $data = '', $digest = '') {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_VERBOSE, true);
    curl_setopt($ch, CURLOPT_POST, true);
    if(!empty($digest)) {
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
        curl_setopt($ch, CURLOPT_USERPWD, $digest);
    }
    $result = curl_exec($ch);
    $info = curl_getinfo($ch);
    $errno = curl_errno($ch);
    if($errno) {
        $result = sprintf("cURL Error (%d): %s\n", $errno, curl_strerror($errno));
    } else if($info['http_code'] >= 300) {
        $result = sprintf("HTTP Error (%d): %s\n", $info['http_code'], httpStatusString($info['http_code']));
    }
    curl_close($ch);
    return $result;
}

这将返回:

*   Trying 192.168.0.246:80...
* TCP_NODELAY set
* Connected to 192.168.0.246 (192.168.0.246) port 80 (#0)
> POST /plugin_install HTTP/1.1
Host: 192.168.0.246
Accept: */*
Transfer-Encoding: chunked
Content-Type: application/x-www-form-urlencoded
Expect: 100-continue

* Mark bundle as not supporting multiuse
< HTTP/1.1 401 Unauthorized
< Content-Length: 0
< WWW-Authenticate: Digest qop="auth", realm="rokudev", nonce="1590164871"
< 
* Closing connection 0
* Issue another request to this URL: 'http://192.168.0.246/plugin_install'
* Hostname 192.168.0.246 was found in DNS cache
*   Trying 192.168.0.246:80...
* TCP_NODELAY set
* Connected to 192.168.0.246 (192.168.0.246) port 80 (#1)
* Server auth using Digest with user 'rokudev'
> POST /plugin_install HTTP/1.1
Host: 192.168.0.246
Authorization: Digest username="rokudev", realm="rokudev", nonce="1590164871", uri="/plugin_install", cnonce="NjhiOGQyOGVmZDdiY2FiYTczNGI1ZDRjYmZjY2UzZDU=", nc=00000001, qop=auth, response="4d830af782cb25747f58a7df78a19ae0"
Accept: */*
Transfer-Encoding: chunked
Content-Type: application/x-www-form-urlencoded
Expect: 100-continue

* Mark bundle as not supporting multiuse
< HTTP/1.1 411 Length Required
< Content-Length: 27
< Content-Type: text/plain
< Connection: close
< 
* Closing connection 1

这是我的 curl 命令行:

curl --digest -urokudev:$ROKU_PASS -v -X POST -F 'mysubmit=Delete' -F 'passwd=' -F 'archive=' http://$ROKU_DEV/plugin_install

这是结果:

Note: Unnecessary use of -X or --request, POST is already inferred.
*   Trying 192.168.0.246:80...
* TCP_NODELAY set
* Connected to 192.168.0.246 (192.168.0.246) port 80 (#0)
* Server auth using Digest with user 'rokudev'
> POST /plugin_install HTTP/1.1
> Host: 192.168.0.246
> User-Agent: curl/7.68.0
> Accept: */*
> Content-Length: 0
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 401 Unauthorized
< Content-Length: 0
< WWW-Authenticate: Digest qop="auth", realm="rokudev", nonce="1590102994"
< 
* Connection #0 to host 192.168.0.246 left intact
* Issue another request to this URL: 'http://192.168.0.246/plugin_install'
* Found bundle for host 192.168.0.246: 0x55ae72f12c20 [serially]
* Can not multiplex, even if we wanted to!
* Re-using existing connection! (#0) with host 192.168.0.246
* Connected to 192.168.0.246 (192.168.0.246) port 80 (#0)
* Server auth using Digest with user 'rokudev'
> POST /plugin_install HTTP/1.1
> Host: 192.168.0.246
> Authorization: Digest username="rokudev", realm="rokudev", nonce="1590102994", uri="/plugin_install", cnonce="ZjlhMjgxZDYxNjRiNWNjMTM4Nzc2YmUxMjM5NzcwNTI=", nc=00000001, qop=auth, response="8e7d0866953f033a3cf352aac96b62c7"
> User-Agent: curl/7.68.0
> Accept: */*
> Content-Length: 340
> Content-Type: multipart/form-data; boundary=------------------------217b05bd89ebff16
> 
* We are completely uploaded and fine
* Connection died, retrying a fresh connect
* Closing connection 0
* Issue another request to this URL: 'http://192.168.0.246/plugin_install'
* Hostname 192.168.0.246 was found in DNS cache
*   Trying 192.168.0.246:80...
* TCP_NODELAY set
* Connected to 192.168.0.246 (192.168.0.246) port 80 (#1)
* Server auth using Digest with user 'rokudev'
> POST /plugin_install HTTP/1.1
> Host: 192.168.0.246
> Authorization: Digest username="rokudev", realm="rokudev", nonce="1590102994", uri="/plugin_install", cnonce="ZjlhMjgxZDYxNjRiNWNjMTM4Nzc2YmUxMjM5NzcwNTI=", nc=00000002, qop=auth, response="e9c57a236d1516b4416aa8c5f1883ef1"
> User-Agent: curl/7.68.0
> Accept: */*
> Content-Length: 340
> Content-Type: multipart/form-data; boundary=------------------------217b05bd89ebff16
> 
* We are completely uploaded and fine
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< init_ca_bundle_stat: stat /common/certs/ca-bundle.crt: No such file or directory
< Content-Type: text/html
< Transfer-Encoding: chunked
< 
<html>
... Actual html removed ...
</html>

标签: phpcurlphp-curl

解决方案


推荐阅读