当前时区为 UTC + 8 小时



发表新帖 回复这个主题  [ 9 篇帖子 ] 
作者 内容
1 楼 
 文章标题 : [已解决] php 中用 file_get_contents() 提交 xml 请求?
帖子发表于 : 2012-12-26 22:48 
头像

注册: 2008-09-28 17:24
帖子: 1989
送出感谢: 6
接收感谢: 14
我在做一个小 app 可以同步 youtube 的评论。在 google API 的官网说明上,添加 youtube 评论需要提交如下样例的请求:
引用:
POST /feeds/api/videos/VIDEO_ID/comments HTTP/1.1
Host: gdata.youtube.com
Content-Type: application/atom+xml
Content-Length: CONTENT_LENGTH
Authorization: Bearer ACCESS_TOKEN
GData-Version: 2
X-GData-Key: key=DEVELOPER_KEY

<?xml version="1.0" encoding="UTF-8"?>
<entry xmlns="http://www.w3.org/2005/Atom"
xmlns:yt="http://gdata.youtube.com/schemas/2007">
<content>This is a crazy video.</content>
</entry>

链接:https://developers.google.com/youtube/2.0/developers_guide_protocol_comments

我搜到一些教程都是用 curl 的。我没有 curl,只能用 file_get_contents() 来做。我不太清楚怎么写 header,代码也不成功。


_________________
latex 是个命令集,不是软件,所以在应用程序里找不到,也不存在启动。使用的话,自己写个 .tex 的文件,用 latex 编译。http://forum.ubuntu.com.cn/viewtopic.php?f=35&t=331555 的 4楼 有入门教程PDF下载。


页首
 用户资料  
 
2 楼 
 文章标题 : Re: [已解决] php 中用 file_get_contents() 提交 xml 请求?
帖子发表于 : 2013-01-17 21:08 
头像

注册: 2008-09-28 17:24
帖子: 1989
送出感谢: 6
接收感谢: 14
还是用 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]


_________________
latex 是个命令集,不是软件,所以在应用程序里找不到,也不存在启动。使用的话,自己写个 .tex 的文件,用 latex 编译。http://forum.ubuntu.com.cn/viewtopic.php?f=35&t=331555 的 4楼 有入门教程PDF下载。


页首
 用户资料  
 
3 楼 
 文章标题 : Re: [已解决] php 中用 file_get_contents() 提交 xml 请求?
帖子发表于 : 2013-01-23 17:13 
头像

注册: 2006-04-12 18:54
帖子: 4671
送出感谢: 0 次
接收感谢: 7
其实我是没看懂你地一个帖子。
file_get_contents可以发送post请求


_________________
荃创想


页首
 用户资料  
 
4 楼 
 文章标题 : Re: [已解决] php 中用 file_get_contents() 提交 xml 请求?
帖子发表于 : 2013-01-23 17:15 
头像

注册: 2006-04-12 18:54
帖子: 4671
送出感谢: 0 次
接收感谢: 7
$customHeaders=array();
foreach($this->customHeaders as $key=>$value)
{
$customHeaders[]=$key.': '.$value;
}
$url=$this->serverUrl.strtr($this->apiUrl,$data);
$opt=array(
'http'=>array(
'method' => $this->method,
'header'=>(
array_merge($this->defaultHeaders,$customHeaders)
),
'content'=>$this->content,
),
);
try
{
$ret = @file_get_contents($url,false,stream_context_create($opt));
$json=CJSON::decode($ret);
if (!isset($json))
{
$this->successful=false;
}else{
$this->successful=$this->decode($json);
}


_________________
荃创想


页首
 用户资料  
 
5 楼 
 文章标题 : Re: [已解决] php 中用 file_get_contents() 提交 xml 请求?
帖子发表于 : 2013-01-24 4:05 
头像

注册: 2008-09-28 17:24
帖子: 1989
送出感谢: 6
接收感谢: 14
jarlyyn 写道:
其实我是没看懂你地一个帖子。
file_get_contents可以发送post请求

好像有种说法,这个样子的叫做模拟提交请求。我 access token 就是用 file_get_contents() 取得的,跟你的代码差不多。
样例 post 的格式为
引用:
POST /o/oauth2/token HTTP/1.1
Host: accounts.google.com
Content-Type: application/x-www-form-urlencoded

code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp7&
client_id=8819981768.apps.googleusercontent.com&
client_secret={client_secret}&
redirect_uri=https://oauth2-login-demo.appspot.com/code&
grant_type=authorization_code


https://developers.google.com/accounts/ ... 2WebServer


我的做法是
[php]
function _get_auth_token($params, $code)
{
$url = 'https://accounts.google.com/o/oauth2/token';

$fields = 'code=' . $code . '&';
$fields .= 'client_id=' . $params['client_id'] . '&';
$fields .= 'client_secret=' . $params['client_secret'] . '&';
$fields .= 'redirect_uri=' . $params['redirect_uri'] . '&';
$fields .= 'grant_type=authorization_code';

$response = _do_post($url, 'Content-type: application/x-www-form-urlencoded', $fields);
return $response;

}


function _do_post($url, $header, $content)
{
$opts = array('http' => array(
'method' => 'POST',
'protocol_version' => '1.1',
'header' => $header,
'content' => $content,
'timeout' => '30'
));

$context = stream_context_create($opts);
$response = file_get_contents($url, 0, $context);
var_dump($http_response_header);
echo "<br/>";

return $response;
}
[/php]

我第一个帖子里的 xml 需要的 header 更加复杂点,总之用 file_get_contents() 成功不了。


_________________
latex 是个命令集,不是软件,所以在应用程序里找不到,也不存在启动。使用的话,自己写个 .tex 的文件,用 latex 编译。http://forum.ubuntu.com.cn/viewtopic.php?f=35&t=331555 的 4楼 有入门教程PDF下载。


页首
 用户资料  
 
6 楼 
 文章标题 : Re: [已解决] php 中用 file_get_contents() 提交 xml 请求?
帖子发表于 : 2013-01-24 4:19 
头像

注册: 2008-09-28 17:24
帖子: 1989
送出感谢: 6
接收感谢: 14
不过现在问题是提交个请求浏览器的卡死10分钟,代码又找不出问题,想杀人了都。


_________________
latex 是个命令集,不是软件,所以在应用程序里找不到,也不存在启动。使用的话,自己写个 .tex 的文件,用 latex 编译。http://forum.ubuntu.com.cn/viewtopic.php?f=35&t=331555 的 4楼 有入门教程PDF下载。


页首
 用户资料  
 
7 楼 
 文章标题 : Re: [已解决] php 中用 file_get_contents() 提交 xml 请求?
帖子发表于 : 2013-01-24 10:54 
头像

注册: 2006-04-12 18:54
帖子: 4671
送出感谢: 0 次
接收感谢: 7
$fields = 'code=' . $code . '&';
$fields .= 'client_id=' . $params['client_id'] . '&';
$fields .= 'client_secret=' . $params['client_secret'] . '&';
$fields .= 'redirect_uri=' . $params['redirect_uri'] . '&';
$fields .= 'grant_type=authorization_code';

$response = _do_post($url, 'Content-type: application/x-www-form-urlencoded', $fields);

这一段是作为get参数,又不是作为header......


_________________
荃创想


页首
 用户资料  
 
8 楼 
 文章标题 : Re: [已解决] php 中用 file_get_contents() 提交 xml 请求?
帖子发表于 : 2013-01-24 17:30 
头像

注册: 2008-09-28 17:24
帖子: 1989
送出感谢: 6
接收感谢: 14
jarlyyn 写道:
$fields = 'code=' . $code . '&';
$fields .= 'client_id=' . $params['client_id'] . '&';
$fields .= 'client_secret=' . $params['client_secret'] . '&';
$fields .= 'redirect_uri=' . $params['redirect_uri'] . '&';
$fields .= 'grant_type=authorization_code';

$response = _do_post($url, 'Content-type: application/x-www-form-urlencoded', $fields);

这一段是作为get参数,又不是作为header......


没错。这里的 header 是 Content-type: application/x-www-form-urlencoded。
我是说第一个帖子里那个 header 比较复杂,要
Content-Type: application/atom+xml
Content-Length: CONTENT_LENGTH
Authorization: Bearer ACCESS_TOKEN
GData-Version: 2
X-GData-Key: key=DEVELOPER_KEY
这么长一串。


_________________
latex 是个命令集,不是软件,所以在应用程序里找不到,也不存在启动。使用的话,自己写个 .tex 的文件,用 latex 编译。http://forum.ubuntu.com.cn/viewtopic.php?f=35&t=331555 的 4楼 有入门教程PDF下载。


页首
 用户资料  
 
9 楼 
 文章标题 : Re: [已解决] php 中用 file_get_contents() 提交 xml 请求?
帖子发表于 : 2013-01-24 18:36 
头像

注册: 2006-04-12 18:54
帖子: 4671
送出感谢: 0 次
接收感谢: 7
用我的代码,加在customerheader里,可以做。
header可以传数组进去


_________________
荃创想


页首
 用户资料  
 
显示帖子 :  排序  
发表新帖 回复这个主题  [ 9 篇帖子 ] 

当前时区为 UTC + 8 小时


在线用户

正在浏览此版面的用户:没有注册用户 和 0 位游客


不能 在这个版面发表主题
不能 在这个版面回复主题
不能 在这个版面编辑帖子
不能 在这个版面删除帖子
不能 在这个版面提交附件

前往 :  
本站点为公益性站点,用于推广开源自由软件,由 DiaHosting VPSBudgetVM VPS 提供服务。
我们认为:软件应可免费取得,软件工具在各种语言环境下皆可使用,且不会有任何功能上的差异;
人们应有定制和修改软件的自由,且方式不受限制,只要他们自认为合适。

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
简体中文语系由 王笑宇 翻译