通过PHP中的CURL获取Google Sites API - 获取"prolog中不允许使用内容".

Wal*_*ter 8 php curl google-sites

$curl = new Curl();
$data = 'Email='.urlencode('MYEMAIL@EMAIL.COM').'&Passwd='.urlencode('MYPASSWORD').'&accountType=GOOGLE&source=Google-cURL-Example&service=jotspot';
$curl->post('https://www.google.com/accounts/ClientLogin',$data);

//match authorization token
preg_match("!Auth=(.*)!",$curl->response,$match);
$auth = $match[1];

//set curl headers
$curl->set_headers(array(
'Content-Type: application/atom+xml; charset=utf-8',
'Host: sites.google.com',
'GData-Version: 1.4',
'Authorization: GoogleLogin auth='. trim($auth)));

//get a list of sites associated with my domain
$curl->get('https://sites.google.com/feeds/site/clevertechie.mygbiz.com'); 

//contains data returned by $curl->get();
echo $curl->response;
Run Code Online (Sandbox Code Playgroud)

因此,我没有从$ curl-> response获取网站列表,而是收到一条消息 - "prolog中不允许内容." 我到处寻找并且找不到解决方案,请帮忙!谢谢!:)

这是应该由响应返回的XML:

<?xml version='1.0' encoding='UTF-8'?>
<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gAcl='http://schemas.google.com/acl/2007' xmlns:sites='http://schemas.google.com/sites/2008' xmlns:gs='http://schemas.google.com/spreadsheets/2006' xmlns:dc='http://purl.org/dc/terms' xmlns:batch='http://schemas.google.com/gdata/batch' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'>
<updated>2012-10-31T19:00:17.297Z</updated>
<app:edited xmlns:app='http://www.w3.org/2007/app'>2012-10-31T19:00:17.297Z</app:edited>
<title>My Site Title</title>
<summary>My Site Summary</summary>
<sites:siteName>my-site-title</sites:siteName>
<sites:theme>slate</sites:theme>
</entry>
Run Code Online (Sandbox Code Playgroud)

我无法粘贴"https://sites.google.com/feeds/site/clevertechie.mygbiz.com"的来源,因为如果未在头文件中指定授权令牌,则无法直接访问该源.检索其数据的唯一方法是使用我已完成的标题中的标记.而不是获得XML,我得到"prolog中不允许内容".

$ curl的var_dump:

object(Curl)#1 (11) { ["curl_resource":protected]=> resource(4) of type (Unknown) ["proxy":protected]=> bool(false) 
["proxy_type":protected]=> NULL ["response"]=> string(33) "Content is not allowed in prolog." ["time"]=> float(249)
 ["info"]=> array(26) { ["url"]=> string(59) "https://sites.google.com/feeds/site/clevertechie.mygbiz.com" 
 ["content_type"]=> string(24) "text/html; charset=UTF-8" ["http_code"]=> int(400) ["header_size"]=> int(676) 
 ["request_size"]=> int(1935) ["filetime"]=> int(-1) ["ssl_verify_result"]=> int(20) ["redirect_count"]=> int(0) 
 ["total_time"]=> float(0.249) ["namelookup_time"]=> float(0.015) ["connect_time"]=> float(0.046) 
 ["pretransfer_time"]=> float(0.109) ["size_upload"]=> float(111) ["size_download"]=> float(33) 
 ["speed_download"]=> float(132) ["speed_upload"]=> float(445) ["download_content_length"]=> float(-1) 
 ["upload_content_length"]=> float(111) ["starttransfer_time"]=> float(0.249) ["redirect_time"]=> float(0) 
 ["certinfo"]=> array(0) { } ["primary_ip"]=> string(14) "74.125.224.194" ["primary_port"]=> int(443) 
 ["local_ip"]=> string(13) "192.168.1.133" ["local_port"]=> int(61985) ["redirect_url"]=> string(0) "" } 
 ["error"]=> NULL ["custom_headers"]=> NULL ["cookie_file"]=> string(46) "cookies.txt" 
 ["custom_curl_options":protected]=> array(3) { [47]=> int(1) [10015]=> string(111) 
 "Email=MYEMAIL&Passwd=MYPASSWORD&accountType=GOOGLE&source=Google-cURL-Example&service=jotspot" 
 [10023]=> array(4) { [0]=> string(49) "Content-Type: application/atom+xml; charset=utf-8" 
 [1]=> string(22) "Host: sites.google.com" [2]=> string(18) "GData-Version: 1.4" [3]=> string(320) 
 "Authorization: GoogleLogin auth=DQAAAMMAAAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" } } ["curl_options":protected]=> array(9) { [10018]=> string(74) 
 "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20100101 Firefox/14.0.1" [10016]=> string(22) 
 "http://www.google.com/" [13]=> int(60) [78]=> int(60) [19913]=> int(1) [52]=> int(1) [64]=> int(0) [81]=> int(0) [42]=> int(0) } }
Run Code Online (Sandbox Code Playgroud)

$ auth只是一个字符串,它不应该被格式化为XML.我确认没有多余的空格或字符,它与第一个$ curl-> post请求返回的空格或字符完全匹配.

小智 1

将请求类型保留为 POST(内容为atom xml 有效负载),将内容类型设置为 \xe2\x80\x9capplication/atom+xml\xe2\x80\x9d 但将所有 oAuth 作为 GET 传递,即作为 URL 查询字符串上的转义 (urlEncoded) 值。

\n