2012-02-12 4 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 부분과 '경계'없이 요청하는 방법을 알고 있습니다.

어떤 도움

부탁드립니다 : D

답변

4

양식에 enctype 오히려이 경우 multipart/related에, multipart/form-data 때문에 경계가 필요합니다. 경계는 요청의 다른 어느 곳에서도 표시 할 수없는 고유 한 문자열이며 텍스트 입력 값인지 또는 파일 업로드 여부와 상관없이 양식에서 각 요소를 구분하는 데 사용됩니다. 각 경계에는 고유 한 내용 유형이 있습니다.

Curl은 multipart/related을 사용할 수 없으므로 회피 방법을 사용해야합니다. 제안 된 내용은 컬 메일 링리스트의 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