Youtube Api Curl Upload systeem
peterdg - 18/09/2009 21:28
Nieuw lid
Hoi iedereen,
Ik ben bezig om een youtube api te gebruiken zonder Zend )
Google zelf geeft een prachtige uitleg waar ik geen hol van snap omdat het niet wil werken
Uploading the video file
Het is me echter wel gelukt om een Authenticatie code op te vragen met een SID en een LSID code, maar het uploaden doet echt heel erg vaag en raar.
Mijn code tot nu:
<?php
[...]
function upload() {
$files = $_FILES;
$name = $files['file']['name'];
$type = $files['file']['type'];
$size = $files['file']['size'];
$tmp_nm = $files['file']['tmp_name'];
$data = array('name' => 'Foo', 'file' => '@'.$tmp_nm);
print_r($_POST);
print_r($_FILES);
echo 'Size '.$size;
$headers = array(
"Authorization: AuthSub token=".$this->auth,
"GData-Version: 2",
"X-GData-Key: key=".$this->dev_key,
"Content-length: ".$size,
"API_XML_request"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://gdata.youtube.com/action/GetUploadToken');
curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_REFERER,true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1);
curl_setopt($ch, CURLOPT_HEADER,0);
if($this->get_info)
{
$this->curlget_info($ch);
}
$output = curl_exec($ch);
print_r($output);
return $output;
}
[...]
?>
<?php
[ ... ]
function upload( ) {
$files = $_FILES ;
$name = $files [ 'file' ] [ 'name' ] ;
$type = $files [ 'file' ] [ 'type' ] ;
$size = $files [ 'file' ] [ 'size' ] ;
$tmp_nm = $files [ 'file' ] [ 'tmp_name' ] ;
$data = array ( 'name' => 'Foo' , 'file' => '@' . $tmp_nm ) ;
"Authorization: AuthSub token=" . $this -> auth ,
"GData-Version: 2" ,
"X-GData-Key: key=" . $this -> dev_key ,
"Content-length: " . $size ,
"API_XML_request"
) ;
$ch = curl_init( ) ;
curl_setopt( $ch , CURLOPT_URL, 'http://gdata.youtube.com/action/GetUploadToken' ) ;
curl_setopt( $ch , CURLOPT_USERAGENT, $this -> user_agent ) ;
curl_setopt( $ch , CURLOPT_RETURNTRANSFER, TRUE ) ;
curl_setopt( $ch , CURLOPT_TIMEOUT, 10 ) ;
curl_setopt( $ch , CURLOPT_SSL_VERIFYPEER, false ) ;
curl_setopt( $ch , CURLOPT_POST, 1 ) ;
curl_setopt( $ch , CURLOPT_FOLLOWLOCATION, 1 ) ;
curl_setopt( $ch , CURLOPT_HTTPHEADER, $headers ) ;
curl_setopt( $ch , CURLOPT_POSTFIELDS, $data ) ;
curl_setopt( $ch , CURLOPT_REFERER, true ) ;
curl_setopt( $ch , CURLOPT_FOLLOWLOCATION , 1 ) ;
curl_setopt( $ch , CURLOPT_HEADER, 0 ) ;
if ( $this -> get_info )
{
$this -> curlget_info ( $ch ) ;
}
$output = curl_exec( $ch ) ;
return $output ;
}
[ ... ]
?>
Output 1:
Citaat:
[ignore]
Array ( [token] => TOKEN ) Array ( [file] => Array ( [name] => 0016.png [type] => image/png [tmp_name] => D:wamptmpphp178D.tmp [error] => 0 [size] => 4216 ) ) Size 4216
[/ignore]
Google
Error
Length Required
POST requests require a Content-length header.
Output 2:
Citaat:
[ignore]
Array ( [token] => TOKEN ) Array ( [file] => Array ( [name] => Film.wmv [type] => video/x-ms-wmv [tmp_name] => D:wamptmpphp11D3.tmp [error] => 0 [size] => 96589 ) ) Size 96589
[/ignore]
Google
Error
Length Required
POST requests require a Content-length header.
Ik hoop dat iemand me ermee kan helpen
6 antwoorden
Gesponsorde links
marten - 19/09/2009 01:33
Beheerder
Heb je op irc al verteld dat je er een get van moet maken......
peterdg - 30/09/2009 15:51 (laatste wijziging 30/09/2009 15:52)
Nieuw lid
@marten, get ondersteunen ze niet
Dit is wat ik dan krijg als ik POST in GET omzet:
Citaat:
HTTP method GET is not supported by this URL
Error 405
maar Ik heb het probleem geprobeerd te construeren met sockets en fsockopen.. maar nog steeds hetzelfde probleem en output
Sockets:
<?php
$server = "gdata.youtube.com";
$script = "/action/GetUploadToken";
define('SERVER', $server);
define('PORT', 80);
define('PATH', $script);
define('_EOL', "\r\n");
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
if (!$sock) {
echo 'Fatal error: Couldn\'t create socket';
exit;
}
if (!socket_connect($sock, SERVER, PORT)) {
echo 'Fatal error: Couldn\'t connect';
exit;
}
$request = null;
$request .= 'POST ' . PATH . ' HTTP/1.0' . _EOL;
$request .= 'Host: ' . SERVER . _EOL;
$request .= 'Authorization: AuthSub token="'.$this->auth.'"'. _EOL;
$request .= 'GData-Version: 2 '._EOL;
$request .= 'X-GData-Client: '.$this->sid._EOL;
$request .= 'X-GData-Key: key='.$this->dev_key._EOL;
$request .= 'Content-Length: 5' . _EOL;
$request .= 'Content-Type: application/atom+xml; charset=UTF-8'._EOL;
$request .= 'API_XML_request'._EOL;
$request .= 'Connection: close' . _EOL;
$request .= _EOL;
echo '<pre>';
print_r($request);
echo '</pre>';
if (!socket_send($sock, $request, strlen($request), 0)) {
echo 'Fatal error: Couldn\'t send request';
exit;
}
$output = null;
$buffer = null;
while (socket_recv($sock, $buffer, 1024, 0) != 0) {
$output .= $buffer;
}
print_r($output);
?>
<?php
$server = "gdata.youtube.com";
$script = "/action/GetUploadToken";
define('SERVER', $server);
define('PORT', 80);
define('PATH', $script);
define('_EOL', "\r\n");
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
if (!$sock) {
echo 'Fatal error: Couldn\'t create socket';
exit;
}
if (!socket_connect($sock, SERVER, PORT)) {
echo 'Fatal error: Couldn\'t connect';
exit;
}
$request = null;
$request .= 'POST ' . PATH . ' HTTP/1.0' . _EOL;
$request .= 'Host: ' . SERVER . _EOL;
$request .= 'Authorization: AuthSub token="'.$this->auth.'"'. _EOL;
$request .= 'GData-Version: 2 '._EOL;
$request .= 'X-GData-Client: '.$this->sid._EOL;
$request .= 'X-GData-Key: key='.$this->dev_key._EOL;
$request .= 'Content-Length: 5' . _EOL;
$request .= 'Content-Type: application/atom+xml; charset=UTF-8'._EOL;
$request .= 'API_XML_request'._EOL;
$request .= 'Connection: close' . _EOL;
$request .= _EOL;
echo '<pre>';
print_r($request);
echo '</pre>';
if (!socket_send($sock, $request, strlen($request), 0)) {
echo 'Fatal error: Couldn\'t send request';
exit;
}
$output = null;
$buffer = null;
while (socket_recv($sock, $buffer, 1024, 0) != 0) {
$output .= $buffer;
}
print_r($output);
?>
Output:
Citaat:
HTTP/1.0 411
Length Required Content-Type: text/html;
charset=UTF-8
Content-Length: 1363
Date: Wed, 30 Sep 2009 13:38:57 GMT
Server: GFE/2.0 X-XSS-Protection: 0
Google Error
Length Required
POST requests require a Content-length header.
En fsockopen:
<?php
$server = "gdata.youtube.com";
$script = "/action/GetUploadToken";
$sock = fsockopen($server, 80);
if ( $sock )
{
$szParams = $data['file'];
$headers= "POST ".$script." HTTP/1.1\r\n";
$headers.= "Host: gdata.youtube.com\r\n";
$headers.= "Authorization: AuthSub token=\"".$this->auth."\"\r\n";
$headers.= "GData-Version: 2\r\n";
$headers.= "X-GData-Client: ".$this->sid."\r\n";
$headers.= "X-GData-Key: key=".$this->dev_key."\r\n";
$headers.= "Content-Length: ".strlen($szParams)."\r\n";
$headers.= "Content-Type: application/atom+xml; charset=UTF-8\r\n";
$headers.= "API_XML_request\r\n";
$headers.= "Connection: close\r\n";
$headers.= "\r\n";
fwrite($sock, $headers);
fwrite($sock, $szParams);
$output = "";
while ( !feof($sock) )
{
$output .= fread($sock, 4096);
}
print_r($output);
}
?>
<?php
$server = "gdata.youtube.com";
$script = "/action/GetUploadToken";
$sock = fsockopen($server, 80);
if ( $sock )
{
$szParams = $data['file'];
$headers= "POST ".$script." HTTP/1.1\r\n";
$headers.= "Host: gdata.youtube.com\r\n";
$headers.= "Authorization: AuthSub token=\"".$this->auth."\"\r\n";
$headers.= "GData-Version: 2\r\n";
$headers.= "X-GData-Client: ".$this->sid."\r\n";
$headers.= "X-GData-Key: key=".$this->dev_key."\r\n";
$headers.= "Content-Length: ".strlen($szParams)."\r\n";
$headers.= "Content-Type: application/atom+xml; charset=UTF-8\r\n";
$headers.= "API_XML_request\r\n";
$headers.= "Connection: close\r\n";
$headers.= "\r\n";
fwrite($sock, $headers);
fwrite($sock, $szParams);
$output = "";
while ( !feof($sock) )
{
$output .= fread($sock, 4096);
}
print_r($output);
}
?>
zelfde output (
genkstar - 30/09/2009 17:03
Nieuw lid
required - This code indicates that a required field is missing or has an empty value.
SvenP - 30/09/2009 17:07
PHP interesse
kan het mis hebben hoor, maar kan het zijn dat de api dinges hoofdlettergevoelig is?
$headers.= "Content-Length: ".strlen($szParams)."\r\n";
$headers .= "Content-Length: " . strlen ( $szParams ) . "\r \n " ;
Maak van die L een lowercase l. als in het bericht
Citaat:
POST requests require a Content-length header.
Richard - 30/09/2009 17:28
Crew algemeen
Lijkt me sterk, Content-Length is HTTP/1.1 standaard protocol.
Verder, de content length header klopt gewoon niet. Simpel genoeg.
Overigens klopt je hele body gewoon niet, die hoor je met bijvoorbeeld PHP.net: http_build_query op te bouwen.
peterdg - 30/09/2009 18:44
Nieuw lid
Okej ik weet zeg maar ongeveer wat de probleem is.
Wanneer ik mijn verstuurde waardes controlleer met wireshark krijg ik dit te zien:
POST /action/GetUploadToken HTTP/1.0\r\n
Citaat:
Request Method: POST
Request URI: /action/GetUploadToken
Request Version: HTTP/1.0
Host: gdata.youtube.comrn
Authorization: AuthSub token="D[..HELE lange token code hier..]wgn
En na authorization zie ik niets meer.. Althans niet in de kader waar je de tabellen mooi gestructureerd krijg, maar in de onderste kader van wireshark.
Echter als ik mijn content length boven authorization token ding zet dan zie ik het wel in dat tabelletje van wireshark :S
POST /action/GetUploadToken HTTP/1.0\r\n
Citaat:
Request Method: POST
Request URI: /action/GetUploadToken
Request Version: HTTP/1.0
Host: gdata.youtube.comrn
Content-length: 5rn
Authorization: AuthSub token="D[..HELE lange token code hier..]qgn
maar dan krijg ik een leuke error:
Token invalid - Invalid AuthSub token.
Error 401
Gesponsorde links
Dit onderwerp is gesloten .