PHP curl将Content-Type更改为application / x-www-form-urlencoded。不应该这样做 [英] PHP curl changes the Content-Type to application/x-www-form-urlencoded. Shouldn't do that

查看:1328
本文介绍了PHP curl将Content-Type更改为application / x-www-form-urlencoded。不应该这样做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要从我使用PHP curl的服务器上传视频直接到YouTube。



我需要此请求格式:

  POST / feeds / api / users / default / uploads HTTP / 1.1 
Host:uploads.gdata.youtube.com
授权:承载ACCESS_TOKEN
GData-Version:2
X-GData-Key:key = adf15ee97731bca89da876c ... a8dc
Slug:video-test.mp4
Content-Type:multipart /有关; 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>
我在朋友的婚礼上给了一个糟糕的烤面包。
< / 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
内容传送编码:二进制

<二进制文件数据>
--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(
=> 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 应用程序/ x-www-form-urlencoded ,当然不是由youtube支持。



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

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

解决方案

根据关于选项参数的文档部分,将HTTP方法设置为 POST 默认为内容类型设置为 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:'。 ',$ this-> video-> getFilename()),
'Content-Type:'。sprintf('multipart / related; boundary =%s',
$ this-> ; boundaryString),
'Content-Length:'。strlen($ content),
'Connection:close'


I want to upload a video direct to Youtube from my server for which I am using PHP curl.

I need this request format:

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--

This is what I have:

$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);

Dumping the result shows me that curl changed the Content-Type to application/x-www-form-urlencoded which is of course not supported by youtube.

I put my binary content (the video) into CURLOPT_POSTFIELDS, maybe this is wrong, but I don't know how to set the request body other than that.

So how do I preserve my Content-Type that I set?

解决方案

According to the documentation section regarding the options' parameter setting the HTTP method to POST defaults to content type being set to application/x-www-form-urlencoded. So my suspicion is that setting all the options at once, results in your Content-Type being overwritten.
My suggestion would be to set the content type in a single statement, after you've set the method, i.e.

$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'
));

这篇关于PHP curl将Content-Type更改为application / x-www-form-urlencoded。不应该这样做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆