[已解决] php 中用 file_get_contents() 提交 xml 请求?

软件和网站开发以及相关技术探讨
回复
头像
leni
帖子: 1989
注册时间: 2008-09-28 17:24

[已解决] php 中用 file_get_contents() 提交 xml 请求?

#1

帖子 leni » 2012-12-26 22:48

我在做一个小 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 ... l_comments

我搜到一些教程都是用 curl 的。我没有 curl,只能用 file_get_contents() 来做。我不太清楚怎么写 header,代码也不成功。
latex 是个命令集,不是软件,所以在应用程序里找不到,也不存在启动。使用的话,自己写个 .tex 的文件,用 latex 编译。viewtopic.php?f=35&t=331555 的 4楼 有入门教程PDF下载。
头像
leni
帖子: 1989
注册时间: 2008-09-28 17:24

Re: [已解决] php 中用 file_get_contents() 提交 xml 请求?

#2

帖子 leni » 2013-01-17 21:08

还是用 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 编译。viewtopic.php?f=35&t=331555 的 4楼 有入门教程PDF下载。
头像
jarlyyn
帖子: 4671
注册时间: 2006-04-12 18:54
联系:

Re: [已解决] php 中用 file_get_contents() 提交 xml 请求?

#3

帖子 jarlyyn » 2013-01-23 17:13

其实我是没看懂你地一个帖子。
file_get_contents可以发送post请求
头像
jarlyyn
帖子: 4671
注册时间: 2006-04-12 18:54
联系:

Re: [已解决] php 中用 file_get_contents() 提交 xml 请求?

#4

帖子 jarlyyn » 2013-01-23 17:15

$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);
}
头像
leni
帖子: 1989
注册时间: 2008-09-28 17:24

Re: [已解决] php 中用 file_get_contents() 提交 xml 请求?

#5

帖子 leni » 2013-01-24 4:05

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 编译。viewtopic.php?f=35&t=331555 的 4楼 有入门教程PDF下载。
头像
leni
帖子: 1989
注册时间: 2008-09-28 17:24

Re: [已解决] php 中用 file_get_contents() 提交 xml 请求?

#6

帖子 leni » 2013-01-24 4:19

不过现在问题是提交个请求浏览器的卡死10分钟,代码又找不出问题,想杀人了都。
latex 是个命令集,不是软件,所以在应用程序里找不到,也不存在启动。使用的话,自己写个 .tex 的文件,用 latex 编译。viewtopic.php?f=35&t=331555 的 4楼 有入门教程PDF下载。
头像
jarlyyn
帖子: 4671
注册时间: 2006-04-12 18:54
联系:

Re: [已解决] php 中用 file_get_contents() 提交 xml 请求?

#7

帖子 jarlyyn » 2013-01-24 10:54

$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......
头像
leni
帖子: 1989
注册时间: 2008-09-28 17:24

Re: [已解决] php 中用 file_get_contents() 提交 xml 请求?

#8

帖子 leni » 2013-01-24 17:30

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 编译。viewtopic.php?f=35&t=331555 的 4楼 有入门教程PDF下载。
头像
jarlyyn
帖子: 4671
注册时间: 2006-04-12 18:54
联系:

Re: [已解决] php 中用 file_get_contents() 提交 xml 请求?

#9

帖子 jarlyyn » 2013-01-24 18:36

用我的代码,加在customerheader里,可以做。
header可以传数组进去
回复