【问题标题】:PHP Curl sending content-length in header not workingPHP Curl在标题中发送内容长度不起作用
【发布时间】:2014-09-16 10:59:33
【问题描述】:

我正在使用 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 获得content-length 的值

   $file = 'test.jpg';
   $file_size = filesize($file);

    $headers = array(
                "Host: test.example.com",
                "Proxy-Connection: keep-alive",
                "Data-Type: json",
                "Accept-Encoding: gzip, deflate",
                "Content-length: $file_size",
                "Content-Type: application/json",       
                "Accept-Language: en-us",
                "Connection: keep-alive",
                "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_URL, $url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
$result = curl_exec($ch);

我实际上并不需要标头,脚本通常在没有标头的情况下也可以工作,但它经常失败,为了解决问题,我将标头放入。

除content-length 外,所有标题都可以正常工作。当我把它放进去时,我收到来自服务器的响应,表明命令格式不正确(我无法在此处显示实际响应)。我对content-length 不太了解,但我相信这会告诉服务器文件的大小,并帮助它确定上传是否完成。我认为如果我能正常工作,它会增加我的脚本的稳定性。

我将不胜感激。

【问题讨论】:

  • 内容长度现在是两倍吗?
  • 如果我们能看到 $data 或 $data_string 的内容可能真的有帮助吗?
  • 抱歉,POSTFIELDS 出现两次的错字。我无法显示 $data 的内容,但它包含有关缩略图的附加信息。这有帮助吗?也许我发送了错误的内容值?
  • Curl 自己设置内容长度,为什么不让它决定什么是最好的呢?
  • Content-Length 用于让服务器决定是否拒绝该消息。这意味着您可以通过检查标头来防止有人试图向您发送 TB 的数据。如果没有指定 Content-Length,则可以一起关闭连接。现在,自己设置内容长度时的棘手部分是您可能不包括标题和实际内容之间的分隔符的大小。因此,为什么@NaeiKinDus 说了什么 - 让 curl 弄清楚。

标签: php curl content-length


【解决方案1】:

如果您要使用 curl 发送文件,请将完整的 filePath 放在 @ 前面,然后 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);

【讨论】:

  • 谢谢你。如果它不完全是一个文件怎么办。就我而言,我正在读取 jpg 的 base64 内容,将其与其他数据结合起来,并将所有这些内容保存到一个变量中,然后再发布。
  • @user2029890 所以你只有一个用于所有数据的postfields是吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-02
  • 1970-01-01
  • 2017-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多