2012-02-12 8 views
3

私はカールのセッションですべてのデータを設定することができますどのように思ってカールを経由して、XMLとヘッダを送信する:は、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(なぜここ?)、より多くのヘッダーとファイルの内容。

私はxml部分と '境界'なしでリクエストを行う方法を知っています。

任意の助けが理解されるであろう:フォームのenctypeがなく、この場合multipart/relatedで、multipart/form-dataあるため、D

答えて

4

境界が必要です。境界は、要求のどこにも出現できない一意の文字列であり、テキスト入力の値であろうとファイルのアップロードであろうと、各要素をフォームから分離するために使用されます。各境界にはそれぞれ独自のコンテンツタイプがあります。

Curlはあなたのためにmultipart/relatedを行うことはできませんので、回避策を使用する必要があります。提案のためにcurlメーリングリストのthis messageを参照してください。基本的には、自分でメッセージの大部分を構築する必要があります。

最後の境界には、最後に--が追加されています。

このコードでは、うまくいけば、あなたが始めるのに役立つはずです。

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

お役に立てば幸いです。

+0

ありがとうございます。 – greenbandit

関連する問題