还是用 fsockopen 解决了。file_get_contents 只适用于 urlencoded 的发送方式,不适用于atom+xml。
[php]// ----------- post a comment --------------------
function _post_youtube_comment($video_id, $comment_content, $develop_key, $access_token){
$data_xml = "<?xml version='1.0' encoding='UTF-8'?><entry xmlns='
http://www.w3.org/2005/Atom'
xmlns:yt='
http://gdata.youtube.com/schemas/2007'>
<content>{$comment_content}</content></entry>";
$part_uri = "/feeds/api/videos/{$video_id}/comments";
$response = _request_by_socket('gdata.youtube.com', $part_uri, $data_xml, $develop_key, $access_token);
return $response;
}
function _request_by_socket($remote_server, $remote_path, $post_string, $d_key, $a_token){
$fp = fsockopen($remote_server, 80, $errno, $errstr, 30);
if (!$fp){
$response = $errstr . $errno ;
}
else{
$req = 'POST '. $remote_path . ' HTTP/1.1' . "\r\n";
$req .= 'Host: ' . $remote_server . "\r\n";
$req .= 'Content-Type: application/atom+xml' . "\r\n";
$req .= 'Content-Length: '.strlen($post_string) . "\r\n";
$req .= 'Authorization: Bearer '.$a_token . "\r\n";
$req .= 'GData-Version: 2' . "\r\n";
$req .= 'X-GData-Key: key=' . $d_key . "\r\n\r\n";
$req .= $post_string;
fwrite($fp, $req);
while (!feof($fp)){
$response = fgets($fp, 4096);
}
fclose($fp);
}
return $response;
}
[/php]