PHP Curl发送标题中的内容长度无效

PHP Curl发送标题中的内容长度无效,php,curl,content-length,Php,Curl,Content Length,我正在使用Curl将照片上传到实际站点。我可以从Fiddler中看到标题如下所示: Host: test.example.com Proxy-Connection: keep-alive Data-Type: json Accept-Encoding: gzip, deflate Content-Length: 62601 Content-Type: application/json Accept-Language: en-us Accept: application/json Connecti

我正在使用Curl将照片上传到实际站点。我可以从Fiddler中看到标题如下所示:

Host: test.example.com
Proxy-Connection: keep-alive
Data-Type: json
Accept-Encoding: gzip, deflate
Content-Length: 62601
Content-Type: application/json
Accept-Language: en-us
Accept: application/json
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36
我使用的正是上面提到的标题。我使用filesize获取内容长度的值

实际上我并不需要标题,脚本通常在没有标题的情况下工作,但是它失败得太频繁了,为了解决问题,我将标题放进去了

除了内容长度之外,所有标题都可以正常工作。当我把它放进去时,我从服务器得到一个响应,表明命令的格式不正确,我不能在这里显示实际的响应。我不太了解内容长度,但我相信这会告诉服务器文件的大小,并帮助服务器确定上传是否完成。我想如果我能正常工作,它会增加我脚本的稳定性


我将感谢任何帮助

如果要发送带有curl的文件,请将完整的文件路径放在@之前,curl将处理其余的文件

$headers = array(
        "Proxy-Connection: keep-alive",
        "Data-Type: json",
        "Accept-Encoding: gzip, deflate",
        "Content-Type: application/json",       
        "Accept-Language: en-us",
        "Connection: keep-alive",
    );
$data = array(
    'fieldForFile' => '@'.  $filePath //Don't forget @ and the complete file path to the file
    'anyOtherFields' => $value,


);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
// Use CURLOPT_USERAGENT for setting the user agent
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $POST_DATA);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
$response = curl_exec($ch);

// Error handling
if($errno = curl_errno($ch)) {
    $error_message = curl_strerror($errno);
    echo "cURL error ({$errno}):\n {$error_message}";
} else {
    echo "Success";
}

curl_close ($ch);

内容长度是现在的两倍吗?如果我们可以看到$data或$data\u字符串的内容,这可能真的很有帮助?对不起,输入错误,POSTFIELDS出现了两次。我无法显示$data的内容,但它包含有关缩略图的其他信息。这有用吗?也许我发送了错误的内容值?Curl本身设置了内容长度,为什么不让它来决定什么是最好的呢?使用了内容长度,这样服务器就可以决定是否拒绝消息。这意味着您可以通过检查头来防止人们试图向您发送TB的数据。如果未指定内容长度,则可以一起关闭连接。现在,当您自己设置内容长度时,棘手的部分是您可能不会在标题和实际内容之间包含分隔符的大小。因此,为什么@NaeiKinDus会这么说——让curl来解决这个问题。谢谢你。如果它不完全是一个文件呢。在我的例子中,我正在读取jpg的base64内容,将其与其他数据相结合,并将所有内容保存到一个变量中,然后该变量将被发布。@user2029890所以所有数据只有一个postfields,是吗?
$headers = array(
        "Proxy-Connection: keep-alive",
        "Data-Type: json",
        "Accept-Encoding: gzip, deflate",
        "Content-Type: application/json",       
        "Accept-Language: en-us",
        "Connection: keep-alive",
    );
$data = array(
    'fieldForFile' => '@'.  $filePath //Don't forget @ and the complete file path to the file
    'anyOtherFields' => $value,


);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
// Use CURLOPT_USERAGENT for setting the user agent
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $POST_DATA);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
$response = curl_exec($ch);

// Error handling
if($errno = curl_errno($ch)) {
    $error_message = curl_strerror($errno);
    echo "cURL error ({$errno}):\n {$error_message}";
} else {
    echo "Success";
}

curl_close ($ch);