PHP curl将Content-Type更改为application / x-www-form-urlencoded。不应该这样做

时间:2013-04-18 09:52:40

标签: php curl http-headers

我想从我的服务器上直接将视频上传到Youtube,我正在使用PHP curl。

我需要这种请求格式:

POST /feeds/api/users/default/uploads HTTP/1.1
Host: uploads.gdata.youtube.com
Authorization: Bearer ACCESS_TOKEN
GData-Version: 2
X-GData-Key: key=adf15ee97731bca89da876c...a8dc
Slug: video-test.mp4
Content-Type: multipart/related; boundary="f93dcbA3"
Content-Length: 1941255
Connection: close

--f93dcbA3
Content-Type: application/atom+xml; charset=UTF-8

<?xml version="1.0"?>
<entry xmlns="http://www.w3.org/2005/Atom"
  xmlns:media="http://search.yahoo.com/mrss/"
  xmlns:yt="http://gdata.youtube.com/schemas/2007">
  <media:group>
    <media:title type="plain">Bad Wedding Toast</media:title>
    <media:description type="plain">
      I gave a bad toast at my friend's wedding.
    </media:description>
    <media:category
      scheme="http://gdata.youtube.com/schemas/2007/categories.cat">People
    </media:category>
    <media:keywords>toast, wedding</media:keywords>
  </media:group>
</entry>
--f93dcbA3
Content-Type: video/mp4
Content-Transfer-Encoding: binary

<Binary File Data>
--f93dcbA3--

这就是我所拥有的:

$content = $this->buildRequestContent();

$ch = curl_init();

$curlConfig = array(
    CURLOPT_URL => 'http://uploads.gdata.youtube.com/feeds/api/users/default/uploads',
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_BINARYTRANSFER => true,
    CURLOPT_POSTFIELDS => $content,
    CURLOPT_HTTPHEADER, array(
        "Authorization" => sprintf("GoogleLogin auth=%s", $this->accessToken),
        "GData-Version" => 2,
        "X-GData-Key" => sprintf("key=%s", $this->developerKey),
        "Slug" => sprintf("%s", $this->video->getFilename()),
        "Content-Type" => sprintf("multipart/related; boundary=\"%s\"", $this->boundaryString),
        "Content-Length" => strlen($content),
        "Connection" => "close"
    ),
);

curl_setopt_array($ch, $curlConfig);

$result = curl_exec($ch);

转储结果会显示curl已将Content-Type更改为application/x-www-form-urlencoded,这当然不受youtube支持。

我将我的二进制内容(视频)放入CURLOPT_POSTFIELDS,也许这是错误的,但我不知道如何设置除此之外的请求正文。

那么如何保留我设置的Content-Type?

2 个答案:

答案 0 :(得分:10)

接受的答案对我没有帮助,我在您的代码中发现了另一个问题。根据当前的PHP documentation CURLOPT_HTTPHEADER必须设置如下:

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-type: text/plain',
    'Content-length: 100'
));

它必须是字符串列表,而不是像array('Content-Type' => 'text/plain')这样的散列。 希望它能节省一些人的调试时间。

答案 1 :(得分:7)

根据documentation section regarding the options' parameter设置,POST的HTTP方法默认为内容类型设置为application/x-www-form-urlencoded。所以我怀疑是一次性设置所有选项会导致Content-Type被覆盖 我的建议是在设置方法后将内容类型设置在单个语句中,即

$curlConfig = array(
    CURLOPT_URL => 'http://uploads.gdata.youtube.com/feeds/api/users/default/uploads',
    CURLOPT_POST => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_BINARYTRANSFER => true,
    CURLOPT_POSTFIELDS => $content,
);

curl_setopt_array($ch, $curlConfig);

curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Authorization: ' . sprintf('GoogleLogin auth=%s', $this->accessToken),
    'GData-Version: 2',
    'X-GData-Key: ' . sprintf('key=%s', $this->developerKey),
    'Slug: ' . sprintf('%s', $this->video->getFilename()),
    'Content-Type: ' . sprintf('multipart/related; boundary="%s"',
                                  $this->boundaryString),
    'Content-Length: ' . strlen($content),
    'Connection: close'
));