通过curl发送xml和头

通过curl发送xml和头

本文介绍了通过curl发送xml和头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想知道如何在curl会话中设置所有这些数据:

wondering how I can set all this data in a curl session, via php:

POST /feeds/api/users/default/uploads HTTP/1.1
Host: uploads.gdata.youtube.com
Authorization: AuthSub token="DXAA...sdb8"
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--

我不明白为什么有一些标题, - f93dcbA3 更多标题(是什么边界?),一些xml(为什么?),更多的标题和文件的内容。

I don't understand why have some headers, then the --f93dcbA3 more headers (what's the boundary?), some xml (why here ?), more headers and the content of a file.

我知道如何在没有 xml 部分和'boundary'的情况下发出请求。

I know how to make the request without the xml part and the 'boundary'.

任何帮助将被赞赏:D

Any help will be appreciated :D

推荐答案

边界是必需的,因为形式enctype multipart / form-data ,而在这种情况下 multipart / related 。边界是一个唯一字符串,不能出现在请求中的任何其他位置,它用于将每个元素与表单分离,无论是文本输入的值还是文件上传。每个边界都有自己的内容类型。

The boundary is required because the form enctype is multipart/form-data, rather in this case multipart/related. The boundary is a unique string that cannot appear anywhere else in the request, and it is used to separate each element from the form, whether it is the value of a text input, or a file upload. Each boundary has its own content-type.

Curl不能为你做 multipart / related 要使用解决方法,请参阅curl邮寄名单中的建议。基本上,你必须自己构造大部分的消息。

Curl cannot do multipart/related for you, so you will need to use a workaround, see this message from the curl mailing list for suggestions. Basically, you will have to construct most of the message yourself.

注意,最后一个边界有一个额外的 -

Note, the last boundary has an additional -- at the end.

此代码应该有助于您开始:

This code should hopefully help get you started:

<?php

$url       = 'http://uploads.gdata.youtube.com/feeds/api/users/default/uploads';
$authToken = 'DXAA...sdb8'; // token you got from google auth
$boundary  = uniqid();      // generate uniqe boundary
$headers   = array("Content-Type: multipart/related; boundary=\"$boundary\"",
                   "Authorization: AuthSub token=\"$authToken\"",
                   'GData-Version: 2',
                   'X-GData-Key: key=adf15....a8dc',
                   'Slug: video-test.mp4');

$postData  = "--$boundary\r\n"
            ."Content-Type: application/atom+xml; charset=UTF-8\r\n\r\n"
            .$xmlString . "\r\n"  // this is the xml atom data
            ."--$boundary\r\n"
            ."Content-Type: video/mp4\r\n"
            ."Content-Transfer-Encoding: binary\r\n\r\n"
            .$videoData . "\r\n"  // this is the content of the mp4
            ."--$boundary--";


$ch  = curl_init($url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$response = curl_exec($ch);
curl_close($ch);

希望有帮助。

这篇关于通过curl发送xml和头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 20:28