通过cURL和PHP在Google Docs中创建空的电子表格

问题描述:

我正尝试在不使用Zend库的情况下在Google文档中创建新的电子表格。我通过文档工作,并试图按照步骤,但现在我卡住了。我的代码看起来像这样通过cURL和PHP在Google Docs中创建空的电子表格

$curl = curl_init(); 

    $headers = array(
     "GData-Version: 3.0", 
     "Authorization: GoogleLogin auth=" . $myToken, 
     "Content-Length: 350", 
     "Content-Type: application/atom+xml", 
     "X-Upload-Content-Length: 0" 
    );  

    $post_field = '<?xml version="1.0" encoding="UTF-8"?> 
     <entry xmlns="http://www.w3.org/2005/Atom" xmlns:docs="http://schemas.google.com/docs/2007"> 
     <category scheme="http://schemas.google.com/g/2005#kind" 
      term="http://schemas.google.com/docs/2007#spreadsheet"/> 
     <title>Mytitle</title> 
     </entry>'; 

    curl_setopt($curl, CURLOPT_URL, 'https://docs.google.com/feeds/upload/create-session/default/private/full'); 
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($curl, CURLOPT_POSTFIELDS, $post_field); 
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); 
    curl_setopt($curl, CURLOPT_POST, true); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  

    $response = curl_exec($curl); 
    curl_close($curl); 

该令牌似乎工作正常,因为我能够使用它从Google文档检索数据。我惊讶的是,我没有得到API的回应。在尝试不同的解决方案时,我通常会收到一条错误消息,但这里的响应字符串是空的。

+0

什么是您收到的HTTP响应代码? ('var_dump(curl_getinfo($ curl,CURLINFO_HTTP_CODE));') – DaveRandom

+0

我得到一个200的状态代码,所以请求成功。我正在寻找的请求代码是201 – Marco

您正在发送启动可恢复上传会话(https://developers.google.com/gdata/docs/resumable_upload)的请求,但从未发送实际内容。创建一个空文件时不需要

可恢复上传,而不是发送请求至https://docs.google.com/feeds/default/private/full

如果可以的话,你应该使用谷歌云端硬盘API的files.insert方法来代替。